The following are some examples of file read/write:
# Include <fstream>
STD: ifstream fin;
Fin. Open ("file name ");
While (! Fin. EOF ())
{
Char * pchar = new char [255];
Fin. Getline (pchar, 255); // each row is in pchar :)
..................
..................
}
# Include <iostream>
# Include <string>
# Include <vector>
# Include <fstream>
Using namespace STD;
Int file_to_vector (const string & filename, vector <string> & SVEC)
{
Ifstream infile (filename. c_str ());
If (! Infile. is_open ())
Return 1;
String S;
While (Getline (infile, S ))
SVEC. push_back (s );
Infile. Close ();
If (infile. EOF ())
Return 4;
If (infile. Bad ())
Return 2;
If (infile. Fail ())
Return 3;
}
Int main ()
{
Vector <string> SVEC;
String filename, S;
Cout <"Enter filename:" <Endl;
Cin> filename;
Switch (file_to_vector (filename, SVEC ))
{
Case 1: cout <"error! Can not open file: "<FILENAME <Endl; Return-1; break;
Case 2: cout <"error! Systerm failure "<Endl; Return-1;
Case 3: cout <"error! Read failure "<Endl; Return-1;
}
Cout <"vector:" <Endl;
For (vector <string >:: iterator iter = SVEC. Begin (); iter! = SVEC. End (); ++ ITER)
Cout <* ITER <Endl;
Return 0;
}
# Include <iostream>
# Include <fstream>
# Include <vector>
# Include <cstdlib>
Using namespace STD;
Int main ()
{
Vector <int> CH;
Vector <int>: iterator ITER;
Int ival;
Ifstream infile ("a.txt ");
If (! Infile. is_open ())
{
Cerr <"error! Can not open file! "<Endl;
Exit (exit_failure );
}
Else
{
While (infile> ival)
{
Ch. push_back (ival );
}
For (iter = CH. Begin (); iter! = CH. End (); ++ ITER)
Cout <* ITER <Endl;
}
Return 0;
}
Include <iostream>
# Include <vector>
# Include <iterator>
# Include <algorithm>
Using namespace STD;
Int
Main (void)
{
Vector <int> V;
Istream_iterator <int> in_iter (CIN), in_eof;
Ostream_iterator <int> out_iter (cout, "\ n ");
For (; in_iter! = In_eof; ++ in_iter)
V. push_back (* in_iter );
Copy (V. Begin (), V. End (), out_iter );
Return 0;
}
$ Cat rdint.txt
1 2 3 4 5
6 7 8 9 10
$ Cat rdint.txt |./rdint
1
2
3
4
5
6
7
8
9
10
If you want to directly read the file, modify it.
Istream_iterator <int> in_iter (CIN), in_eof;
Change
Ifstream fin ("rdint.txt ");
Istream_iterator <int> in_iter (FIN), in_eof;
Of course, the file must start:
# Include <fstream>
# Include <iostream>
# Include <fstream>
# Include <string>
Using namespace STD;
// Empty output line
Void outputanemptyline ()
{
Cout <"\ n ";
}
// read method: word-by-word reading, 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 ()
{< br> ifstream fin ("data.txt");
string s;
while (Fin> S)
{< br> cout <"read from file:" }< BR >}
// read method: Read rows by line. Read rows into character arrays and use carriage return to line feed 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 ()
{< br> ifstream fin ("data.txt");
const int line_length = 100;
char STR [line_length];
while (Fin. getline (STR, line_length)
{< br> cout <"read from file:" }< BR >}
// read method: Read rows by row, 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 ()
{< br> ifstream fin ("data.txt");
string s;
while (Getline (FIN, S ))
{< br> cout <"read from file:" }< BR >}
// 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;
}