我明天有一个C ++决赛,我的教授说要确保我们能够正确地做这个练习:
这里是我的代码:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream file("118394.txt", ios::out | ios::binary);
int x = 1;
int y;
file.write(reinterpret_cast<char *>(x), sizeof(x));
file.close();
file.open("118394.txt", ios::in | ios::out | ios::binary);
file.read(reinterpret_cast<char *>(y), sizeof(y));
file.seekp(0L, ios::beg);
x++;
file.write(reinterpret_cast<char *>(x), sizeof(x));
file.close();
}
它将编译并运行,但是当它发生时,我得到一个错误的访问错误&#34;。
关于我做错的任何想法?我正在使用Xcode。