473,799 Members | 2,885 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read Backwards in a text file

JB
I'm trying to read a text file, and store the position of a specified
line, and then later seek back to that line and start reading again.

I'm using simple code like the following:

StreaReader sr = new StreamReader("t est.txt");
String s = "";
while (S != null)
{
S = sr.ReadLine()
//process line
}
sr.BaseStream.P osition always returns the same value (1024) so isn't
much use in this regard.

I can track the line position manually, but then i cant Seek back to
the start of said line.
Or Can I?

Cheers guys,

Jul 20 '07 #1
2 3381
On Fri, 20 Jul 2007 15:03:21 -0700, JB <ja*******@gmai l.comwrote:
[...]
sr.BaseStream.P osition always returns the same value (1024) so isn't
much use in this regard.

I can track the line position manually, but then i cant Seek back to
the start of said line.
Or Can I?
The BaseStream and the StreamReader are not always at the same position,
because the StreamReader buffers data from the BaseStream. That's why the
BaseStream.Posi tion is returning 1024; the StreamReader has already read
1024 bytes from the BaseStream at the point you check the property's value.

You can go back to a specific position by setting the Position property of
the BaseStream, and then calling DiscardBuffered Data() on the
StreamReader. This will force the StreamReader to sync back up with your
new position in the BaseStream.

So, as long as you keep track of the actual position to which you want to
seek somehow, you can go back to that position any time you like.

Keep in mind that this is only possible, of course, for seekable streams.
Not all streams support seeking.

Pete
Jul 20 '07 #2
JB wrote:
I'm trying to read a text file, and store the position of a specified
line, and then later seek back to that line and start reading again.

I'm using simple code like the following:

StreaReader sr = new StreamReader("t est.txt");
String s = "";
while (S != null)
{
S = sr.ReadLine()
//process line
}
sr.BaseStream.P osition always returns the same value (1024) so isn't
much use in this regard.

I can track the line position manually, but then i cant Seek back to
the start of said line.
Or Can I?

Cheers guys,
The problem with moving the file pointer in a text file is that you want
to move to a specific character in the file, but the file pointer
doesn't know about characters, it only knows about bytes.

You could create your own line based stream reader that uses the fact
that the line break (cr+lf) is of a known size regardless of the
encoding used. You could read the bytes from the stream until you
encounter a line break, and then decode the bytes in the line. That way
you could get the file pointer location of a line.

--
Göran Andersson
_____
http://www.guffa.com
Jul 21 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
17092
by: Chris | last post by:
Can anybody help. I need to read a txt file backwords line by line. Can anybody help me do this. Thanks Chris
8
2351
by: nephish | last post by:
Hey there. i want to set a variable to represent the last line of a text file how do i do that? or even better, how do i create a list of the lines of a text file?
18
5706
by: JG | last post by:
Does anyone know a standard (or supported on Linux, Mac, Win32) way to clear a read stream buffer (standard ANSI C file stream)? I would even settle for a platform specific way of doing it. And no, I know I can use direct low level I/O or non-buffered to do reads, but for my app, I need the buffering. I can implement myself, but this is not optimal. Example, I open a read only file using fopen(). I periodically know
2
527
by: Robbie | last post by:
Anybody out there know of a way to read a text file starting from the bottom of the file then going backwards line by line? I am reading large log files in which the data I'm looking for is always near the bottom. I would like something like: StreamReader sr = new StreamReader("MyFile.txt"); string line = sr.ReadLine(-1); <-- this functionality doesn't exist.
2
6325
by: Lam | last post by:
Hi the default readline method in StreamReader read the text from the top to bottom how can I read a text file from the bottom to the top? Thanks
35
11501
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt", "r+"); fseek(file, -2, SEEK_END); fscanf(file, "%d", &c); this works fine if the integer is only a single character. When I get into larger numbers though (e.g. 502) it only reads in the 2. Is there
24
3227
by: Ground21 | last post by:
Hello. How could I read the whole text file line after line from the end of file? (I want to ~copy~ file) I want to copy file in this way: file 1: a b
1
2965
by: stanats | last post by:
hello i got this problem on how to read a text file backwards, that is to read the last line first and then down to the beginning... can somebody help me how to implement that in C ? thanks
28
3281
by: tlpell | last post by:
Hey, read some tips/pointers on PHP.net but can't seem to solve this problem. I have a php page that reads the contents of a file and then displays the last XX lines of the file. Problem is this...whenever the file gets larger that ~5MB, the page just displays nothing, as though a timeout has occurred but I get no error. At 4.8MB (last confirmed size)...the function still works. Any ideas what code below is lacking?? <? $handle=...
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10249
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10219
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10025
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7563
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5584
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.