473,398 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 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 3965
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. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.