如何使用 C++ 将测验分数存储在 TXT 文件中

how to store a quiz score in a txt file using c++

本文关键字:存储 TXT 文件 C++ 何使用      更新时间:2023-10-16

我是编程新手,我想解决的项目之一是测验,问题是我不知道如何将我的测验分数存储在文本文件中,有人可以帮忙吗?一个给定的例子会很好

这是 cplusplus 中给出的示例的略微修改的示例

// basic file operations
#include <iostream>
#include <fstream>
int main () {
std::ofstream myfile("example.txt");
if(myfile.is_open())
{
myfile << "Writing this to a file.n";
myfile << 1 << 2 << 3;
}
else 
{
std::cout << "error opening file" << std::endl;
}
return 0;
}

这会在您的工作目录中创建example.txt(如果它尚不存在(,打开它,流式传输到它并在文件被销毁时关闭myfile。现在该文件包含以下文本:

Writing this to a file.
123