【发布时间】:2022-06-14 19:49:49
【问题描述】:
我正在开发一个游戏,我正在尝试编写一个高分表,它将文件中的名称和数字读取到一个向量
这里是处理文件写入和读取的主要函数:
`void Controller::readTable()
{
std::fstream highrcordFile;
//open for write
highrcordFile.open(FILE_TABLE_NAME, std::ios::out);
if (!m_file.is_open())
std::cout << "problem" << std::endl;
int i = 123;
highrcordFile << "test" << std::endl;
highrcordFile << i << std::endl;
//open for read
highrcordFile.open(FILE_TABLE_NAME, std::ios::in);
if (!m_file.is_open())
std::cout << "problem" << std::endl;
//read high-records data from file to vector <pair>
highrcordFile.clear();
highrcordFile.seekg(0, std::ios::beg);
std::pair<std::string, int> p;
for (auto line = std::string(); std::getline(highrcordFile, line);)
{
p.first = line;
std::getline(highrcordFile, line);
p.second = std::stoi(line);
m_table.push_back(p);
}
}
【问题讨论】:
-
您知道
close()的作用以及它是如何工作的吗?你忘了这样做,VTC 是一个错字。