篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp FileIO C ++相关的知识,希望对你有一定的参考价值。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
//create an output stream to write to the file
//append the new lines to the end of the file
ofstream myfileI ("input.txt", ios::app);
if (myfileI.is_open())
{
myfileI << "\nI am adding a line.\n";
myfileI << "I am adding another line.\n";
myfileI.close();
}
else cout << "Unable to open file for writing";
//create an input stream to write to the file
ifstream myfileO ("input.txt");
if (myfileO.is_open())
{
while ( getline (myfileO,line) )
{
cout << line << '\n';
}
myfileO.close();
}
else cout << "Unable to open file for reading";
return 0;
}