【问题标题】:File Storage and Retrieval文件存储和检索
【发布时间】:2013-06-10 11:44:12
【问题描述】:

我是一名爱好编程的高中生。我制作免费的东西,我正在使用 opengl 开发游戏。我需要保存和加载数据,但遇到困难时,我做了以下测试来测试我的方法。

保存文件“shiptest”是正确的,但是当我打开使用“shiptest”中的保存数据创建的第二个文件“shipout”时,只有第一行存在。起初我认为我的数组没有加载任何新数据,并且 clear 函数没有摆脱第一个元素。我通过在保存数据后覆盖这些行并观察保存的行毕竟被加载来纠正这个假设。我的新假设是 getline 函数每次调用时只获取第一行;但我不知道如何解决这个问题。

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>

unsigned short int shipPart;

float editShip[256][3];//part ID, x relative, y relative, r,g,b
float activeShip[256][3];

void CLEAR(bool edit)
{
    for (int n = 0; n < 256; n++)
    {
        if (edit)
            editShip[n][0] = -1;
        else
            activeShip[n][0] = -1;
    }
}

void saveEdit(std::string name)
{
    std::ofstream out;
    out.open ("ship" + name + ".txt", std::ofstream::out);

    for (int n = 0; n < 256; n++)
    {
        for (int i = 0; i < 3; i++)
        {
            if (editShip[n][0] == -1)
                break;
            out << editShip[n][i] << " ";
        }
        out << "\n";
    }

    out.close();
}

void load(std::string name, bool edit)
{
    CLEAR(edit);
    std::ifstream in;
    in.open ("ship" + name + ".txt", std::ifstream::in);
    std::string line, buf;
    std::stringstream ss;
    int i;
    for (int n = 0; n < 3; n++)
    {
        getline(in, line);
        ss << line;
        i=0;
        while (ss >> buf)
        {
            if (edit)
                editShip[n][i] = atof(buf.c_str());
            else
                activeShip[n][i] = atof(buf.c_str());
            i++;
        }
    }
    in.close();
}

int main()
{
    for (int n = 0; n < 256; n++)
    {
        editShip[n][0] = -1;
        activeShip[n][0] = -1;
    }

    editShip[0][0] = 5;
    editShip[0][1] = .11;
    editShip[0][2] = .22;
    editShip[1][0] = 4;
    editShip[1][1] = .33;
    editShip[1][2] = .44;
    editShip[2][0] = 3;
    editShip[2][1] = .55;
    editShip[2][2] = .66;

    saveEdit("test");

    editShip[0][0] = 5000;
    editShip[0][1] = 8978;
    editShip[0][2] = 8888;

    load("test",1);

    saveEdit("out");

    std::cout << "Hello world!" << std::endl;
    return 0;
}

【问题讨论】:

    标签: c++ arrays istream ostream


    【解决方案1】:

    在load() 中,您不断在stringstream ss 中添加更多行,但它的 eof 标志可能在上一次循环中保持设置,因此即使要从中读取更多内容,eof 也已设置,因此它不会继续通过operator&gt;&gt;() 提供数据。如果您只是在for() 循环的顶部调用ss.clear(),您将在每个循环上从一个空的stringstream 开始,我想您会得到您想要的。

    【讨论】:

      【解决方案2】:

      在您的load() 函数中:

      for (int n = 0; n < 3; n++)
      {
          ss.clear(); //< Clear ss here before you use it!
          getline(in, line);
          ss << line;
          i=0;
          while (ss >> buf)
          {
              if (edit)
                  editShip[n][i] = atof(buf.c_str());
              else
                  activeShip[n][i] = atof(buf.c_str());
              i++;
          }
      }
      

      Getline() 工作正常。只需在使用之前清除 stringstream,就可以了。在我的计算机上运行此代码,它可以按需要运行。

      编辑:确认!刚刚看到那个电话标记器在我回答时说了同样的话。他应该得到 +1 而不是我。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-15
        相关资源
        最近更新 更多