473,387 Members | 1,326 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,387 software developers and data experts.

File and string question

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
1 1320


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
1
by: Marc Cromme | last post by:
I would like to ask a question about (good ?) style and possibilities in mixing C FILE* and C++ file streams. The background is that I want to use the C libpng library from within C++, but I...
13
by: Leo | last post by:
Hi I had asekd previously a question qith regards to running a DLL file from ASP. It was not running for me. Bo error returned, nothing, Just an empty string (when it should have not been...
11
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
12
by: dbuchanan | last post by:
Hello, (Is this the proper newsgroup?) === Background === I am building a solution with two projects. One project is my data access layer which contains my DataSet as an xsd file. The XSD...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.