I'm learning file I/O in c++ ATM and I'm getting a strange problem when I'm (at least I think so) following a tutorial.
What I'm doing is simply writing a User object to a file
and then read that same object from the file. I'm doing this just to see that the file operations work.
Code below (place inside a member function of the User class):
#include <fstream> fstream f("D:/users.bin", ios::in | ios::out | ios::binary); User userpost = *this; User readuserpost; f.write(reinterpret_cast<char *>(&userpost), sizeof(userpost)); f.seekg(0, ios::beg); f.read(reinterpret_cast<char *>(&readuserpost), sizeof(readuserpost)); f.close();
Now, the above works fine. But if I comment out the f.write() command, I get an access violation / segmentation error.
So, I try to comment out the f.seekg() command, and now it works fine again. But I want to be able to move through the file so I really need it :) What am I doing wrong?