473,657 Members | 2,921 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 2175
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.S eek(0, SeekOrigin.Begi n);

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**********@g mail.comwrote in message
news:e3******** ******@TK2MSFTN GP06.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**********@g mail.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
4902
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 user might expect because the file iterator buffered data and readline did not read from that buffer. Hence the call to readline might unexpectedly skip some lines.
0
4194
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 signals while in the readline() function. In C, when I've set a SIGINT handler using signal(), it is immediately called (even in readline()) when I hit CTRL-C. Likewise in Perl, if I am reading in a while() loop from STDIN, and I have set the...
1
4408
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 warning.) building 'readline' extension gcc -pthread -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -fPIC -fno-strict-aliasing -I. -I/nfs/gs/home/jianq/Python-2.4.2/./Include -I/gs/home/jianq/include -I/usr/local/include
1
5187
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 elegant way to inject CrLf into the input stream, which effectively unblocks the ReadLine method. Last night, I had finally got the WriteConsoleInput
2
5367
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 of the output. I start a new process from my app to run the external app. StartInfo.RedirectStandardOutput is set to true. Then I start a thread that just keep reading the standard output of this new process in an infinite loop using...
2
8485
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 riga, where I put the return value of Readline(), and this is what happens, when I read these three lines: 106933;;rosso;ivan mauricio;3347614603;trak15@yahoo.com;1984-06-04;M;;;milano;CO;politecnico # ingegneria;;;...
6
3731
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: class GeneInfo(): def __init__(self): #urllib.urlretrieve('ftp://ftp.ncbi.nih.gov/gene/DATA/ gene_info.gz',"/tmp/gene_info.gz")
0
2439
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 _sqlite3 _tkinter gdbm linuxaudiodev spwd sunaudiodev
0
1518
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 the large set of platforms on which Python can run. However, please also note that FreeBSD4 is a rather old version of that OS. FWIW: Python 2.6 compiles just fine on FreeBSD6. Thanks.
0
1934
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 math, mmap and readline modules, that worked fine before, cannot be built anymore.
0
8820
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8499
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
7314
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5630
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1937
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1601
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.