Here's some code that represnts a part of what my bigger program should do. It runs absolutely fine.
However, when I change the types of variables inside of book from int to string and write strings instead of numbers there, Visual Studio takes me to the xmemory page where it says "Write access violation".
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
struct book
{
int name, author;
};
int main()
{
setlocale(LC_ALL, ".866");
book var;
var.author = 1;
var.name = 2;
ofstream outFile;
outFile.open("file.bin", ios::binary);
outFile.write((char*)&var, sizeof(book));
outFile.close();
book newVar;
ifstream inFile;
inFile.open("file.bin", ios::binary);
inFile.read((char*)&newVar, sizeof(book));
inFile.close();
cout << newVar.name;
}
Why is this and how to fix that for string? I think I have missed something very basic and very important