C++ istringstream-如何做到这一点?

C++ istringstream-如何做到这一点?,c++,file-io,C++,File Io,我有一个文件: a 0 0 b 1 1 c 3 4 d 5 6 使用istringstream,我需要先得到a,然后是b,然后是c,等等。但我不知道如何做到这一点,因为在网上或我的书中没有好的例子 迄今为止的代码: ifstream file; file.open("file.txt"); string line; getline(file,line); istringstream iss(line); iss >> id; getline(file,line); iss &g

我有一个文件:

a 0 0
b 1 1
c 3 4
d 5 6
使用istringstream,我需要先得到a,然后是b,然后是c,等等。但我不知道如何做到这一点,因为在网上或我的书中没有好的例子

迄今为止的代码:

ifstream file;
file.open("file.txt");
string line;

getline(file,line);
istringstream iss(line);
iss >> id;

getline(file,line);
iss >> id;
这两次都会为id打印“a”。显然,我不知道如何使用istringstream,我必须使用istringstream。请帮忙

ifstream file;
file.open("file.txt");
string line;

getline(file,line);
istringstream iss(line);
iss >> id;

getline(file,line);
istringstream iss2(line);
iss2 >> id;

getline(file,line);
iss.str(line);
iss >> id;

istringstream
复制您给它的字符串。它看不到对
行的更改。要么构造一个新的字符串流,要么强制它获取字符串的新副本。

您也可以通过使用两个while循环来实现这一点:-/

while ( getline(file, line))
{
    istringstream iss(line);

    while(iss >> term)
    {
        cout << term<< endl; // typing all the terms
    }
}
while(getline(文件,行))
{
istringstream iss(线);
while(国际空间站>>术语)
{

cout此代码片段使用单个循环提取令牌

#include <iostream>
#include <fstream>
#include <sstream>

int main(int argc, char **argv) {

    if(argc != 2) {
        return(1);
    }

    std::string file = argv[1];
    std::ifstream fin(file.c_str());

    char i;
    int j, k;
    std::string line;
    std::istringstream iss;
    while (std::getline(fin, line)) {
        iss.clear();
        iss.str(line);
        iss >> i >> j >> k;
        std::cout << "i=" << i << ",j=" << j << ",k=" << k << std::endl;
    }
    fin.close();
    return(0);
}
#包括
#包括
#包括
int main(int argc,字符**argv){
如果(argc!=2){
申报表(1);
}
std::string file=argv[1];
std::ifstream fin(file.c_str());
char i;
int j,k;
std::字符串行;
std::istringstream iss;
while(std::getline(fin,line)){
iss.clear();
国际空间站str(直线);
iss>>i>>j>>k;

std::cout我希望不是这样,但我在循环中添加了iss2,这样它每次都会重新初始化,效果很好。感谢您在使用
iss.str(line)
设置新字符串后始终调用
iss.clear()
,这一点很重要