在本文中,您将学习如何使用c ++编写文本文件。
首先,创建一个新项目(我建议使用控制台应用程序)
删除其中的所有内容,我们将从头开始; 这将帮助您学习。
通过将其放在前三行来开始代码:
#include <iostream>
#include <fstream>
using namespace std;
现在开始代码:
1->首先输入您的主要功能:
int main()
{
}
您将在两个括号之间编写代码。
2->创建,打开并命名您的文本文件:
ofstream myfile;
myfile.open ("Test.txt");
测试是我今天要命名的。
3->在您的文本文件中写入:
myfile << "Hello.\n";
在文件中,我告诉它写“这是一个测试”。
4->关闭文件并结束我们的功能:
myfile.close();
return 0;
您的代码应如下所示:
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
ofstream myfile;
myfile.open ("test.txt");
myfile << "Hello.\n";
myfile.close();
return 0;
}
编译程序,然后单击.exe,它将在该目录中创建一个文本文件。
将文本文件写入目录的方法有很多,这只是一种。
如果您有任何问题或想知道什么,请随时收件箱。
From: https://bytes.com/topic/c/insights/927036-how-write-text-file-c