2014-03-27 5 views
0

작성중인 클러스터링 프로그램의 경우 파일에서 정보를 읽어야합니다. 나는 특정 형식의 파일에서 좌표를 읽기 위해 노력하고 있어요 :파일에서 복식 쌍 읽기 (C++)

1.90, 2 
0.0, 4.01 
6, 1.00 

불행하게도, 내가 있기 때문에이 파일에 존재하는 줄 바꿈과 점으로,이 작업을 수행 할 수 없었다. 다음과 같은 두 가지 기능 중 어느 쪽이 파일 스트림은 "좋은"임에도 불구하고, 작업 :

std::vector<Point*> point_list_from_file(std::ifstream& ifstr) { 
    double x, y; 
    char comma; 

    std::vector<Point*> point_list; 

    while(ifstr >> x >> comma >> y) { 
     point_list.push_back(new Point(x,y)); 
    } 

    return point_list; 
} 

std::vector<Point*> point_list_from_file(std::ifstream& ifstr) { 
    double x, y; 
    char comma; 

    std::vector<Point*> point_list; 

    while(ifstr >> x >> comma >> y >> comma) { 
     point_list.push_back(new Point(x,y)); 
    } 

    return point_list; 
} 

내가 어떻게이 문제를 해결하는 방법 아무 생각이없고 어떤 도움이 크게 감사합니다.

+0

정확하게 당신이 겪고있는 문제가 무엇입니까? – 0x499602D2

+2

첫 번째 방법은 표시되는 입력에 대한 [ideone (link)에 완벽하게 작동] (http://ideone.com/1bNKHA)입니다. – dasblinkenlight

+0

흠, 문제는 무엇인지 모르겠지만 텍스트 파일이 리눅스에서 만들어졌고 윈도우즈에서 코딩하고 있다는 사실과 관련이 있을지도 모릅니다. 다른 개행과 관련이 있습니까? 나는 모른다. 코드가 동일하게 유지되는 동안 입력 파일을 조작 한 후 지금은 작동하는 것 같습니다. – Knaapje

답변

0

노력이 한 -

std::vector<Point*> point_list_from_file(std::ifstream& ifstr) { 
    char sLineChar [256]; 
    std::vector<Point*> point_list; 
    ifstr.getline (sLineChar, 256); 

    while (ifstr.good()) { 
     std::string sLineStr (sLineChar); 
     if (sLineStr.length() == 0){ 
      ifstr.getline (sLineChar, 256); 
      continue; 
     } 
     int nSep = sLineStr.Find (','); 
     double x = atof (sLineStr.Mid(0,nSep).Trim()); 
     double y = atof (sLineStr.Mid(nSep+1).Trim()); 
     point_list.push_back(new Point(x,y)); 
     ifstr.getline (sLineChar, 256); 
    } 
    return point_list; 

}