473,588 Members | 2,554 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getline problems

hi,
i'm using code like this

string s
while(getline(c in,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 5181
Terry IT wrote:
hi,
i'm using code like this

string s
while(getline(c in,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...@e xample.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...@e xample.comwrote :
>Terry IT wrote:
hi,
i'm using code like this
string s
while(getline(c in,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...@e xample.comwrote :
Terry IT wrote:
hi,
i'm using code like this
string s
while(getline(c in,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(c in,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(c in,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 objektorientier ter Datenverarbeitu ng
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(c in,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(c in,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...@e xample.comwrote :
>Terry IT wrote:
hi,
i'm using code like this
string s
while(getline(c in,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(c in,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 objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 10 '08 #10

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

Similar topics

1
2359
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 record length flat file that is generated by an old IBM mainframe. These data fields need to be read in by my program EXACTLY as they are represented in the file. Here is an example text file that I used in my test. ...
6
1665
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 quite the fool ;-) For both cin and ifsteram the method getline is defined, and I kind of like it, since I can
6
7142
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 cin.getline function entiryly and going back upto the start of the menu. the problem seems to lie with '\n' bit if i change that to say 'a' it will run fine but accept a as ending the input. any help getting the '\n' working is appreciated!! heres the...
15
2817
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) ) { cout << s << '\n'; }
14
3869
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 ..txt file that I want to read into a multi-dimensional string array. Each line of the file needs to be read into the array. OK..sounds easy enough, but I can't get the getline(file_name array_name) to work. So...I am thinking it is definitely...
22
3036
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 structure. The compiler obviously allows VLA however it craps out after the above amount of bytes. I was told i was attempting to put everythng on the stack and not the heap. So i was wondering if anyone can maybe clear it up, is that true? would i...
11
3260
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 I put this code in my main program the b-array does not get a value. When I change my code the array does not get a value.
9
1990
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 case, it goes straight without waiting for input... any ideas?
6
6748
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 goes all wrong. Is there some known differences with file handling on OS X? My code is quite long, but one of the defect parts looks like this (sorry about the indentation - I'm new to posting code on a newsgroup):
0
7927
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
8352
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...
0
6632
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...
1
5723
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
5396
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
3846
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
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1457
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1194
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.