C++ 按行读取数据文件,每行以tab分隔

#include 
#include 
#include 
#include 
using namespace std;

int main()
{
    double temp1, temp2;
    string input;
    ifstream fin;
    bool error;

    fin.open("in.txt");
    error=false;

    while (true)
    {
        getline(fin,input);
        if (!fin) break; //check for eof

        istringstream buffer(input);
        buffer >> temp1 >> temp2;

        //check for non numerical input
        //and less/more input than needed
        if (!buffer || !buffer.eof())
        {
            error=true;
            break;
        }

        //do what you want with temp1 and temp2
        cout << temp1 << '\t' << temp2 << endl;
    }

    if (error)
        cout << "file is corrupted..." << endl;

    cout << "hit enter to quit...";
    cin.get();
    return 0;
}

你可能感兴趣的:(C++)