C++ provides the following classes to perform output and input of characters to/from files:
ofstream
: Stream class to write on filesifstream
: Stream class to read from filesfstream
: Stream class to both read and write from/to files.
Writing text into a file
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream file;
file.open("some_file.txt");
file << "This is a new line \n";
file.close();
return 0;
}
Reading from a file
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string line;
ifstream file("some_file.txt");
if(file.is_open())
{
while(getline(file,line))
{
cout << line << endl;
}
file.close();
}
else cout << "Unable to open file" << endl;
return 0;
}
Copying a file
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream infile("some_file.txt");
ofstream outfile("copied_file.txt");
outfile << infile.rdbuf();
infile.close();
outfile.close();
return 0;
}
The C++ fstreams are buffered internally. They use an efficient buffer size. rdbuf()
just returns a pointer to the buffer, so just copy one stream buffer to a stream and the internal magic will do an efficient copy of one stream to the other.