程序在VS 2008中运行良好,在程序结束时在VS 2010中抛出异常

时间:2014-10-22 22:12:34

标签: c++ visual-studio-2010 visual-studio-2008 incompatibility

我正在使用二进制/直接访问来写文件。在2008年像魅力一样工作,但在2010年运行时在第128行创建了一个例外:Unhandled exception at 0x535dad54 (msvcp100d.dll) in HovedProsjekt.exe: 0xC0000005: Access violation writing location 0xfeeefeee.

单击该错误会将我带到xutility,其中第201行:_Pnext != 0; *_Pnext = (_Pnext)->_Mynextiter)接下来要执行。我看着微软和#34;突破变化"页面没有运气。我以明文形式复制/粘贴整个代码,因此没有过时的库。我已经尝试了启用和禁用_HAS_ITERATOR_DEBUGGING和_SECURE_SCL的所有组合。我在另一个用户的建议上改变了我的问题以修改我的代码。

//Miniprosjekt.cpp

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool sjekkForOverskriving(void);
void LagFil(void);
void LeggTil();
void skrivUtFeil();

class Feil{
    friend ostream& operator<<(ostream&, Feil);
    friend istream& operator>>(istream&, Feil&);
protected:
    int ID;
    int prioritet;
    string dato;
    char beskrivelse[300];
    char skrevetAv[30];
public:
    Feil();
    Feil(int, int, string, string, string);
};

Feil::Feil(){ //Default constructor
    ID = 0;
    prioritet = 1;
    dato = "01.01.1900";
    beskrivelse[0] = ' ';
    skrevetAv[0] = ' ';
}

Feil::Feil(int id, int pri, string d, string beskr, string navn){ //Constructor
    ID = id;
    prioritet = pri;
    dato = d;
    strcpy(beskrivelse, beskr.c_str());
    strcpy(skrevetAv, navn.c_str());
}


ostream& operator<<(ostream& out, Feil enfeil) //Overloader << operator, allowing for: cout << Feil afeil
{
    out << "Id nr: " << enfeil.ID << "\nSkrevet av: " << enfeil.skrevetAv  << "\nDato: " <<
        enfeil.dato << "\nPrioritet: ";
    if(enfeil.prioritet == 1)out << "Lav";
    else if(enfeil.prioritet == 2)out << "Middels";
    else out << "Høy";
    out << "\nBeskrivelse:\n" << enfeil.beskrivelse << endl;
    return out;
}

istream& operator>>(istream& in, Feil& enfeil) //Overloader >> operator: allowing for cin >> Feil afeil
{
    in >> enfeil.ID >> enfeil.dato >> enfeil.beskrivelse >> enfeil.skrevetAv;
    return in;
}

int main(void){

    LagFil();
    LeggTil();
    skrivUtFeil();

    return 0;
}



void LagFil(void){ //Creates a file with 500 empty "feil" objects
    const int MAXFEIL = 500;
    Feil enfeil;
    ofstream utFil;
    utFil.open("FeilTeller.dat");
    utFil << 0;
    utFil.close();
    utFil.open("Feilmeldinger.dat", ios::out | ios::binary);
    for(int a = 0; a <= MAXFEIL; a++){ //Fills the file with empty "Feil" objects
        utFil.write(reinterpret_cast <const char*>(&enfeil), sizeof(enfeil));
    }
    utFil.close();
}

void LeggTil(){
    int ID;
    ifstream innFil;
    innFil.open("FeilTeller.dat");
    innFil >> ID;
    innFil.close();
    fstream fil;
    fil.open("FeilTeller.dat");
    ID++; //Opens feilteller.dat, reads how many objects have already been created. Adds one to that to append.
    fil << ID;
    fil.close();
    int pri = 1;
    string beskrivelse = "test";
    string dato = "test2";
    string skrevetAv = "test3";
    Feil enfeil(ID, pri, dato, beskrivelse, skrevetAv);
    fil.open("Feilmeldinger.dat", ios::out | ios::in | ios::binary);
    fil.seekp((ID - 1) * sizeof(enfeil));
    fil.write(reinterpret_cast <const char*>(&enfeil),
            sizeof(Feil));
    fil.close();
}

void skrivUtFeil(){
    int idnr = 1;
    //for(int i = 0; i < 2; i++){
    Feil enfeil;
    int antallFeil;
    ifstream innFil;
    innFil.open("FeilTeller.dat");
    innFil >> antallFeil;
    innFil.close();
    innFil.open("Feilmeldinger.dat", ios::in | ios::binary);
    innFil.read(reinterpret_cast <char*>(&enfeil),
            sizeof(Feil));
    innFil.clear();
    //Prints out Feil with ID "idnr"
    innFil.seekg((idnr - 1) * sizeof(enfeil));
    innFil.read(reinterpret_cast <char*>(&enfeil),
            sizeof(Feil));
    cout << enfeil << endl;
    cout << "Totalt antall feil: " << antallFeil << endl;
    system("pause");
    innFil.close();
    //}
} //Exception on this line

1 个答案:

答案 0 :(得分:1)

您的班级Feil包含string dato,因此它是not trivially copyable。因此,将其作为原始二进制数据进行写入和读取是非法的。

如果你这样做会怎么样? std::string包含指向字符数组的指针,因此您可以编写其值,而不是内容。当你阅读它时,你会在指针中读取相同的值,但它指向垃圾。