向文本文件中写入内容

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
	// ofstream is used for writing files
	// We'll make a file called Sample.txt
	ofstream outf("Sample.txt");//注意:此处的等价于:ofstream outf;outf.open("Sample.txt");

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

	// We'll write several lines into this file
	string s;
	getline(cin,s);
	while(s.size())
	{
		outf<<s<<endl;//将s写入文本文件
		getline(cin,s);
	}

	return 0;
}

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