473,320 Members | 2,048 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,320 software developers and data experts.

getline problems

hi,
i'm using code like this

string s
while(getline(cin,s)){
process(s);
}
// this is the last line
process(s);

process does some replacement and rescanning. The problem is i can't
strip or add newlines. So i don't know whether the lastline contains a
'\n' or it was just EOF.

I can't read the whole buffer as it is too huge and some line doesn't
need to be replaced.

Using while(fgets(str,MAX,stdin){
s=str
}
works but again this conversion of str to s is an overhead.

Can you suggest on how to overcome on this getline issue ?
Nov 9 '08 #1
10 5165
Terry IT wrote:
hi,
i'm using code like this

string s
while(getline(cin,s)){
process(s);
}
// this is the last line
process(s);
This is wrong. s will not have new data after the loop.
Nov 9 '08 #2
On Nov 9, 9:19*am, red floyd <no.spam.h...@example.comwrote:
Terry IT wrote:
hi,
*i'm using code like this
string s
*while(getline(cin,s)){
* * process(s);
* }
// this is the last line
* process(s);

This is wrong. *s will not have new data after the loop.
i thought if file contains no newline ,then s contains all the chars
until the end of stream.
Nov 9 '08 #3
Terry IT wrote:
On Nov 9, 9:19*am, red floyd <no.spam.h...@example.comwrote:
>Terry IT wrote:
hi,
i'm using code like this
string s
while(getline(cin,s)){
process(s);
}
// this is the last line
process(s);

This is wrong. *s will not have new data after the loop.

i thought if file contains no newline ,then s contains all the chars
until the end of stream.
The point is not what s contains. The point is that you are processing the
last line twice. That is probably not what you want.
Best

Kai-Uwe Bux
Nov 9 '08 #4
On Nov 9, 11:33*am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
Terry IT wrote:
On Nov 9, 9:19*am, red floyd <no.spam.h...@example.comwrote:
Terry IT wrote:
hi,
i'm using code like this
string s
while(getline(cin,s)){
process(s);
}
// this is the last line
process(s);
This is wrong. *s will not have new data after the loop.
i thought if file contains no newline ,then s contains all the chars
until the end of stream.

The point is not what s contains. The point is that you are processing the
last line twice. That is probably not what you want.

Best

Kai-Uwe Bux- Hide quoted text -

- Show quoted text -
oh! That was a mistake. if i had to read a file line by line and
output it how would i do it . if i get while(getline(cin,s)) cout
<<s<<endl;
outputs newline for everyline including the lastline. the lastline
needn't have a newline but otherlines needs to be output with '\n'.
How do i achieve it ?
Nov 9 '08 #5
Terry IT wrote:
hi,
i'm using code like this

string s
while(getline(cin,s)){
process(s);
}
// this is the last line
process(s);

process does some replacement and rescanning. The problem is i can't
strip or add newlines. So i don't know whether the lastline contains a
'\n' or it was just EOF.

I can't read the whole buffer as it is too huge and some line doesn't
need to be replaced.

Using while(fgets(str,MAX,stdin){
s=str
}
works but again this conversion of str to s is an overhead.

Can you suggest on how to overcome on this getline issue ?
Besides what the others pointed out:
'std::getline()' reads until the next newline (or whatever
character you passed as the optional third parameter) or
until it encounters EOF. In the latter case, IMO 'cin.eof()'
should be true.
Would that help?

Schobi
Nov 9 '08 #6
On Nov 9, 5:17*am, Terry IT <tryi...@gmail.comwrote:
*i'm using code like this
string s
*while(getline(cin,s)){
* * process(s);
* }
// this is the last line
* process(s);
Which was already processed in the loop.
process does some replacement and rescanning. The problem is i
can't strip or add newlines. So i don't know whether the
lastline contains a '\n' or it was just EOF.
If it doesn't end with a '\n', then it's not a text file:-).

Seriously, if you have opened the file in text mode, there is no
such thing as an incomplete line; it really depends on how your
implementation treats it.
I can't read the whole buffer as it is too huge and some line
doesn't need to be replaced.
Using while(fgets(str,MAX,stdin){
* * * * *s=str}
works but again this conversion of str to s is an overhead.
Can you suggest on how to overcome on this getline issue ?
Drop the call to process outside of the loop, and it should
work. (Supposing your implementation accepts unterminated last
lines in a text file, of course.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 9 '08 #7
Terry IT <tr*****@gmail.comwrote:
if i had to read a file line by line and
output it how would i do it . if i get while(getline(cin,s)) cout
<<s<<endl;
outputs newline for everyline including the lastline. the lastline
needn't have a newline but otherlines needs to be output with '\n'.
How do i achieve it ?
You have to treat either the first time getline is called special, or
the last time. It is much easier to detect which call to getline is the
first one, than which call is the last one, so treat the first one
special instead:

string s;
getline(cin, s);
cout <<< s;
while (getline(cin, s))
cout << '\n' << s;
Nov 9 '08 #8
Terry IT <tr*****@gmail.comkirjutas:
oh! That was a mistake. if i had to read a file line by line and
output it how would i do it . if i get while(getline(cin,s)) cout
<<s<<endl;
outputs newline for everyline including the lastline. the lastline
needn't have a newline but otherlines needs to be output with '\n'.
How do i achieve it ?
Why do you want to avoid newline after the last line? In Unix world there
is a long tradition of ending all non-empty text files with a newline. That
way you you don't get nasty surprises when you concatenate them together,
or #include them in a C/C++ file.

Paavo


Nov 9 '08 #9
On Nov 9, 9:31 am, Terry IT <tryi...@gmail.comwrote:
On Nov 9, 11:33 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
Terry IT wrote:
On Nov 9, 9:19 am, red floyd <no.spam.h...@example.comwrote:
>Terry IT wrote:
hi,
i'm using code like this
string s
while(getline(cin,s)){
process(s);
}
// this is the last line
process(s);
>This is wrong. s will not have new data after the loop.
I thought if file contains no newline ,then s contains all
the chars until the end of stream.
The point is not what s contains. The point is that you are
processing the last line twice. That is probably not what
you want.
oh! That was a mistake. if i had to read a file line by line and
output it how would i do it . if i get while(getline(cin,s)) cout
<<s<<endl;
outputs newline for everyline including the lastline. the
lastline needn't have a newline but otherlines needs to be
output with '\n'. How do i achieve it ?
I'm not sure what your motivation is. As I mentioned elsewhere,
it's implementation defined whether you can even write a text
file without a final newline; on most systems I've seen, you
can't. (Actually, Unix and Windows are probably about the only
ones where you can. And it doesn't have any real meaning, and
will cause all sorts of problems for other programs, under
Unix.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 10 '08 #10
On Nov 9, 3:35 pm, r...@zedat.fu-berlin.de (Stefan Ram) wrote:
Terry IT <tryi...@gmail.comwrites:
[...]
It might even be so that your implementation requires such a
new-line character, in which case the source text stream
would be ill-formed. (So it would not be the fault of the
code, but the duty of the producer of the stream to
terminate the last line.)
I agree, but you can't always change the producer. In such
cases, the best you can do is try to understand the file anyway
(it isn't guaranteed that you'll even see the partial line at
the end), a not perpetuate the error.

Note that in a text file, what you read and write doesn't
necessarily correspond exactly to what is on disk. If a system
adopts the convention that 0x0A is a line separator, and not a
line terminator, then the file on disk may very well not have a
trailing 0x0A---in fact, it shouldn't, since this would imply an
additional empty line. In that case, however, you still have to
write the final '\n' to the stream; it is the library code which
ensures the mapping between the internal representation of line
('\n' terminated) and the external representation (either
terminated or separated by some special character or character
sequence, or represented somehow in the organization of the file
itself).

And of course, one widespread problem is that different programs
under Windows use different conventions, so the C++ library
can't really know what to do.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 10 '08 #11

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

Similar topics

1
by: Jim Phelps | last post by:
Hello all, I am in a bit of a pickle using the getline function with an ifstream. It does not seem to work as advertised. Here is my scenario. In a nutshell, my code needs to pick up a fixed...
6
by: Fred H | last post by:
Hi I'm trying to write a more or less fool proof method of getting lines of text from either standard input (cin) or files (ifstream). It needs to be fool proof for later use, since I can be...
6
by: Simon Gibson | last post by:
Hi there, im trying to write a program where you can write reports and save them into an array. im having problems with getting the string into an array tho it seems to be skipping over the...
15
by: Christopher Benson-Manica | last post by:
The dumb-o-meter's pegging out today... What, if anything, is wrong with the following code? std::ifstream f( "myfile.txt" ); if( !f ) { cerr << "Couldn't open file\n"; } while( getline(f,s)...
14
by: KL | last post by:
I am so lost. I am in a college course for C++, and first off let me state I am not asking for anyone to do my assignment, just clarification on what I seem to not be able to comprehend. I have a...
22
by: bitshadow | last post by:
using the following code, i was able to have my compiler seg fault on me when i gave the argument as anythng greater than 20,832,000bytes. In the case of the struct its 868 instances of said...
11
by: Markus | last post by:
Hi, I want to get an integer from the user. If the user inserts a character instead of an int the program goes crazy. So I tried something like this: This program works pretty well. But if...
9
by: Michele 'xjp' | last post by:
Hi there. I have some problems with cin. Here's the code: http://rafb.net/p/GhK3AU65.html If you press '6', and 'enter', it will have to ask for another insert of a string. However, in this...
6
by: JML | last post by:
Hi, I have some code which parses a text file and creates objects based on what is in the text file. The code works just fine on Windows, but when I compile it using XCode on OS X the parsing...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.