472,374 Members | 1,363 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 software developers and data experts.

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 3907
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Dan Greenblatt | last post by:
I am writing some software that, among other things, needs to track the state of database tables. This includes occasionally checking the table to see what records or added, modified, or deleted....
5
by: gsb | last post by:
I track the mouse location like this code: function mousePos(e) { var p = new Object(); if(e) { p.x = e.pageX; p.y = e.pageY; } else { p.x = event.x; p.y = event.y; } ... (show) }...
5
by: byrocat | last post by:
Sybase and DB2 both have the capability of tracking user activities at a number of levels: invalid access attempts to databases, table, etc.; creation/deletion/modification of database...
3
by: Kyle Friesen via AccessMonster.com | last post by:
Mike, I have databse that creates a "tracking number" based on the selections made on the form via concatenating. At the end of the tracking number, I need a two digit (01-99) sequence number by...
1
by: Muddassir | last post by:
hi everybody I am writing an application for tracking files and directory changes I used FindFirstChangeNotification FindNextChangeNotification FindCloseChangeNotification...
4
by: Glenn Owens | last post by:
I have a DataGrid web control which I've dynamically populated with template columns to be used for bulk-editting. Generally, all of the columns are textbox and/or dropdownlist child controls. ...
2
by: C# programmer | last post by:
Hi All, I'm working on a project which requires tracking of recent document downloads. There is a feature in which user can download the docs without logining into the website for some of the...
3
by: =?Utf-8?B?R3JhaGFt?= | last post by:
I've added 2 tracking services to the wf runtime; one is the standard SqlTrackingService: trackingService = new SqlTrackingService(<trackingConnectionString>); <workflow...
0
by: LiveTecs | last post by:
http://www.livetecs.com TimeLive Web Collaboration Suite is an integrated suite that allows you to manage project life cycle including tasks, issues, bugs, timesheet, expense, attendance. ...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.