【问题标题】:Fstream _Fgetc access violationFstream _Fgetc 访问冲突
【发布时间】:2012-01-22 18:37:04
【问题描述】:

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

【问题讨论】:

  • 立即可见的一件事是 getline() 永远不会返回 false,因此您的循环永远不会终止。它实际上返回 ifs。
  • 删除using namespace std; 并拼出命名空间。 getline 有多个版本,因此请务必选择正确的版本。
  • @ybungalobill 酷!给我的消息,谢谢:) 只见过操作员!实际上就是这样做的......
  • 有机会获得崩溃的堆栈跟踪吗?

标签: c++ visual-studio-2010 fstream getline fgetc


【解决方案1】:

问题已解决,是旧库引起的。下载当前版本的MinGW后,就可以正常使用了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 2013-02-26
    • 2015-04-23
    • 2018-04-16
    • 1970-01-01
    相关资源
    最近更新 更多