我假设你需要:我想读每行一个号码。
否则评论我;我将删除答案。
与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 ...
它是如何工作的?
由于您的文件只有2行,所以我在同一个文件中使用了两个input_stream
。其中一个是第一行,另一个是第二行。但在去循环之前。 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();
输出::(与上面相同)而在for循环,(或同时),你可以通过>>
操作
或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