简体   繁体   中英

Fstream _Fgetc access violation

I'd like to read line from file with fstream (I used this before with no errors), but now if I call getline, I get access violation exception. I traced exception thru code to function _Fgetc from fstream. That "if" line throws exception, but I don't know why.

I think, the file pointer is probably null, but what can I do with it? Or, is my function wrong? Miss I some setting in my Visual Studio 2010?

I'm using:

#include <vector>
#include <istream>
#include <fstream>
#include <string>

My function:

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;
}

_Fgetc from fstream that cause exception:

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);
    }
}

There are another 3 overloaded functions _Fgetc in fstream, some with fread, fgetwc, but how could I control which function will be used?

EDIT: Extract from my stack:

>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++

Problem solved, it was caused by old libraries. After downloading current version of MinGW, it works alright.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM