如何在C++中读取格式化数据?

The*_*y92 14 c++ string iostream ifstream string-parsing

我格式化了如下数据:

Words          5
AnotherWord    4
SomeWord       6

它在一个文本文件中,我使用ifstream来读取它,但是如何将数字和单词分开?这个单词只包含字母,单词和数字之间会有一些空格或标签,不确定多少.

Don*_*alo 20

假设"单词"中没有任何空格(那么它实际上不是1个单词),这里有一个如何读取到文件末尾的示例:

std::ifstream file("file.txt");
std::string str;
int i;

while(file >> str >> i)
    std::cout << str << ' ' << i << std::endl;
Run Code Online (Sandbox Code Playgroud)