【问题标题】:I'm trying using c++ to write somthing on a file and then read the data into a vector, but the program did not write a thing and I dont know why我正在尝试使用 c++ 在文件上写一些东西,然后将数据读入一个向量,但是程序没有写任何东西,我不知道为什么
【发布时间】:2022-06-14 19:49:49
【问题描述】:

我正在开发一个游戏,我正在尝试编写一个高分表,它将文件中的名称和数字读取到一个向量> 并使用 sfml 将其打印在屏幕上。 由于某种原因,我的程序没有在我的文件上写任何东西,我不知道为什么。 我做了一个应该工作的小例子,所以你可以看到问题。 如有任何帮助,我将不胜感激,谢谢。

这里是处理文件写入和读取的主要函数:

`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 是一个错字。

标签: c++ file


猜你喜欢
  • 2021-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-25
  • 2012-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多