Fstream_Fgetc访问冲突

Fstream _Fgetc access violation

本文关键字:访问冲突 Fgetc Fstream      更新时间:2023-10-16

我想用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++

问题解决了,它是由旧库引起的。下载当前版本的MinGW后,它可以正常工作。