[Reserved] using ifstream and getline to read the file contents-c – turn
[Everyone gasps.]
using namespace std;
{
/ / output a blank line
}
ifstream fin (” data.txt “);
{
cout < <"Read from file:" < Data read test.
}
void ReadDataFromFileLBLIntoString ()
{
Read from file: not
Fry: One Jillion dollars.
{
opening dataFUNNY.txt for input
ReadDataFromFileLBLIntoCharArray (); / / read word-by-character array
Read from file: Auctioneer: Sir, that not a number.
OutPutAnEmptyLine (); / / output a blank line
}
Read from file: Sir,
ReadDataFromFileWBW (); / / read word by word strings
{
/ / Simply evaluating an I / O object in a boolean context will return false
}
ReadDataFromFileLBLIntoString (); / / read word by word strings
{
Read from file: data read test.
Read from file: test
Fry:
< img ALT = "" ALIGN = "top" src = "http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src = "http://www.cnblogs.com/Images/OutliningIndicators/ None.gif "TITLE =" [Reserved] using ifstream and getline reads the contents of the file-c - switch "/> / / if any errors have occurred
Read from file: gasps.]
Read from file:.
while (fin>> s)
string filename =” dataFUNNY.txt “;
string s;
cout <<"Read from file:" < Read from file: a
if (! fin)
return 0;
while (fin.getline (str, LINE_LENGTH))
ReadDataWithErrChecking (); / / read with detection
Read from file: Jillion
Read from file: Auctioneer: Sir, that not a number.
/ / If you want to avoid reading into character arrays,
data.txt file,
/ / we could read the file in L ine- B y- L ine using the I / O getline () function.
original address: Use ifstream and getline reads the contents of the file-c - turn Author: walk through the clouds
void ReadDataFromFileLBLIntoCharArray ()
}
void OutPutAnEmptyLine ()
/ / read mode: reads word for word, the word with a space between the distinction
/ / you can use the C string getline () function to read lines into strings
Read from file:
exit (-1);
}
/ / If we were interested in preserving whitespace,
ifstream fin (" data.txt ");
Read from file: Fry: One Jillion
{
ifstream fin (" data.txt ");
< br /> dollars.
/ / read data from the file, W ord B y W ord
/ / Read mode: read line by line, the line read into the string, the line of distinguish between the use of carriage return line feed
Read from file: Fry: One Jillion
ifstream fin (filename.c_str ());
Read from file: One
Read from file: [Everyone gasps.]
{
OutPutAnEmptyLine (); / / output a blank line
output is:
Read from file: Auctioneer:
}
Suppose there is a call
{< br /> / / read with error detection method
while ( getline (fin, s))
string s;
}
< img ALT = "" ALIGN = "top" src = "http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src = "http://www.cnblogs.com/Images/OutliningIndicators/ None.gif "TITLE =" [Reserved] using ifstream and getline reads the contents of the file-c - switch "/> void ReadDataWithErrChecking ()
Error
Auctioneer: Sir, that not a number.
Read from file: [Everyone gasps.]
Press any key to continue
dollars.
cout <<"Error opening" << filename <<"for input" < # include
Read from file: dollars.
# include
int main () < br />
{
}
Read from file: data read,
cout <<"Read from file:" < const int LINE_LENGTH = 100;
It contains the following:
Read from file: that
Read from file: [Everyone < br /> Read from file: number.
void ReadDataFromFileWBW ()
# include
Read from file: data read test.
/ / but all of the whitespace that separated words (including newlines) was lost.
char str [LINE_LENGTH];
/ / when used in this manner, we l get space-delimited bits of text from the file
} < br /> / / Read mode: read line by line, the line read into the character array, the line between with a carriage return line feed distinguish
OutPutAnEmptyLine (); / / output a blank line
cout <<” n “;
