memory access wrong with 0xC0000005

-2

plz help me, it is my homework, I always see 0x6E133817 (vcruntime140d.dll)处(位于 guanlixitong.exe 中)引发的异常: 0xC0000005: 读取位置 0x012539F8 时发生访问冲突。 出现了 . The problem is in the function vector IAttendance::query(). It happends in line 10.

vector<Attendance> IAttendance::query() {
    vector<Attendance> vec;
    ifstream ifs("attendance.txt", ios::in | ios::binary);
    if (!ifs.is_open()) {
        cout << "打开文件失败";
        return vec;
    }
    char* temp = new char[sizeof(Attendance)];
    while (ifs.read(temp, sizeof(Attendance))) {
        Attendance at = *(Attendance*)(temp);
        vec.push_back(at);
    }
    ifs.close();
    return vec;
}

I try to open a file but the memory was always wrong except the first time opening the file(code 1). I do not know how, but in my another cpp, I could open file like this successfully(code 2). this is my failed cpp.

#include "IAttendance.h"
#include<iostream>
#include<vector>
#include<string>
#include<fstream>
#include"Attendance.h"
using namespace std;

IAttendance::IAttendance(){}

bool IAttendance::inCard(Attendance am){
    try
    {
        ofstream ofs("attendance.txt", ios::app | ios::binary);
        ofs.write((const char*)&am, sizeof(Attendance));
        ofs.close();
        return true;
    }
    catch (const std::exception&)
    {
        return false;
    }
    return false;
}

bool IAttendance::outCard(Attendance am){
    try
    {
        ofstream ofs("attendance.txt", ios::app | ios::binary);
        ofs.write((const char*)&am, sizeof(Attendance));
        ofs.close();
        return true;
    }
    catch (const std::exception&)
    {
        return false;
    }
    return false;
}

vector<Attendance> IAttendance::query() {
    vector<Attendance> vec;
    ifstream ifs("attendance.txt", ios::in | ios::binary);
    if (!ifs.is_open()) {
        cout << "打开文件失败";
        return vec;
    }
    char* temp = new char[sizeof(Attendance)];
    while (ifs.read(temp, sizeof(Attendance))) {
        Attendance at = *(Attendance*)(temp);
        vec.push_back(at);
    }
    ifs.close();
    return vec;
}

this one failed.↑


this is my success cpp.

#include "Iuser.h"
#include<iostream>
#include<vector>
#include<string>
#include<fstream>
#include "User.h"
using namespace std;
Iuser::Iuser(){}

bool Iuser::check(string username, string password) {
    vector<User> vec = getAll();
    for (int i = 0; i < vec.size(); i++) {
        if (vec[i].getUsername()==username ||vec[i].getpassword()==password) {
            return true;
        }
    }
    return false;
}

bool Iuser::checkusername(string username) {
    vector<User> vec = getAll();
    for (int i = 0; i < vec.size(); i++) {
        if (vec[i].getUsername()==username) {
            return true;
        }
    }
    return false;
}

bool Iuser::regedit(User u) {
    try
    {
        ofstream ofs("user.txt", ios::app | ios::binary);
        ofs.write((const char*)&u, sizeof(User));
        ofs.close();
        return true;
    }
    catch (const std::exception&)
    {
        return false;
    }
    return false;
}

vector<User> Iuser::getAll() {
    vector<User> vec;
    ifstream ifs("user.txt", ios::in | ios::binary);
    if (!ifs.is_open()) {
        cout << "打开文件失败";
        return vec;
    }
    char* temp = new char[sizeof(User)];
    while (ifs.read(temp, sizeof(User))) {
        User u = *(User*)(temp);
        vec.push_back(u);
    }
    ifs.close();
    return vec;
}

this one success.↑


this is the file attendance.

#include "Attendance.h"
#include<iostream>
#include<vector>
#include<string>
#include<fstream>
using namespace std;

Attendance::Attendance(){}

string Attendance::getUsername() {
    return username;
}

void Attendance::setUsername(string username) {
    this->username = username;
}

string Attendance::getTime() {
    return time;
}

void Attendance::setTime(string time) {
    this->time = time;
}

int Attendance::getType() {
    return type;
}

void Attendance::setType(int type) {
    this->type = type;
}
c++
asked on Stack Overflow Feb 12, 2020 by tvp100 • edited Feb 12, 2020 by tvp100

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0