By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,917 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,917 IT Pros & Developers. It's quick & easy.

File and string question

P: n/a
Hi:

Assume that the content of the file textfile.txt is:

Have a good day.
Hello world.

I use the following code to access the file textfile.txt:

#include <iostream>
#include <fstream>
#include <string>

int main(){

ifstream infile ("textfile.txt", ios::in)

string s1, s2, s3, s4;
string s;

infile>>s1>>s2>>s3>>s4;//LINE0
getline(infile,s); //LINE1

cout<<"s1:"<<s1<<" s2:"<<s2<<" s3:"<<s3<<" s4:"<<s4<<endl;//LINE2
cout<<"s:"<<s<<endl; //LINE3

}

The output of LINE2 is correct. But the output of LINE3 is a blank
line, not "Hello world." Why?

My thought is that at the end of the string "Have a good day.", there
is a newline character, so LINE1 read the newline character, other than
the string
"Hello world.". Am I right?

If I still want to use LINE0, how to prevent the above problem?

Another question is about the eof() function.
When I use the following code to read a file:

while(!infile.eof()){
getline(infile,s);
cout<<s<<endl;//LINE4
}

I found that after the content of the file is printed, LINE4 always
outputs extra blank lines. It seems to me that the eof() function can
not find the correct end of a file. Why?

Thanks a lot.

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a


Jack schrieb:
Hi:

Assume that the content of the file textfile.txt is:

Have a good day.
Hello world.

I use the following code to access the file textfile.txt:

#include <iostream>
#include <fstream>
#include <string>

int main(){

ifstream infile ("textfile.txt", ios::in)

string s1, s2, s3, s4;
string s;

infile>>s1>>s2>>s3>>s4;//LINE0
getline(infile,s); //LINE1

cout<<"s1:"<<s1<<" s2:"<<s2<<" s3:"<<s3<<" s4:"<<s4<<endl;//LINE2
cout<<"s:"<<s<<endl; //LINE3

}

The output of LINE2 is correct. But the output of LINE3 is a blank
line, not "Hello world." Why?

My thought is that at the end of the string "Have a good day.", there
is a newline character, so LINE1 read the newline character, other than
the string
"Hello world.". Am I right?
Yes

If I still want to use LINE0, how to prevent the above problem?
Use getline(...) to read both lines, or
read one character after LINE0 and discard it if it is '\n'.

Another question is about the eof() function.
When I use the following code to read a file:

while(!infile.eof()){
getline(infile,s);
cout<<s<<endl;//LINE4
}

I found that after the content of the file is printed, LINE4 always
outputs extra blank lines. It seems to me that the eof() function can
not find the correct end of a file. Why?
eof() does not check for the file end in advance,
the bit is set after reading input from a stream failed.

while (infile) {
getline(infile, s);

if (infile) {
// use `s'
}
else {
// error handling
}
}

Thanks a lot.


regards
Stephan Brönnimann
br****@osb-systems.com
Open source rating and billing engine for communication networks.

Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.