2012-01-22 10 views
0

fstream을 사용하여 파일에서 행을 읽고 싶습니다. (오류없이 사용 했었습니다.) 이제 getline을 호출하면 액세스 위반 예외가 발생합니다. fstream에서 _Fgetc를 기능화하기 위해 코드를 통해 예외를 추적했습니다. 그 "if"라인이 예외를 던지지만, 나는 왜 그런지 모른다.Fstream _Fgetc 액세스 위반

파일 포인터가 아마도 null이지만 파일로 무엇을 할 수 있을까요? 또는 내 기능이 잘못 되었습니까? Visual Studio 2010의 일부 설정을 놓치셨습니까?

내가 사용 :

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

내 기능 : fstream에서

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의 원인이 예외 :

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

다른 3 개 오버로드 기능 _Fgetc가 fstream에있다, 일부 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++ 
+0

바로 보이는 것은 getline()이 false를 반환하지 않으므로 루프가 종료되지 않습니다. 실제로는 ifs를 반환합니다. –

+0

'using namespace std;'를 제거하고 네임 스페이스를 철자하십시오. 'getline'의 여러 버전이 있으므로 올바른 버전을 가져야합니다. –

+0

@JoachimIsaksson :'ifs'가 bool로 변환됩니다. 스트림이 나쁜 상태 (예 : 마지막 작업이 실패한 경우) 인 경우 false입니다. – ybungalobill

답변

2

문제는이 오래된 라이브러리에 의해 발생 된, 해결했다. MinGW의 최신 버전을 다운로드하면 문제가 없습니다.