1

I need to read last 10 lines in my file. File is about a 1MB, but I need code to be very efficient because of it will be running in a limited device.

PD: I've tested code in this post and no one is working for me.

EDIT This code is not working because of NETMF 4.1 does not have GetByteCount and GetString, do you know any other way to do this?

Community
  • 1
  • 1
Manu
  • 1,130
  • 3
  • 10
  • 26
  • Could you explain more precisely what is not working for you? Are you getting an error message? How is the actual result different than the expected result? – Darin Dimitrov Dec 22 '11 at 09:15
  • 2
    Have you tried the code in http://stackoverflow.com/questions/452902/how-to-read-a-text-file-reversely-with-iterator-in-c-sharp/452945#452945? Note that you'll need to say what the encoding of the file it - it makes a huge difference. – Jon Skeet Dec 22 '11 at 09:16
  • None working you need to expand that part – V4Vendetta Dec 22 '11 at 09:16
  • Could you specify what "no one is working" means? What have you tried, and what happens? – Guffa Dec 22 '11 at 09:16
  • This one http://stackoverflow.com/a/398512/1004962 only return me one line and this http://stackoverflow.com/a/399841/1004962 is not returning last 10 lines and are not in right order. – Manu Dec 22 '11 at 09:25

2 Answers2

1

This is How I've finally solved. Anyway code is too slow so if any of you have any advice, please tell me:

    public static string ReadEndTokens(string filename, Int64 numberOfTokens, Encoding encoding, string tokenSeparator)
    {
        lock (typeof(SDAccess))
        {
            PersistentStorage sdPS = new PersistentStorage("SD");
            sdPS.MountFileSystem();
            string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;

            int sizeOfChar = 1;//The only encoding suppourted by NETMF4.1 is UTF8
            byte[] buffer = encoding.GetBytes(tokenSeparator);


            using (FileStream fs = new FileStream(rootDirectory + @"\" + filename, FileMode.Open, FileAccess.ReadWrite))
            {
                Int64 tokenCount = 0;
                Int64 endPosition = fs.Length / sizeOfChar;

                for (Int64 position = sizeOfChar; position < endPosition; position += sizeOfChar)
                {
                    fs.Seek(-position, SeekOrigin.End);
                    fs.Read(buffer, 0, buffer.Length);

                    encoding.GetChars(buffer);
                    if (encoding.GetChars(buffer)[0].ToString() + encoding.GetChars(buffer)[1].ToString() == tokenSeparator)
                    {
                        tokenCount++;
                        if (tokenCount == numberOfTokens)
                        {
                            byte[] returnBuffer = new byte[fs.Length - fs.Position];
                            fs.Read(returnBuffer, 0, returnBuffer.Length);                          
                            sdPS.UnmountFileSystem();// Unmount file system
                            sdPS.Dispose();
                            return GetString(returnBuffer);
                        }
                    }
                }

                // handle case where number of tokens in file is less than numberOfTokens
                fs.Seek(0, SeekOrigin.Begin);
                buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                sdPS.UnmountFileSystem();// Unmount file system
                sdPS.Dispose();
                return GetString(buffer);
            }
        }
    }

    //As GetString is not implemented in NETMF4.1 I've done this method
    public static string GetString(byte[] bytes)
    {
        string cadena = "";
        for (int i = 0; i < bytes.Length; i++)
            cadena += Encoding.UTF8.GetChars(bytes)[i].ToString();
        return cadena;
    }
Manu
  • 1,130
  • 3
  • 10
  • 26
-1

Try: File.ReadLines(myFilePath); and use Linq. http://msdn.microsoft.com/en-us/library/dd383503.aspx

juFo
  • 17,849
  • 10
  • 105
  • 142