2012-11-22 2 views
1

제목에서 알 수 있듯이 공백을 포함하여 파일의 모든 문자를 읽고 새 줄을 제외하고 싶습니다.공백을 포함하여 문자 읽기 및 파일에서 줄 바꾸기 제외 C++

파일보기의 예.

 .  
##   . : 
    #___-  
## 
-------------- 
______________ 

다음지도를 사용하여 각 문자를 정수로 변환합니다.

나는 단지`분할 오류 (코어 덤프) 얻을 내가 컴파일지도 std::map<char, int> map_converter;

std::ifstream map("level_1.map"); 

for(int t = 0; t < TOTAL_TILES; t++) { 
    int tileType = -1; 
    char load_type = ' '; 

    map >> load_type; 
    tileType = map_converter.find(load_type)->second; 
    tiles[t] = new Tile(x, y, tileType); 
} 

내가 어떻게해야합니까? `

+0

의심의 여지가 없습니다. –

+2

'if (map_converter.find (load_type) == map_converter.end())'문제가 발생합니다. end iterator에서'second'를 호출하는 것은 유효하지 않습니다. – andre

+0

흠, 나는 당신이 의미하는 것을 얻지 못합니다. 나는 C++에 상당히 익숙하다. 어떻게 해결해야합니까? –

답변

2

std::map::find 값을 찾지 못하면 std::map::end을 반환합니다. 이 문제는 tileType = std::map::end()->second 지정이 유효하지 않습니다.

std::ifstream map("level_1.map"); 

for(int t = 0; t < TOTAL_TILES; t++) { 
    int tileType = -1; 
    char load_type = ' '; 

    map >> std::noskipws >> load_type; 

    if (map_converter.find(load_type) == map_converter.end()) { 
     continue; 
    } 
    tileType = map_converter.find(load_type)->second; //this is now safe to do. 
    tiles[t] = new Tile(x, y, tileType); 
} 
+0

하지만 한 가지 문제가 여전히 있습니다. 어떻게 공백을 건너 뛰지 않습니까? –

+0

@DanAndreasson 스트림에 대해 [noskipws] (http://www.cplusplus.com/reference/iostream/manipulators/noskipws/)를 설정하십시오 ('map >> std :: noskipws;') – andre

+0

흠. noskipws를 사용할 때 세그먼트 오류 오류가 다시 발생합니다. –

관련 문제