[C#] [CF] [VS 2005 B2] FileStream ?

How to use afileStream.Position ?


FileStream fileStream =newFileStream(generatedPath,FileMode.OpenOrCreate);

StreamWriter streamWriter =newStreamWriter(fileStream);

StreamReader streamReader =newStreamReader(fileStream);

stringline ="";

CSLogger.Debug("stream length = " + fileStream.Length);

while ( (line = streamReader.ReadLine() ) !=null)

{

CSLogger.Debug("read = " +line +" and pos "+ fileStream.Position);

if (line.Split('=')[0] == key)

{

value ="trouv";

CSLogger.Debug("key find at pos = "+fileStream.Position);

break;

}

}


When i write that all the logged message shox the same thingTongue Tied
fileStream.Length == fileStream.Position

Where ever i am...

[3131 byte] By [lmussier] at [2008-2-21]
# 1
Better !

Here is a sample file.

line1
line2
line3
line4
line5
line6

here is the reading code...


FileStream fileStream = new FileStream(generatedPath, FileMode.OpenOrCreate);StreamWriter streamWriter = new StreamWriter(fileStream);
StreamReader streamReader = new StreamReader(fileStream);

CSLogger.Debug("stream length = " + fileStream.Length);
CSLogger.Debug("read {0} pos {1} ", streamReader.ReadLine(), fileStream.Position);
CSLogger.Debug("read {0} pos {1} ", streamReader.ReadLine(), fileStream.Position);
CSLogger.Debug("read {0} pos {1} ", streamReader.ReadLine(), fileStream.Position);
CSLogger.Debug("read {0} pos {1} ", streamReader.ReadLine(), fileStream.Position);
CSLogger.Debug("read {0} pos {1} ", streamReader.ReadLine(), fileStream.Position);

and now the log file...



2005/07/21 - 16:42:43:000 #Debug Level# stream length = 45
2005/07/21 - 16:42:43:000 #Debug Level# read line1 pos 45
2005/07/21 - 16:42:43:000 #Debug Level# read line2 pos 45
2005/07/21 - 16:42:43:000 #Debug Level# read line3 pos 45
2005/07/21 - 16:42:43:000 #Debug Level# read line4 pos 45
2005/07/21 - 16:42:43:000 #Debug Level# read line5 pos 45

lmussier at 2007-9-9 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...
# 2

Nobody can see the a solution ?

lmussier at 2007-9-9 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...
# 3
Hi lmussier,

FileStream.Position is the same as FileStream.Length in this case because StreamReader maintains an internal buffer on top of FileStream. When you do a StreamReader.ReadLine(), the internal buffer is filled with the actual contents of the FileStream object, in this case the contents of the entire file. This explains why the FileStream position stays the same as you read through the file with StreamReader. To obtain positional information about the file, either count the size of the array returned by StreamReader.ReadLine(), or use FileStream.Read() directly.

Cheers,
Anthony [MSFT]

AnthonyWong at 2007-9-9 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...