C++问题, 新手求教
写了一个文件写入和读取的程序,编译通过,但运行时异常停止运行,小弟新手,求教#include"stdafx.h"#include<iostream>#include<fs...
写了一个文件写入和读取的程序,编译通过,但运行时异常停止运行,小弟新手,求教
#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<utility>
using namespace std;
class MyClass
{
public:
MyClass(int);
~MyClass();
int n;
};
MyClass::MyClass(int i)
{
n = i;
}
MyClass::~MyClass()
{
}
int _tmain(int argc, _TCHAR* argv[])
{
vector<MyClass> ms;
ms.push_back(*new MyClass(3));
ms.push_back(*new MyClass(2));
errno_t err;
FILE *fp;
if ((err = fopen_s(&fp, "writetest.txt", "w")) == 0){
fwrite(&ms, sizeof(ms), 1, fp);
cout << "t" << endl;
}
fclose(fp);
FILE *fptr;
vector<MyClass> mso;
if ((err = fopen_s(&fptr, "writetest.txt", "r")) == 0){
fread(&mso, sizeof(mso), 1, fptr);
cout << "t" << endl;
}
cout << mso[1].n << endl;
fclose(fptr);
return 0;
} 展开
#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<utility>
using namespace std;
class MyClass
{
public:
MyClass(int);
~MyClass();
int n;
};
MyClass::MyClass(int i)
{
n = i;
}
MyClass::~MyClass()
{
}
int _tmain(int argc, _TCHAR* argv[])
{
vector<MyClass> ms;
ms.push_back(*new MyClass(3));
ms.push_back(*new MyClass(2));
errno_t err;
FILE *fp;
if ((err = fopen_s(&fp, "writetest.txt", "w")) == 0){
fwrite(&ms, sizeof(ms), 1, fp);
cout << "t" << endl;
}
fclose(fp);
FILE *fptr;
vector<MyClass> mso;
if ((err = fopen_s(&fptr, "writetest.txt", "r")) == 0){
fread(&mso, sizeof(mso), 1, fptr);
cout << "t" << endl;
}
cout << mso[1].n << endl;
fclose(fptr);
return 0;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询