【问题标题】:How do you save a whole array to a disk in c++如何在 C++ 中将整个数组保存到磁盘
【发布时间】:2011-04-28 13:39:23
【问题描述】:

我一直在寻找,但除了二维数组中的一个坐标之外,我一直无法保存任何东西

【问题讨论】:

  • 如果说这是一个模糊的问题,那就太客气了。您使用的是什么类型的数组,C 数组、动态数组(如 std::vector)或其他?如果是动态数组,是否需要从保存的数据中查找大小,还是预先知道?这些数组中存储了什么,您可以将这些类型的对象保存到磁盘吗?

标签: c++ serialization


【解决方案1】:

这称为serialization。请查看this question,了解有关使用 Boost 实现此目的的更多信息。

【讨论】:

    【解决方案2】:

    基于StackedCrooked's idea,这里有一个解决方案,它允许您使用std::vector 的C 样式数组或任何其他元素已为其定义<< 的序列。

    #include <cstddef>
    #include <fstream>
    #include <iostream>
    
    
    // Beware, brain-compiled code ahead! 
    template<class InpIt>
    void save_seq(const std::ostream& os, InpIt begin, InpIt end)
    {
        if(begin != end)
            os << *begin++;
        while(begin != end)
            os << ' ' << *begin++;
    }
    
    template<class OutpIt>
    bool load_seq(const std::istream& is, OutpIt begin, std::size_t n)
    {
        for( std::size_t i=0; is && i<n; ++i)
            is >> *begin++
        return is.good() || is.eof();
    }
    template<class OutpIt>
    bool load_seq(const std::istream& is, OutpIt begin)
    {
        while(is.good())
            is >> *begin++
        return is.eof();
    }
    
    template<class T, std::size_t N>
    void save_array(const std::ostream& os, const T (&data)[N])
    {
        save_seq(os, data, data+N);
    }
    
    
    template<class T, std::size_t N>
    bool load_array(const std::istream& is, T (&data)[N])
    {
        return load_seq(is, data, N);
    }
    
    int main()
    {
        const std::size_t size = 5;
        int numbers[size];
        numbers[0] = 10;
        numbers[1] = 11;
        numbers[2] = 12;
        numbers[3] = 13;
        numbers[4] = 14;
        {
            std::oftsream ofs("array.txt");
            if(!ofs.good())
                return 1;
            save_array(ofs, numbers);
        }
        {
            std::iftsream ifs("array.txt");
            if(!ifs.good())
                return 2;
            int test[size];
            load_array(ifs, test);
            for (std::size_t idx = 0; idx < size; ++idx)
                std::cout << "test[" << idx << "]: " << test[idx] << std::endl;
        }
    
        std::vector<int> numbers2;
        numbers2.push_back(20);
        numbers2.push_back(21);
        numbers2.push_back(22);
        numbers2.push_back(23);
        {
            std::oftsream ofs("array.txt");
            if(!ofs.good())
                return 1;
            save_Seq(ofs, numbers2.begin(), numbers2.end());
        }
        {
            std::iftsream ifs("array.txt");
            if(!ifs.good())
                return 2;
            std::vector<int> test;
            load_seq(ifs, std::back_inserter(test));
            for (std::size_t idx = 0; idx < numbers2.size(); ++idx)
                std::cout << "test[" << idx << "]: " << test[idx] << std::endl;
        }
    
        return 0;
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用 std::fstream 或 boost::serialization。你的问题有点含糊,所以我不完全确定你想要什么,需要什么?

      【讨论】:

        【解决方案4】:

        如果数组包含 flat 数据(即不包含指向其他数据的指针的数据),您可以在一次写入文件中写入整个数组.

        如果不知道您的数据是什么样的,就不可能多说。

        【讨论】:

          【解决方案5】:

          经过一番修改,我想出了这个:

          #include <cstddef>
          #include <fstream>
          #include <iostream>
          
          
          template<class T>
          void SaveArray(const std::string & file, T * data, std::size_t length)
          {
              std::ofstream out(file.c_str());
              for (std::size_t idx = 0; idx < length; ++idx)
              {
                  if (idx != 0)
                  {
                      out << " ";
                  }
                  out << *data++;
              }
          }
          
          
          template<class T>
          std::size_t LoadArray(const std::string & file, T * data, std::size_t length)
          {
              std::ifstream in(file.c_str());
              std::size_t count = 0;
              while (count++ < length && in >> *data++);
              return count - 1; // return number of items
          }
          
          
          int main()
          {
              int numbers[5];
              numbers[0] = 10;
              numbers[1] = 11;
              numbers[2] = 12;
              numbers[3] = 13;
              numbers[4] = 14;
              SaveArray("array.txt", &numbers[0], 5);
          
              int test[5];
              LoadArray("array.txt", &test[0], 5);
          
              for (std::size_t idx = 0; idx < 5; ++idx)
              {
                  std::cout << "test[" << idx << "]: " << test[idx] << std::endl;
              }
              return 0;
          }
          

          欢迎提出改进建议。

          【讨论】:

            猜你喜欢
            • 2018-06-16
            • 2017-12-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-05-20
            • 2022-10-30
            相关资源
            最近更新 更多