나는 당신이 필요로하는 가정내가 각 라인에서 하나 개의 번호를 읽고 싶어.
그렇지 않으면 나를 코멘트하십시오; 나는 대답을 지울 것이다.
병렬 2 개 스트림으로 파일을 읽어
std::ifstream input_file_stream_1("file");
std::ifstream input_file_stream_2("file");
std::string line_1;
std::string line_2;
std::string ignore;
std::getline(input_file_stream_2, ignore); // ignore the whole first line
for(; input_file_stream_1 >> line_1 && input_file_stream_2 >> line_2;){
std::cout << line_1 << " and " << line_2 << '\n';
}
input_file_stream_1.close();
input_file_stream_2.close();
입력 :
X.11 X.12 X.13 ...
X.21 X.22 X.23 ...
출력 :
X.11 and X.21
X.12 and X.22
X.13 and X.23
... and ...
,745,151을
어떻게 작동합니까?
파일에 2 줄만 있으므로 같은 파일에 2 개의 input_stream
을 사용했습니다. 첫 번째 줄에는 두 번째 줄과 두 번째 줄에 대한 줄 중 하나.그러나 for-loop로 가기 전에. input_file_stream_2
은 첫 번째 행을 읽고 input_file_stream_1
이 이것을 읽고 싶어하므로이를 사용할 필요가 없습니다. 그래서 그 라인 (첫 번째 라인)을 무시하십시오. input_file_stream_1
에는 줄 1이 있고 input_file_stream_2
에는 줄 2가 있습니다. 이제 스트림에 두 줄이 있습니다.
std::ifstream input_file_stream("file");
std::string line_1;
std::string line_2;
std::getline(input_file_stream, line_1);
std::getline(input_file_stream, line_2);
std::regex regex(R"(\s+)");
std::regex_token_iterator<std::string::iterator> begin_1(line_1.begin(), line_1.end(), regex, -1), end_1;
std::regex_token_iterator<std::string::iterator> begin_2(line_2.begin(), line_2.end(), regex, -1), end_2;
while(begin_1 != end_1 && begin_2 != end_2){
std::cout << *begin_1++ << " and " << *begin_2++ << '\n';
}
input_file_stream.close();
출력 : (상기와 동일) 그리고 용 루프 (또는 동안) 귀하 >>
연산자
또는 std::regex
라이브러리 각각 텍스트를 추출 할
X.11 and X.21
X.12 and X.22
X.13 and X.23
... and ...
참고 :
하나 이상의 방법이 있습니다
X.11 & X.12는 X.11 & X.21이어야합니다. – CIsForCookies
원하는대로 할 수는 있지만 프로그램을 작성하여 전체 파일을 조 변경 한 다음 줄 단위로 읽을 수 있습니다. –
숫자 길이와 줄 길이가 고정되어 있습니까? 이 경우 다음 열의 시작 위치를 계산하고 [seekg] (http://www.cplusplus.com/reference/istream/istream/seekg/)를 사용합니다 (예를 들어 그 원인을 묻는 질문 ...) – urban