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;
}
@ 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.
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;
}
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;
}