简体   繁体   English

使用结构读取和写入二进制文件

[英]Reading and writing binary files using structures

I am attempting read from a binary file and dump the information into a structure.我正在尝试从二进制文件中读取并将信息转储到一个结构中。 Before I read from it I write into the file from a vector of structures.在我读取它之前,我从结构向量写入文件。 Unfortunately I am not able to get the new structure to receive the information from the file.不幸的是,我无法获得新结构以从文件中接收信息。

I have tried switching between vectors and individual structures.我试过在向量和单个结构之间切换。 Also tried messing with the file pointer, moving it back and forth and also leaving it as is to see if that has been the problem.还尝试弄乱文件指针,来回移动它并保持原样,看看这是否是问题所在。 Using vectors because it is supposed to take unlimited values.使用向量是因为它应该采用无限值。 Also allows me to test what the output should look like when I look up a specific structure in the file.还允许我在查找文件中的特定结构时测试输出应该是什么样子。

struct Department{
    string departmentName;
    string departmentHead;
    int departmentID;
    double departmentSalary;
};

int main()
{
    //...
   vector<Employee> emp;
   vector<Department> dept;
   vector<int> empID;
   vector<int> deptID;
        if(response==1){
            addDepartment(dept, deptID);
            fstream output_file("departments.dat", ios::in|ios::out|ios::binary);
            output_file.write(reinterpret_cast<char *>(&dept[counter-1]), sizeof(dept[counter-1]));
            output_file.close();
        }
        else if(response==2){
            addEmployee(emp, dept, empID);
        }
        else if(response==3){
            Department master;
            int size=dept.size();
            int index;
            cout << "Which record to EDIT:\n";
            cout << "Please choose one of the following... 1"<< " to " << size << " : ";
            cin >> index;
            fstream input_file("departments.dat", ios::in|ios::out|ios::binary);
            input_file.seekg((index-1) * sizeof(master), ios::beg);
            input_file.read(reinterpret_cast<char *>(&master), sizeof(master));
            input_file.close();
            cout<< "\n" << master.departmentName;
        }
        else if(response==4){

        }


//...

Files are streams of bytes.文件是字节流。 If you want to write something to a file and read it back reliably, you need to define the contents of the file at the byte level.如果您想将某些内容写入文件并可靠地将其读回,则需要在字节级别定义文件的内容。 Have a look at the specifications for some binary file formats (such a GIF) to see what such a specification looks like.查看一些二进制文件格式(如 GIF)的规范,看看这样的规范是什么样的。 Then write code to convert to and from your class instance and a chunk of bytes.然后编写代码以在您的类实例和一大块字节之间进行转换。

Otherwise, it will be hit or miss and, way too often, miss.否则,它会被击中或错过,而且经常会错过。 Punch "serialization C++" into your favorite search engine for lots of ideas on how to do this.将“序列化 C++”打入您最喜欢的搜索引擎,以获取有关如何执行此操作的许多想法。

Your code can't possibly work for an obvious reason.由于明显的原因,您的代码可能无法运行。 A string can contain a million bytes of data.一个string可以包含一百万字节的数据。 But you're only writing sizeof(string) bytes to your file.但是您只是将sizeof(string)字节写入文件。 So you're not writing anything that a reader can make sense out of.所以你没有写任何读者可以理解的东西。

Say sizeof(string) is 32 on your platform but the departmentHead is more than 32 bytes.假设sizeof(string)在您的平台上为 32,但departmentHead超过 32 个字节。 How could the file's contents possibly be right?文件的内容怎么可能是正确的? This code makes no attempt to serialize the data into a stream of bytes suitable for writing to a file which is ... a stream of bytes.此代码不会尝试将数据序列化为适合写入文件的字节流,该文件是……字节流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM