fstream を使用してファイルから行を読み取りたいのですが (以前はエラーなしでこれを使用していました)、getline を呼び出すと、アクセス違反の例外が発生します。fstream から関数 _Fgetc へのコードを介して例外を追跡しました。その「if」行は例外をスローしますが、その理由はわかりません。
おそらくファイル ポインタは null だと思いますが、どうすればよいでしょうか。または、私の機能が間違っていますか?Visual Studio 2010 の設定を見逃していませんか?
私は使用しています:
#include <vector>
#include <istream>
#include <fstream>
#include <string>
私の機能:
bool ImageOp::parseMap(LPTSTR filename){
if(filename == NULL) return false;
fstream ifs;
ifs.open ( "me_l1.dm" , ios::in );
if(!ifs.is_open())
return false;
vector<vector<int>> parsedMap;
string line;
while(getline( ifs, line)){
parsedMap.push_back(splitValues(line));
}
ifs.close();
return true;
}
例外を引き起こす fstream からの _Fgetc:
template<> inline bool _Fgetc(char& _Byte, _Filet *_File)
{ // get a char element from a C stream
int _Meta;
if ((_Meta = fgetc(_File)) == EOF)
return (false);
else
{ // got one, convert to char
_Byte = (char)_Meta;
return (true);
}
}
fstream には別の 3 つのオーバーロードされた関数 _Fgetc があり、いくつかは fread、fgetwc を使用していますが、どの関数を使用するかを制御するにはどうすればよいですか?
編集:私のスタックから抽出:
>ntdll.dll!77178dc9()
[Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll]
ntdll.dll!77178cd8()
msvcrt.dll!752eaad6()
>DialogBasedApp.exe!std::_Fgetc<char>(char & _Byte, _iobuf * _File) Line 37 + 0x9 bytes C++
DialogBasedApp.exe!std::basic_filebuf<char,std::char_traits<char> >::uflow() Line 435 + 0x10 bytes C++
DialogBasedApp.exe!std::basic_filebuf<char,std::char_traits<char> >::underflow() Line 413 + 0xf bytes C++
DialogBasedApp.exe!std::basic_streambuf<char,std::char_traits<char> >::sgetc() Line 153 + 0x50 bytes C++
DialogBasedApp.exe!std::getline<char,std::char_traits<char>,std::allocator<char> >(std::basic_istream<char,std::char_traits<char> > && _Istr, std::basic_string<char,std::char_traits<char>,std::allocator<char> > & _Str, const char _Delim) Line 412 + 0x23 bytes C++
DialogBasedApp.exe!std::getline<char,std::char_traits<char>,std::allocator<char> >(std::basic_istream<char,std::char_traits<char> > & _Istr, std::basic_string<char,std::char_traits<char>,std::allocator<char> > & _Str) Line 483 + 0x2e bytes C++
DialogBasedApp.exe!ImageOp::parseMap(char * filename) Line 167 + 0x13 bytes C++