编写一个文本文件写入程序,打开一个 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;
}
本文介绍了如何使用C++的fstream库,通过main()函数实现向test.txt文件中写入Name:ZhangSan, Sex:Male, Age:20的内容。程序确保了文件的正确打开和写入操作。
435

被折叠的 条评论
为什么被折叠?



