我想用 fstream 从文件中读取行(我以前用过这个没有错误),但现在如果我调用 getline,我会得到访问冲突异常。我通过代码跟踪异常以从 fstream 函数 _Fgetc。那条“if”行抛出异常,但我不知道为什么。
我认为,文件指针可能为空,但我能用它做什么呢?或者,我的功能错了吗?想念我在 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++