469,657 Members | 1,819 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,657 developers. It's quick & easy.

Tracking File position in C#

Hi,
The need is to read each line in a text file and based on the read
data decision has to be taken whether to reposition the file pointer. The
StreamReader.Position does not give current position. But it gives position
in multiples of 1024. Also, I am not finding a way to reposition the pointer
using FileStream-StreamReader combination. Due to this the condition
(FileLength = = File Position) misbehaves.
I did some investigation and found out only when i use Read method the file
pointer position gets updated after each read operation. Is there any way by
using the StreamReader-FileStream combination can it be done?
How could such file pointer reposition and tracking file pointer position
could be done elegantly in C# ?

Can anyone help?
Thanks,
Senthil

C# code:
---------
FileStream fs=new FileStream(filePath, FileMode.Open,FileAccess.Read);
StreamReader sw=new StreamReader(fs);
long fileLen=fReader.BaseStream.Length;
---
---
string strLine=sw.ReadLine();
---
if(strLine.CompareTo("hello")==0) Reposition file pointer
---
if(fStream.Position>=fileLen) {....}
Nov 17 '05 #1
2 3752
You can use FileStream Seek method

From the doc: "Sets the current position of this stream to the given value."

here is an expample

const string fileName = "Test#@@#.dat";

// Create random data to write to the file.
byte[] dataArray = new byte[100000];
new Random().NextBytes(dataArray);

using(FileStream
fileStream = new FileStream(fileName, FileMode.Create))
{
// Write the data to the file, byte by byte.
for(int i = 0; i < dataArray.Length; i++)
{
fileStream.WriteByte(dataArray[i]);
}

// Set the stream position to the beginning of the file.
fileStream.Seek(0, SeekOrigin.Begin);

// Read and verify the data.
for(int i = 0; i < fileStream.Length; i++)
{
if(dataArray[i] != fileStream.ReadByte())
{
Console.WriteLine("Error writing data.");
return;
}
}
Console.WriteLine("The data was written to {0} " +
"and verified.", fileStream.Name);
}

--
Vadym Stetsyak aka Vadmyst

"Senthil" <se*************@hotmail.com> wrote in message
news:u2*************@TK2MSFTNGP15.phx.gbl...
Hi,
The need is to read each line in a text file and based on the read
data decision has to be taken whether to reposition the file pointer. The
StreamReader.Position does not give current position. But it gives position in multiples of 1024. Also, I am not finding a way to reposition the pointer using FileStream-StreamReader combination. Due to this the condition
(FileLength = = File Position) misbehaves.
I did some investigation and found out only when i use Read method the file pointer position gets updated after each read operation. Is there any way by using the StreamReader-FileStream combination can it be done?
How could such file pointer reposition and tracking file pointer position
could be done elegantly in C# ?

Can anyone help?
Thanks,
Senthil

C# code:
---------
FileStream fs=new FileStream(filePath, FileMode.Open,FileAccess.Read);
StreamReader sw=new StreamReader(fs);
long fileLen=fReader.BaseStream.Length;
---
---
string strLine=sw.ReadLine();
---
if(strLine.CompareTo("hello")==0) Reposition file pointer
---
if(fStream.Position>=fileLen) {....}

Nov 17 '05 #2
Senthil <se*************@hotmail.com> wrote:
The need is to read each line in a text file and based on the read
data decision has to be taken whether to reposition the file pointer. The
StreamReader.Position does not give current position. But it gives position
in multiples of 1024. Also, I am not finding a way to reposition the pointer
using FileStream-StreamReader combination. Due to this the condition
(FileLength = = File Position) misbehaves.
I did some investigation and found out only when i use Read method the file
pointer position gets updated after each read operation. Is there any way by
using the StreamReader-FileStream combination can it be done?
How could such file pointer reposition and tracking file pointer position
could be done elegantly in C# ?


Well, if it's a simple encoding (eg ISO-8859-1, or ASCII) you could
assume a byte per character, and increment your own counter.

Note that if you reposition the stream, you should call
StreamWriter.DiscardBufferedData().

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Dan Greenblatt | last post: by
5 posts views Thread by byrocat | last post: by
3 posts views Thread by Kyle Friesen via AccessMonster.com | last post: by
1 post views Thread by Muddassir | last post: by
4 posts views Thread by Glenn Owens | last post: by
2 posts views Thread by C# programmer | last post: by
reply views Thread by LiveTecs | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.