我的file1.txt里有这样的信息。
4231650|A|4444
4225642|A|5555
我检查了这里的代码,如何在C++中读取以管道分隔的文件。
我根据自己的需要修改了一下代码。问题是,它能很好地读取第一个管道,但之后如何读取其余的值?
这是我的代码。
std::ifstream file("file1.txt");
std::string line;
while(std::getline(file, line))
{
std::stringstream linestream(line);
std::string data;
std::string valStr1;
std::string valStr2;
std::getline(linestream, data, '|'); // read up-to the first pipe
// Read rest of the pipe values? Why did the accepted answer worked for int but not string???
linestream >> valStr1 >> valStr2;
cout << "data: " << data << endl;
cout << "valStr1: " << valStr1 << endl;
cout << "valStr2: " << valStr2 << endl;
}
这是输出结果
Code Logic starts here ...
data: 4231650
valStr1: A|4444
valStr2: A|4444
data: 4225642
valStr1: A|5555
valStr2: A|5555
Existing ...