EASYX中把数据写入到本地Txt
时间: 2023-07-26 16:03:37 浏览: 110
要将数据写入到本地 txt 文件中,可以使用 C++ 标准库中的 `ofstream` 类。EasyX 中也可以使用此类来实现将数据写入本地 txt 文件。
以下是一个示例代码,实现将数据写入到本地文件 `output.txt` 中:
```c++
#include <graphics.h>
#include <fstream>
int main()
{
// 创建绘图窗口
initgraph(640, 480);
// 打开 output.txt 文件,如果不存在则创建新的文件
std::ofstream outfile("output.txt");
// 写入数据到文件中
outfile << "Hello, world!" << std::endl;
outfile << "The answer is " << 42 << std::endl;
// 关闭文件
outfile.close();
// 关闭绘图窗口
closegraph();
return 0;
}
```
在以上代码中,我们首先打开了一个名为 `output.txt` 的文件,然后向其中写入了两行数据,最后关闭了文件。在实际使用时,可以根据需要修改文件名和要写入的数据。
相关问题
EASYX中把数据保存到本地Txt
要将数据保存到本地 txt 文件中,可以使用 C++ 标准库中的 `ofstream` 类。EasyX 中也可以使用此类来实现将数据保存到本地 txt 文件。
以下是一个示例代码,实现将数据保存到本地文件 `output.txt` 中:
```c++
#include <graphics.h>
#include <fstream>
int main()
{
// 创建绘图窗口
initgraph(640, 480);
// 获取需要保存的数据
int data = 42;
// 打开 output.txt 文件,如果不存在则创建新的文件
std::ofstream outfile("output.txt");
// 将数据保存到文件中
outfile << data;
// 关闭文件
outfile.close();
// 关闭绘图窗口
closegraph();
return 0;
}
```
在以上代码中,我们首先获取需要保存的数据,然后打开了一个名为 `output.txt` 的文件,将数据写入到文件中,最后关闭了文件。在实际使用时,可以根据需要修改文件名和要保存的数据。
easyx从txt文件读取数据
可以通过以下代码,使用EasyX从txt文件中读取数据:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> data;
string line;
ifstream file("file.txt");
if (file.is_open())
{
while (getline(file, line))
{
data.push_back(line);
}
file.close();
}
else
{
cout << "Unable to open file" << endl;
return 0;
}
// 使用读取到的数据进行操作
return 0;
}
```
记得把file.txt替换为你要读取的txt文件的路径。