次のコードは、コマンドプロンプトから呼び出された場合にのみシステムアクセス違反例外をスローします。どうして?実行時に、例外はfstreamの148行目にスローされます。これはVSでデバッグするときには発生しませんが、コマンドプロンプトからコンパイルされたプログラムを実行しようとすると、昇格したコマンドプロンプトと同じです。システムアクセス違反はVS2010ではなくコンパイルされた形式でスローされます。なぜですか?
#include <stdafx.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <windows.h>
using namespace std;
using namespace System;
ifstream::pos_type size;
int filesize;
char * memblock;
int main() {
fstream wfile ("C:\\Plans\\Plan.txt" , ios::out|ios::ate|ios::app);
if(wfile.is_open())
{
wfile<<"\n";
wfile.close();
}
ifstream file ("C:\\Plans\\Plan.txt" , ios::in|ios::ate);
if (file.is_open()){
int size = file.tellg();
filesize= size;
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
cout << size << " bytes loaded into memory" << endl;
return 1;
}
else cout << "Unable to open file" << endl;
return 0;
}
例外は何行発生しますか?例外の正確なテキストは何ですか? –
2バイトがメモリにロードされます。未処理の例外:System.AccessViolationException:保護されたメモリからの読み取りまたは書き込みを試みました。これはしばしば、他のメモリが壊れていることを示します。 std.basic_streambuf>/{dtor}(basic_streambuf > *) –
John
開いているファイルのサイズは約5kです。 – John