0

I am trying to read and write objects of my user defined structure to a binary file .My program writes my objects to the file then read it from the file but at the end it throws an exception 0x006787EA and my screen just become stuck and then I have to close visual studio forcefully. Please any one can spot out the mistake I am doing. This is the exception it is throwingThis is the output This is the code:

#include<iostream>
#include<fstream>
using namespace std;
struct Student {
    int roll_no;
    string name;
};
int main() {
    ofstream wf("student.dat");
    if (!wf) {
        cout << "Cannot open file!" << endl;
        return 1;
    }
    Student wstu[3];
    wstu[0].roll_no = 1;
    wstu[0].name = "mm";
    wstu[1].roll_no = 2;
    wstu[1].name = "Sasd";
    wstu[2].roll_no = 3;
    wstu[2].name = "sdasda";
    for (int i = 0; i < 3; i++)
        wf.write(reinterpret_cast<char*>(&wstu[i]), sizeof(Student));
    wf.close();
    if (!wf.good()) {
        cout << "Error occurred at writing time!" << endl;
        return 1;
    }
    ifstream rf("student.dat");
    if (!rf) {
        cout << "Cannot open file!" << endl;
        return 1;
    }
    Student rstu[3];
    for (int i = 0; i < 3; i++)
        rf.read(reinterpret_cast<char*>(&rstu[i]), sizeof(Student));
    rf.close();
    if (!rf.good()) {
        cout << "Error occurred at reading time!" << endl;
        return 1;
    }
    cout << "Student's Details:" << endl;
    for (int i = 0; i < 3; i++) {
        cout << "Roll No: " << wstu[i].roll_no << endl;
        cout << "Name: " << wstu[i].name << endl;
        cout << endl;
    }
    return 0;
}
  • 0XDDDDDDDD means already freed heap memory. Change the "Stack Frame" to see the line of your code that accessed the invalid pointer – drescherjm Feb 02 '21 at 17:08
  • 2
    Before the duplicate link is posted, where did you get the (false) information that you can write C++ objects this way: `wf.write(reinterpret_cast(&wstu[i]), sizeof(Student));` and similarly, read the object back using: `rf.read(reinterpret_cast(&rstu[i]), sizeof(Student));`? This is totally wrong, and I see this time and time again from new programmers. So someone or somewhere is teaching this. – PaulMcKenzie Feb 02 '21 at 17:08
  • No the bug, but you are printing "wstu" in the last loop. Shouldn't you display "rstu", the data stored from reading? – BNilsou Feb 02 '21 at 17:09
  • You can't serialize objects this way. std::string is not a POD type. It contains inner pointers that can not be restored and are meaningless since the actual string is located outside of the object in a different memory location. Related: [https://isocpp.org/wiki/faq/serialization](https://isocpp.org/wiki/faq/serialization) – drescherjm Feb 02 '21 at 17:11
  • 1
    If you look at those calls to read and write, you will see why this will never work for that class. The last parameter is `sizeof(Student)`, which tells how many bytes to write. The `sizeof(Student)` is a compile-time value, never changes. Let's say that `sizeof(Student)` is 32. What happens if `name` happens to be 100 characters? How will the `read` and `write` functions know to magically write at least 100 bytes to the file, and read 100 bytes from the file, when all you specified was `sizeof(Student)`? See how that isn't going to work? – PaulMcKenzie Feb 02 '21 at 17:14
  • 1
    Either use simple character array for `name`, or use proper object serialization techniques if you must have `std::string` for `name`. – PaulMcKenzie Feb 02 '21 at 17:18
  • @MussabMehboob -- Why are you still trying to get that line of code to work? It is totally wrong and should not be used for the `Student` class. Didn't you read the suggestion of simply using char arrays, or read the duplicate link if you must use `std::string`? If you used `char` array instead of `std::string`, then the code will "work". But whether you want to use char arrays is up to you. – PaulMcKenzie Feb 02 '21 at 17:23
  • 1
    It will not work as long as `Student` contains `string name;` You can make it work if you change `string name;` to `char[200] name;` or some other smaller size that you think no one will have a longer name. – drescherjm Feb 02 '21 at 17:25
  • @PaulMcKenzie Can you please elaborate that what is the correct way of writing and reading objects from binary files? – Mussab Mehboob Feb 02 '21 at 17:26
  • 2
    @MussabMehboob The duplicate link explains it to you. Plus there was another link to isocpp in an earlier comment. Whatever you were told about binary files being "simple", you were told wrong. They are only "simple" if the type you're reading / writing contains no pointers, and is trivially-copyable. Your `Student` class fails to meet those requirements. – PaulMcKenzie Feb 02 '21 at 17:28

0 Answers0