Suppose there is a call
Data.txtFile that contains the following content:
Fry:one jillion dollars.
[Everyone gasps.]
Auctioneer:sir, that's not a number.
Data read, test.
The following is a data.txt-based data read operation:
#include <iostream>
#include <fstream>
#include <string>
using namespace Std;
Output blank Line
void Outputanemptyline ()
{
cout<< "\ n";
}
READ: read by word, between words 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, separated words (including newlines) was lost.
void Readdatafromfilewbw ()
{
Ifstream fin ("data.txt");
string S;
While (Fin >> s)
{
cout << "Read from File:" << s << endl;
}
}
Read by: Row by line read, the line is read into the character array, line with carriage return to differentiate between lines
If we were interested in preserving whitespace,
We could 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 by: Read line by row, read the line into the string, between the lines with carriage return line to differentiate
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 mode with error detection
Simply evaluating an I/O object in a Boolean context would return false
If any errors has 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 word-by-phrase into a string
Outputanemptyline (); Output blank Line
Readdatafromfilelblintochararray (); Read a word into a character array
Outputanemptyline (); Output blank Line
Readdatafromfilelblintostring (); Read word-by-phrase into a string
Outputanemptyline (); Output blank Line
Readdatawitherrchecking (); Read with detection
return 0;
}
The output is:
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:a
Read from File:number.
Read from File: Data read,
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: Data read, 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: Data read, test.
Error opening dataFUNNY.txt for input
Press any key to continuehttp://www.cnblogs.com/jcsu/articles/1190685.html
Read file contents using Ifstream and Getline [C + +] ZZ