Question

I am developing an application which requires to read a text file which is continuously updating. I need to read the file till the end of file(at that very moment) and need to remember this location for my next file read. I am planning to develop this application in C#.net . How can I perform these partial reads and remember the locations as C# does not provide pointers in file handling ?

Edit : File is updated every 30 seconds and new data is appended to the old file. I tried with maintaining the length of previous read and then reading the data from that location but the file can not be accessed by two applications at the same time.

No correct solution

OTHER TIPS

You can maintain the last offset of the read pointer in the file. You can do sth like this

long lastOffset = 0;    

using (var fs = new FileStream("myFile.bin", FileMode.Open))       
{
      fs.Seek(lastOffset, SeekOrigin.Begin);

      // Read the file here 

      // Once file is read, update the lastOffset
      lastOffset=fs.Seek(0, SeekOrigin.End);
 }

Open the file, read everything, save the count of bytes you read in a variable (let's assume it's read_until_here).

next time you read the file, just take the new information (whichever comes after the location of your read_until_here variable ...

I am planning to develop this application in C#.net . How can I perform these partial reads and remember the locations as C# does not provide pointers in file handling ?

Not entirely sure why you'd be concerned about the supposed lack of pointers... I'd look into int FileStream.Read(byte[] array, int offset, int count) myself.

It allows you to read from an offset into a buffer, as many bytes as you want, and tells you how many bytes were actually read... which looks to be all the functionality you'd need.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top