【问题标题】:How to read from a file in a load/save function?如何在加载/保存功能中读取文件?
【发布时间】:2013-05-28 01:09:26
【问题描述】:

我知道标题有点混乱,但我不知道如何解释。

void save (POINT pt)
    {
    ofstream save;
    save.open("coords.txt", ios::trunc);

    if (save.is_open())
    {
        save << pt.x << endl;
        save << pt.y;

        save.close();

        system("CLS");

        cout << "Save successful\n\n\n\n";

        system("PAUSE");
    }

    else
    {
        system("CLS");

        cout << "Error: Could not save\n\n\n\n";

        system("PAUSE");
    }
    }

    int load ( )
    {
        ifstream load;
        load.open("coords.txt", ifstream::in);

        if (load.is_open())
        {

        }
    }

我想从加载函数内部的保存函数中读取POINT。我试过了

 if (load.is_open())
    {
        load >> pt.x;
        load >> pt.y;
    }

但是 pt.xpt.y 是未定义的。我不太擅长这个,但我正在努力理解它。

提前致谢!

【问题讨论】:

  • 我不确定你在问什么
  • 所以,我正在做的是一个自动点击器。我已经在程序中设置了坐标,但我添加了一个校准选项,用户可以在其中输入自己的坐标以适应不同的屏幕分辨率。我想将坐标保存到文件中,当他们重新打开程序时,我希望能够读取文件并将坐标用作默认值。
  • 您的问题似乎是您没有在加载函数中声明 POINT 结构。
  • 但是当我在加载函数中声明一个 POINT 结构时它失败了,因为它还没有被初始化。
  • 失败是什么意思?你看过我提供的答案了吗?

标签: c++ file function


【解决方案1】:

使用普通(标准)iostream 对象进行序列化/反序列化可能会带来很多痛苦。

我建议你使用一些更高级别的序列化库,如boost::archive::binary_oarchiveboost::archive::binary_iarchive (link to the doc)。

它非常易于使用,避免了很多问题,并让您有机会发现一个非常有用的 C++ 库。

更多信息请参见this post

【讨论】:

    【解决方案2】:

    试试这个:

    if (load.is_open())
    {
    while(load.good())    
    {
       getline(load,point);
       pt.x=line;
       getline(load,point);
       pt.y=line;
        }
    else{
     file open failed.
    }
    

    假设pt.xpt.ystring。如果它们是integer,那么您需要将point 转换为integer

    【讨论】:

      【解决方案3】:

      你不需要声明 pt 吗?还是会员?

      int load ( )
          {
              POINT pt;
              ifstream load;
              load.open("coords.txt", ifstream::in);
      
              if (load.is_open())
              {
                  load >> pt.x;
                  load >> pt.y;
              }
          }
      

      【讨论】:

        【解决方案4】:

        正如之前的海报所暗示的,普通的iostream 可能不是最佳选择...

        尽管在这种情况下,您的问题似乎是在 load() 函数的范围内缺少 POINT pt 的定义。

        也许创建一个负责设置的单例?

        class MySettings
        {
        private:
            POINT m_pt;
        public:
            static MySettings* instance()
            {
                 static MySettings settings;
                 return &settings;
            }
        
            int save(POINT pt) {...}
            POINT load() {...}
        }
        

        【讨论】:

          【解决方案5】:

          看来答案已经很明显了,你需要在load中添加一个POINT参数:

          /* The & symbol gives you back the points when the function has returned */
          /* Alternatively, more conventionally, you may use a pointer. */
          
          int load (POINT & pt){
              ifstream load;
          
              load.open("coords.txt", ifstream::in);
          
              if (!load.is_open()){
                  cerr << "File couldn't be opened" << endl;
                  return -1;
              }
          
              load >> pt.x;
              load >> pt.y;
          
              load.close();
          
              return 0;
          }
          

          此函数返回后,pt 参数包含您要查找的点。你可以这样称呼它:

          POINT points;
          load (points);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-09-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多