473,503 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sr.ReadLine() problem

data is like below
1111
2222
3333
4444
5555

while((str1 = sr.ReadLine()) != null)
{
str2 = sr.ReadLine()
}

in this case
str1 read 1111 and then
str2 read 2222 and then
str1 read 3333.......

but i wanna read 2222 to str1 again.
is there any way to solve this?
Dec 5 '06 #1
4 2169
Not really; they are largely sequential and forwards only...

you can "peek", but only 1 character... is this sufficient? i.e. to
notice the 3? Alternatively, how many rows do you ever need to go back?
If just the last you could keep a buffer copy of the last line?

Otherwise you may need to start again... with a stream backer this
might mean Seek() [if the stream is seekable]. Otherwise you might need
to start the stream/string again with a new reader. For this reason,
when processing input from non-seekable / repeatable sources it is
sometimes necessary to write the data to another device (e.g. a
FileStream) first, so that you can subsequently Seek().

Generally, the better design is to write your code (where possible) to
do a single pass.

Marc

Dec 5 '06 #2
Hello PointMan,
>is there any way to solve this?
Assuming that your "sr" variable is of type StreamReader, a line like this
could help you (to reset the reader to the beginning before the second
ReadLine() call):

sr.BaseStream.Seek(0, SeekOrigin.Begin);

I must admit that your algorithm isn't really clear to me - it might make
a lot more sense to restructure it, maybe change the loop type... we can
probably help you better if you describe what you want to do.
Oliver Sturm
--
http://www.sturmnet.org/blog
Dec 5 '06 #3
If you are saying that, as you are reading the data, each line has some of
form of relationship to the following line that needs to be taken into
account while processing the original line then you you need to change your
construct to something like this:

string str1 = sr.ReadLine();

while (str1 != null)
{

// do some magic with the line

// read the following line
str2 = sr.ReadLine()
// break if it wasn't there cos our rule has been broken
if (str2 == null)
break;

// do some magic with the following line

// simulate a ReadLine to make sure the next iteration is using the
right values
str1 = str2;
}
"PointMan" <so**********@gmail.comwrote in message
news:e3**************@TK2MSFTNGP06.phx.gbl...
data is like below
1111
2222
3333
4444
5555

while((str1 = sr.ReadLine()) != null)
{
str2 = sr.ReadLine()
}

in this case
str1 read 1111 and then
str2 read 2222 and then
str1 read 3333.......

but i wanna read 2222 to str1 again.
is there any way to solve this?

Dec 5 '06 #4
Hi,

Your best bet is probably to store previous lines as long as you need them.

while((str1 = sr.ReadLine()) != null)
{
// process str1, the last line will be stored in str2
str2 = str1;
}
On Tue, 05 Dec 2006 23:29:00 +0100, PointMan <so**********@gmail.com
wrote:
data is like below
1111
2222
3333
4444
5555

while((str1 = sr.ReadLine()) != null)
{
str2 = sr.ReadLine()
}

in this case
str1 read 1111 and then
str2 read 2222 and then
str1 read 3333.......

but i wanna read 2222 to str1 again.
is there any way to solve this?



--
Happy Coding!
Morten Wennevik [C# MVP]
Dec 6 '06 #5

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

Similar topics

6
4890
by: Russell E. Owen | last post by:
At one time, mixing for x in file and readline was dangerous. For example: for line in file: # read some lines from a file, then break nextline = readline() # bad would not do what a naive...
0
4185
by: John C. Worsley | last post by:
I've got an extremely inscrutable problem here using Perl's Term::ReadLine::Gnu module. I'm using Perl 5.8.0, readline 4.3 and Term::ReadLine::Gnu 1.14. The problem is specific to catching INT...
1
4396
by: Jian Qiu | last post by:
Hi, I tried to install python2.4.2. Unfortunately building 'readline' extension failed. Here is what I got: (It is a bit long. If you are impatient, please look at the end where it reports the...
1
5170
by: Kevin | last post by:
In a newsgroup thread from Jan 8, 2003 between Barry Holsinger and the VBDotNet Team, please review this excerpt: "You understood my problem completely. Your sample code provides a really...
2
5341
by: Kin | last post by:
I am trying to read the output of an external application using redirected stdout and StreamReader::ReadLine(). The problem is that ReadLine() blocks and I am either reading nothing or just part...
2
8448
by: Eddy | last post by:
I have a big problem with streamreader ReadLine()! I read from a long text files about 13k lines, than I encounter a problem: ReadLine() is not anymore able to go on! I have a string whose name is...
6
3710
by: Sean Davis | last post by:
I have a large file that I would like to transform and then feed to a function (psycopg2 copy_from) that expects a file-like object (needs read and readline methods). I have a class like so: ...
0
2432
by: Akira Kitada | last post by:
Hi list, I was trying to build Python 2.6 on FreeBSD 4.11 and found it failed to build some of the modules. """ Failed to find the necessary bits to build these modules: _bsddb ...
0
1505
by: M.-A. Lemburg | last post by:
On 2008-10-25 08:39, Akira Kitada wrote: Please post a bug report on python.org about these failures. The multiprocessing module is still fairly new and obviously needs more fine tuning for...
0
1924
by: Akira Kitada | last post by:
Hi Marc-Andre, Thanks for the suggestion. I opened a ticket for this issue: http://bugs.python.org/issue4204 Now I understand the state of the multiprocessing module, but it's too bad to see...
0
7064
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
7315
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...
1
6974
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...
0
7445
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...
0
5559
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,...
1
4991
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...
0
4665
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.