C++ 是否可以遍历文本文件行并使用stringstream解析每一行?

C++ 是否可以遍历文本文件行并使用stringstream解析每一行?,c++,stringstream,sstream,C++,Stringstream,Sstream,我试图做的是在使用sstream库进行解析时,从每一行的文本文件中读取数据。我让程序运行,但它陷入了一个循环 节目: 想要的输出: 我怎样才能有效地制作这个 不要使用eof循环 逐行读取文件 使用,作为分隔符拆分每行的字符串 创建第二个和第三个字符串的std::stringstream对象,并使用操作符>>从中获取int和double值 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; int main() { ifstream测试文件(“testdates.txt”

我试图做的是在使用sstream库进行解析时,从每一行的文本文件中读取数据。我让程序运行,但它陷入了一个循环

节目:

想要的输出:

我怎样才能有效地制作这个

  • 不要使用
    eof
    循环

  • 逐行读取文件

  • 使用
    作为分隔符拆分每行的字符串

  • 创建第二个和第三个字符串的
    std::stringstream
    对象,并使用
    操作符>>
    从中获取
    int
    double

  • #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    使用名称空间std;
    int main()
    {    
    ifstream测试文件(“testdates.txt”);
    弦线;
    while(getline(testFile,line)){
    字符串日期;
    整数时间;
    浮动金额;
    std::replace(line.begin()、line.end()、'、'、';
    弦流ss(线);
    ss>>日期;
    ss>>时间;
    ss>>数量;
    库特
    
    string date;
    int time;
    float amount;
    
    ifstream testFile("test.txt");
    string token;
    string line;
    
    while(!testFile.eof()) {
    
        while(getline(testFile,token,',')){
            line += token + ' ';
        }
        stringstream ss(line); 
        ss >> date;
        ss >> time;
        ss >> amount;
    
        cout << "Date: " << date << " ";
        cout << "Time: " << time << " ";
        cout << "Amount: " << amount << " ";
        cout<<endl;
    
        ss.clear();
    
    }    
    testFile.close();
    
    10/12/1993,0800,7.97
    11/12/1993,0800,8.97
    
    Date: 10/12/1993 Time: 0800 Amount: 7.97
    Date: 11/12/1993 Time: 0800 Amount: 8.97
    
    #include <algorithm>
    #include <fstream>
    #include <iomanip>
    #include <iostream>
    #include <sstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {    
        ifstream testFile("testdates.txt");    
        string line;
    
        while(getline(testFile, line)){
    
            string date;
            int time;
            float amount;
    
            std::replace(line.begin(), line.end(), ',', ' ');
    
            stringstream ss(line);
    
            ss >> date;
            ss >> time;
            ss >> amount;
    
            cout << "Date: " << date << " ";
            cout << "Time: " << std::setfill('0') << std::setw(4) << time << " ";
            cout << "Amount: " << amount << " ";
    
            cout << '\n';
        }   
    }