Read file contents using Ifstream and Getline [C + +]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>
UsingNamespaceStd
//Output blank Line
voidOutputanemptyline ()
{
cout<<"\ n";
}
//Read mode:
Word-wise reading,
distinguish between words with 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
//ButAll of the
whitespaceThat separated words (including newlines)Was
lost .
voidREADDATAFROMFILEWBW ()
{
Ifstream Fin ("Data.txt");
StringS
While(Fin>>s)
{
cout<<"Read from File:"<<S<<Endl
}
}
//Read mode:Read
line by line, reading rows into a character array, lines are differentiated between rows by carriage return
//If we were interested in preserving whitespace,
//We could read the file in
Line-
BY-
LINE using the I/O getline () function.
voidReaddatafromfilelblintochararray ()
{
Ifstream Fin ("Data.txt");
ConstIntLine_length= 100;
CharStr[line_length];
While(Fin.getline (Str,line_length))
{
cout<<"Read from File:"<<Str<<Endl
}
}
//Read mode:Read
line by line, reading rows into a string, lines are differentiated between rows by carriage return
//If you want to avoid reading into character arrays,
//You can use the C + + string Getline () function to read lines into strings
voidReaddatafromfilelblintostring ()
{
Ifstream Fin ("Data.txt");
StringS
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
voidReaddatawitherrchecking ()
{
StringFileName="Datafunny. txt";
Ifstream Fin (FILENAME.C_STR ());
If(!Fin)
{
cout<<"Error Opening"<<FileName<<"For input"<<Endl
Exit-1);
}
}
int Main ()
{READDATAFROMFILEWBW ();//words read into string outputanemptyline ();//output blank line Readdatafromfilelblintochararray ();//word-by-phrase read-in character count Group Outputanemptyline (); Output blank line readdatafromfilelblintostring (); Reads the word into the string outputanemptyline (); Output blank line readdatawitherrchecking (); Read return 0 with detection;}
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 continue
Read file contents using Ifstream and Getline [C + +]