编写一个文本文件写入程序,打开一个
test.txt
文件,向文件中写入以下三个字符串,
每个字符串占一行。
Name: Zhang San
Sex: Male
Age: 20
编译运行以后,检查
test.txt
文件中是否正确写入
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream file("text.txt",ios::out);
if(!file.fail())
{
file<<"Name: Zhang San"<<endl;
file<<"Sex: Male"<<endl;
file<<"Age: 20"<<endl;
}
else
cout<<"can not open"<<endl;
file.close();
return 0;
}