Main Site Documentation

StreamReader always EndOfStream when reading simple text file from SD Card


#1

Hi, I’m totally lost at this point, I added the ReadLineEx method as suggested but now I’ve come up against another head scratcher, this should be simple but I see no reason why my code is failing, basically I have a text file which has only one line, that line will contain a number and nothing else, in my example it should be 199 returned but the sr.EndOfStream property is always true even though I explicitly tell it the seek to the beginning of the base stream


public static int GetCount(string fileName)
        {
            var count = 0;
            SdCard.Mount();

            while (!SdCard.Mounted) { }

            var stream = new FileStream(fileName, FileMode.Open);

            using (var sr = new StreamReader(stream))
            {
                string lineRead;
                // Read and display lines from the file until the end of
                // the file is reached.
                sr.BaseStream.Seek(0, SeekOrigin.Begin);
                while (sr.EndOfStream == false)
                {
                    lineRead = ReadLineEx(sr);
                    count = Convert.ToInt32(lineRead);                  
                }
            }

            SdCard.Unmount();

            while (SdCard.Mounted) { }

            return count;
        }

#2

Hi,
perhaps this code will work?


sr.BaseStream.Seek(0, SeekOrigin.Begin);
            bool EndReached = false;
            while (EndReached == false)
            {
                lineRead = ReadLineEx(sr);
                count = Convert.ToInt32(lineRead);
                EndReached = sr.EndOfStream;
            }

or


 sr.BaseStream.Seek(0, SeekOrigin.Begin);
            do
            {
                lineRead = ReadLineEx(sr);
                count = Convert.ToInt32(lineRead);
            }
            while(sr.EndOfStream == false);  //Edit: corrected



#3

@ RoSchmi - Thanks the first code block worked, the second one gets stuck in an endless loop, but I think I’m fooling myself with this code, the sr.EndOfStream property is always true which makes no sense, the real question here is why is this always true, it make no sense.


#4

I think it is only always true as long as you have only one line in your file.


#5

@ RoSchmi - I’ve tested with a file which has 2 lines, sr.EndOfStream is still always true??


#6

Sorry, my suggestion was wrong I did not look deep enough in your problem.


#7

I do not know if this is exactly what you want:


public static int GetCount(string fileName)
        {
            var count = 0;
            int lineCount = 0;
            int wantedLine = 0;

            var stream = new FileStream(fileName, FileMode.Open);
            using (var sr = new StreamReader(stream))
            {
                string lineRead;
                
                while ((lineRead = ReadLineEx(sr)) != null)
                {
                    if (lineCount == wantedLine)
                        count = int.Parse(lineRead);
                    Debug.Print(lineRead);
                    lineCount++;
                }
            }
            return count;
        }


#8

Thanks all, below is what works fine for me

public static int GetTotalLines(string fileName)
        {
            var lineCount = 0;

            var SdCard = new SDCard();

            SdCard.Mount();

            Thread.Sleep(100);

            var stream = new FileStream(fileName, FileMode.Open);

            var sr = new StreamReader(stream);
            lineCount = Convert.ToInt32(sr.ReadToEnd());

            stream.Dispose();
            stream.Close();
            stream = null;

            SdCard.Unmount();

            SdCard.Dispose();

            return lineCount;
        }