【发布时间】:2014-12-16 03:56:06
【问题描述】:
我在这段代码中遇到了异常: Project4.exe 中出现“System.AccessViolationException”类型的未处理异常
附加信息:试图读取或写入受保护的内存。这通常表明其他内存已损坏。
但它不是受保护文件或只读文件!!! ,当我使用文本文件时,错误消失了
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
struct student{
string name;
int age;
};
int main(){
student s1, s2, s3;
s1.name = "basel";
s1.age = 20;
ofstream in;
in.open("example.std" ,ios::binary);
in.write((char*)&s1, sizeof(s1));
in.close();
ifstream out;
out.open("example.std" ,ios::binary);
out.read((char*)&s2, sizeof(s2));
cout << s2.name;
return 0;
}
任何人都可以帮忙!!!
【问题讨论】:
-
'System.AccessViolationException' 是 C++/clr 异常(您可以调整项目设置)
-
.. 它不是指文件 i/o,而是指进程中的内存访问(如您可能尝试写入或读取的错误描述中所述)或来自无效的内存区域)
标签: c++ visual-c++