473,385 Members | 1,356 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,385 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 5168
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.