2013-09-26 4 views
1

I는 다음과 같이 텍스트 파일의 줄이있는 경우 :주문 데이터

1 4:48:08 Orvar Steingrimsson     1979 30 - 39 ara  IS200 
2 4:52:25 Gudni Pall Palsson     1987 18 - 29 ara  IS870 

어떻게 내가 출력이 새 텍스트 파일 위에 데이터 만 세 가지 목록 : 년 - 이름을 - 시간 ...이 두 라인은 다음과 같이 할 수 있도록 :

1979 Orvar Steingrimsson 4:48:08 
1987 Gudni Pall Palsson 4:52:25 

이 내 생각을했다 :

ifstream in("inputfile.txt"); 
ofstream out("outputfile.txt"); 
int score, year; 
string name, time, group, team; 
while (getline(in,str)); 
in >> score >> time >> name >> year >> group >> team; 
//and then do something like this 
out << year << name << time << '\n'; 

그러나 내가 가지고 철 eling 전체 텍스트 파일과 200 줄 모두를 반복 할 수 없습니다. 어떤 팁 감사!

+0

나는 이것을 위해'scanf'를 사용할 것이지만 나는 C의 파일 조작 함수쪽으로 편향되어있다. "적절한"C++에서는 아마도'>>'를 사용할 것입니다. 어쨌든 당신의 정확한 문제에 대해, 당신은 잘못된'substr'을 사용하고 있습니다. 당신은'str.substr (54,4)'를 원한다. 사용중인 것은 C 스타일이며'std :: string's에서는 작동하지 않습니다. – Dave

+0

두 번째 문제 : C/C++에서의 문자는 문자를 의미하고, 문자는 문자열을 의미합니다. – Dave

답변

0

문자열을 추출한 후 strtol() (또는 C++. 11이있는 경우 std::stoi())을 호출하여 문자열을 정수로 변환 할 수 있습니다. 연도와 데이터 행이 모두 있으면 테이블에 저장할 수 있습니다. std::multimap<> 일 수 있습니다.

C없이

++ 11 :. C에서

void process (std::istream &in, std::ostream &out) { 
    typedef std::multimap<int, std::string> table_type; 
    table_type data_by_year; 
    std::string str; 
    while (std::getline(in,str)) { 
     int year = strtol(str.substr(54, 4).c_str(), 0, 10); 
     data_by_year.insert(std::make_pair(year, str)); 
    } 
    for (table_type::iterator i = data_by_year.begin(); 
     i != data_by_year.end(); 
     ++i) { 
     out << i->second << "\n"; 
    } 
} 

++ 11 :. 위의 질문에서

void process (std::istream &in, std::ostream &out) { 
    std::multimap<int, std::string> data_by_year; 
    std::string str; 
    while (std::getline(in,str)) { 
     int year = std::stoi(str.substr(54, 4)); 
     data_by_year.insert(std::make_pair(year, str)); 
    } 
    for (auto v : data_by_year) { 
     out << v.second << "\n"; 
    } 
} 
+0

주 :'std :: stoi'는 C++ 11을 지원합니다. – LihO

+0

@LihO : 음 ... 그럼'auto' 그리고 새로운'for' 문법도 있습니다. 그러나 질문은 C++ 11로 태그 화되지 않았습니다. 좋아,'strtol()'로 바 꾸었습니다. 덕분에 – jxh

+0

누군가이 줄을 어떻게 사용하는지 설명 할 수 있습니까? std :: multimap data_by_year; –

0

, 당신의 필요 '또는'\ t '분리와 파일을 분할하는 것입니다 생각 '. C++에서는 boost 라이브러리를 사용할 수 있습니다. 부스트에서 : 또한 분리를 찾기 위해 find 또는 strchr 기능을 사용할 수 있습니다

#include <stdlib.h>  
#include <iostream> 
#include <string.h> 

using namespace std; 

int main(int argc, char** argv) 
{ 
    char str[] = "Hello, the beautiful world!"; 
    char spliter[] = " ,!"; 

    char * pch; 
    pch = strtok(str, spliter); 
    while(pch != NULL) 
    { 
    cout << pch << endl; 
    pch = strtok(NULL, spliter); 
    } 
    return 0; 
} 

#include <iostream> 
#include <string> 
#include <vector> 
#include <boost/algorithm/string/classification.hpp> 
#include <boost/algorithm/string/split.hpp> 

using namespace std; 

int main(int argc, char** argv) 
{ 
    string s = "Hello, the beautiful world!"; 
    vector<string> rs; // store the fields in your text. 
    boost::split(rs, s, boost::is_any_of(" ,!"), boost::token_compress_on); 
    for(vector<string>::iterator it = rs.begin(); it != rs.end(); ++ it) 
    cout << *it << endl; 

    return 0; 
} 

또는 사용 strtok를() 함수에, 당신은 그것을 나눌 수있다. 첫 번째 샘플에서는 vector<string> rs 또는 두 번째 샘플에서는 pchyear을 수신 할 수 있습니다.