Suppose there isData.txtFile, which contains the following content:
Fry: One Jillion dollars.
[Everyone gasps.]
Auctioneer: Sir, that's not a number.
Read and test data.
The following is the data read operation based on data.txt:
# Include <iostream>
# Include <fstream>
# Include <string>
Using namespace std;
// Empty output line
Void OutPutAnEmptyLine ()
{
Cout <"\ n ";
}
// Read method: Read by word, separated by Spaces
// Read data from the file,
WOrd
BY
WOrd
// When used in this manner, we'll get space-delimited bits of text from the file
// But all of the whitespace that separated words (including newlines) was lost.
Void ReadDataFromFileWBW ()
{
Ifstream fin ("data.txt ");
String s;
While (fin> s)
{
Cout <"Read from file:" <s <endl;
}
}
// Read method: Read rows by row, read rows into character arrays, and use carriage return to line feed to distinguish between rows
// If we were interested in preserving whitespace,
// We cocould read the file in
LIne-
BY-
LIne using the I/O getline () function.
Void ReadDataFromFileLBLIntoCharArray ()
{
Ifstream fin ("data.txt ");
Const int LINE_LENGTH = 100;
Char str [LINE_LENGTH];
While (fin. getline (str, LINE_LENGTH ))
{
Cout <"Read from file:" <str <endl;
}
}
// Read method: Read rows by line, read rows into strings, and use carriage return to line feed to distinguish between rows
// If you want to avoid reading into character arrays,
// You can use the C ++ string getline () function to read lines into strings
Void ReadDataFromFileLBLIntoString ()
{
Ifstream fin ("data.txt ");
String s;
While (getline (fin, s ))
{
Cout <"Read from file:" <s <endl;
}
}
// Read method with Error Detection
// Simply evaluating an I/O object in a boolean context will return false
// If any errors have occurred
Void ReadDataWithErrChecking ()
{
String filename = "dataFUNNY.txt ";
Ifstream fin (filename. c_str ());
If (! Fin)
{
Cout <"Error opening" <filename <"for input" <endl;
Exit (-1 );
}
}
Int main ()
{
ReadDataFromFileWBW (); // read strings one by one
OutPutAnEmptyLine (); // empty output line
ReadDataFromFileLBLIntoCharArray (); // read the character array one by one
OutPutAnEmptyLine (); // empty output line
ReadDataFromFileLBLIntoString (); // read strings one by one
OutPutAnEmptyLine (); // empty output line
ReadDataWithErrChecking (); // read with detection
Return 0;
}
Output result:
Read from file: Fry:
Read from file: One
Read from file: Jillion
Read from file: dollars.
Read from file: [Everyone
Read from file: gasps.]
Read from file: Auctioneer:
Read from file: Sir,
Read from file: that's
Read from file: not
Read from file:
Read from file: number.
Read from file: reads data,
Read from file: Test
Read from file :.
Read from file: Fry: One Jillion dollars.
Read from file: [Everyone gasps.]
Read from file: Auctioneer: Sir, that's not a number.
Read from file: Read data, test.
Read from file: Fry: One Jillion dollars.
Read from file: [Everyone gasps.]
Read from file: Auctioneer: Sir, that's not a number.
Read from file: Read data, test.
Error opening dataFUNNY.txt for input
Press any key to continue
From: http://www.cnblogs.com/JCSU/articles/1190685.html