如何正确地写向量到二进制文件在c ++? [英] how to correctly write vector to binary file in c++?

查看:136
本文介绍了如何正确地写向量到二进制文件在c ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢Mats Petersson的解释复制向量到数组它似乎工作。这里的代码snipset:

thanks to Mats Petersson for the explanation to copy vector to array it's seems work. here the code snipset:

#include <iostream>
#include <string.h>
#include <vector>
#include <fstream>

using namespace std;

class Student
  {
    private:
    char m_name[30];
    int m_score;

    public:
    Student()
      {

      }
    Student(const Student& copy)
      {
           m_score = copy.m_score;   //wonder why i can use this statment as
           strncpy(m_name, copy.m_name, 30); //declare it private
      }
      Student(const char name[], const int &score)
      :m_score(score)
      {
           strncpy(m_name, name, 30);
      }
      void print() const
      {
           cout.setf(ios::left);
           cout.width(20);
           cout << m_name << " " << m_score << endl;
      }
      };


      int main()
      {
        vector<Student> student;
        student.push_back(Student("Alex",19));
        student.push_back(Student("Maria",20));
        student.push_back(Student("muhamed",20));
        student.push_back(Student("Jeniffer",20));
        student.push_back(Student("Alex",20));
        student.push_back(Student("Maria",21));
      {
      Student temp[student.size()];
      unsigned int counter;
      for(counter = 0; counter < student.size(); ++counter)
      {
        temp[counter] = student[counter];
      }

      ofstream fout("data.dat", ios::out | ios::binary);
      fout.write((char*) &temp, sizeof(temp));
      fout.close();
      }

      vector<Student> student2;
      ifstream fin("data.dat", ios::in | ios::binary);

      {
        fin.seekg(0, ifstream::end);
        int size = fin.tellg() / sizeof (Student);
        Student temp2[size];
        fin.seekg(0, ifstream::beg);
        fin.read((char*)&temp2, sizeof(temp2));
        int counter;
        for(counter = 0; counter <6; ++counter)
        {
        student2.push_back(temp2[counter]);
        }
        fin.close();
      }
      vector<Student>::iterator itr = student2.begin();
      while(itr != student2.end())
      {
        itr->print();
        ++itr;
      }
      return 0;
      }

但是我这个方法会浪费巨大的内存和繁琐。也许我会考虑写它先生与ocelot和其他建议。

but i guest this method will waste huge memory and cumbersome. maybe i will consider writing it Mr. with ocelot and other suggestion. thanks everyone for the answer.

推荐答案

您正在写文件向量结构,而不是其数据缓冲区。尝试改变写入程序:

You are writing to file the vector structure, not its data buffer. Try change writing procedure to:

 ofstream fout("data.dat", ios::out | ios::binary);
 fout.write((char*)&student[0], student.size() * sizeof(Student));
 fout.close();

而不是从文件大小计算向量的大小,它更好的写入向量大小之前。在这种情况下,您可以向同一个文件写入其他数据。

And instead of calculation size of vector from file size, it's better write vector size (number of objects) before. In the case you can write to the same file other data.

 size_t size = student.size();
 fout.write((char*)&size, sizeof(size));

这篇关于如何正确地写向量到二进制文件在c ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆