Answer the question
In order to leave comments, you need to log in
How to fix "Access Violation when writing to address" error?
#pragma warning(disable:4996)
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
struct Data {
int year;
int mounth;
int day;
};
struct AutoBase {
string Name;
Data Release;
Data SaleRelease;
};
void input();
void output(string);
AutoBase Divide(AutoBase, string, bool);
int main()
{
input();
cout << "\nList of cars";
output("AutoBase.txt");
}
void input() {
ofstream In("AutoBase.txt", ios::binary);
char k = '+';
AutoBase a;
string Read;
while (k == '+') {
cout << "Enter name of car: "; getline(cin, a.Name);
cout << "Enter Release Date with a point '10.10.2010': "; getline(cin, Read);
a = Divide(a, Read, 0);
cout << "Enter Sale Release Date with a point '10.10.2010': "; getline(cin, Read);
a = Divide(a, Read, 1);
cout << "\nEnter '+' in case you want to contnue: "; cin >> k;
In.write((char*)&a, sizeof(AutoBase));
cin.ignore();
}
In.close();
}
AutoBase Divide(AutoBase a, string Read, bool b) {
int pos1 = Read.find('.');
int pos2 = Read.rfind('.');
if (!b) {
a.Release.day = stoi(Read.substr(0, pos1));
a.Release.mounth = stoi(Read.substr(pos1 + 1, pos2));
a.Release.year = stoi(Read.substr(pos2 + 1));
}
else {
a.SaleRelease.day = stoi(Read.substr(0, pos1));
a.SaleRelease.mounth = stoi(Read.substr(pos1 + 1, pos2));
a.SaleRelease.year = stoi(Read.substr(pos2 + 1));
}
return a;
}
void output(string name) {
ifstream Out(name, ios::binary);
AutoBase a;
while (Out.read((char*)&a, sizeof(AutoBase))) {
cout << endl << "Name: " << a.Name << " Release date: " << a.Release.day << "." << a.Release.mounth << "." << a.Release.year << " Slae Release Date: " << a.SaleRelease.day << "." << a.SaleRelease.mounth << "." << a.SaleRelease.year;
}
Out.close();
}
Answer the question
In order to leave comments, you need to log in
This error means that you are writing to some memory that does not belong to you.
Or you have an uninitialized pointer there, or an array out of bounds, or something like that. It is necessary to take a closer look at all the pointers in the program.
It is possible to press "continue" during debugging and then the debugger will stop exactly at the instruction that caused the error. Then you can already see what variable you write there and where it came from.
It falls because it is impossible to read string and write to a file like this, simply by interpreting the object's memory as char*. Because string contains pointers to dynamically allocated memory.
Therefore, when you write it (as part of AutoBase) to a file and then read it, you get a pointer to the address that was alive along with the old class instance. However, after deleting this old instance, this address no longer belongs to you.
You can only write structures of simple types to a file in this way, and even then not always (any alignment can play a trick on you there). You have to write your own serialization and deserialization methods. A string can be stored, for example, as a length and then as many bytes as needed. Then, when reading, you first clear some bytes of size, and then the required number of bytes into the string itself.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question