以app模式向文本文件末尾写入内容

#include <fstream>
#include <iostream>
#include <string>

int main()
{
	using namespace std;

	// We'll pass the ios:app flag to tell the ofstream to append
	// rather than rewrite the file.  We do not need to pass in ios::out
	// because ofstream defaults to ios::out
	ofstream outf("Sample.txt", ios::app);  //以app模式在文本文件末尾写入内容

	// If we couldn't open the output file stream for writing
	if (!outf)
	{
		// Print an error and exit
		cerr << "Uh oh, Sample.txt could not be opened for writing!" << endl;
		exit(1);
	}

	string s;  
	getline(cin,s);  
	while(s.size())  
	{  
		outf<<s<<endl;//将s写入文本文件  
		getline(cin,s);  
	}  

	return 0;

	// When outf goes out of scope, the ofstream
	// destructor will close the file
}

你可能感兴趣的:(ios,Stream,File,include,output,destructor)