-2

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){

        }


//...
stack ex
  • 111
  • 5
  • The writing of the file seems to be working fine, it does fill the file with characters after running the code – stack ex Jul 26 '19 at 03:16
  • 1
    How can we make sense of this code if wen can't tell what type `dept` is? Or see the class definition (if it is a class) of `Department`. – David Schwartz Jul 26 '19 at 03:33
  • @David Schwartz You are correct, I added the structure declaration. No classes are allowed for this particular assignment. – stack ex Jul 26 '19 at 03:39
  • 1
    Yeah, that's not going to work, see my answer. – David Schwartz Jul 26 '19 at 03:41
  • 1
    Possible duplicate of [debug read/write string to binary file](https://stackoverflow.com/questions/20896825/debug-read-write-string-to-binary-file) – JaMiT Jul 26 '19 at 03:50
  • @JaMiT yes, I think it starts to help me figure out what is going wrong with my code. I think I'll need to do some more reading – stack ex Jul 26 '19 at 04:00
  • 'Reading and writing binary files using structures' ... is already a seriously bad idea. There are around a dozen ways it can go wrong. Don't do this. Don't use structures as file formats. Use file formats as file formats. – user207421 Jul 26 '19 at 04:21

1 Answers1

1

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. 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.

Your code can't possibly work for an obvious reason. A string can contain a million bytes of data. But you're only writing sizeof(string) bytes to your file. 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. 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.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • the dept vector is a vector of structures. I was under the impression that sizeof() anything would give the right amount of bytes for reading and writing. Such as sizeof a vector dept or an individual structure. Should I be reading and writing the individual elements inside the structure one at a time? – stack ex Jul 26 '19 at 03:48
  • @stackex That can't possibly be right. Think about it. How could you possibly know how many bytes to read? The strings could be 5 bytes or 5 milllion bytes. Unfortunately, `sizeof` evaluates to a compile time constant that is the size of the *type*. The parameter to `sizeof` itself isn't even evaluated. `sizeof(a++)` will NOT increment `a` or in any way look at its value. – David Schwartz Jul 26 '19 at 03:50
  • I see, I will do some more reading and see if I can find a fix. Thanks! – stack ex Jul 26 '19 at 03:56