用标准C++读取固定格式的文本文件

文件格式:

1,tlj,1,20,21,30
2,cw,31,40
3,ss,41,50,51,60
4,sw,61,70

相关代码:

//---------------------------------------------------------------------------

#include
#include
#include
#include
#include
#include
#include
#include
#include
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
using namespace std;
int main(int argc, char* argv[])
{
    ifstream fs("c://1.txt", ios_base::in);
    string line;
    istringstream stream;
    string temp;
    vector infos;
    while(fs>>line)
    {
        infos.clear();
        replace(line.begin(), line.end(), ',', ' ');
        stream.str(line);
        while(stream >> temp)
        {
            infos.push_back(temp);
        }
        if (!stream)
        {
            stream.clear();
            stream.sync();
        }
        copy(infos.begin(), infos.end(), ostream_iterator(cout, ","));
        cout << endl;
    }
    system("pause");
    return 0;
}
//---------------------------------------------------------------------------

提示: c++语言的输入默认是以空格为分隔符的

你可能感兴趣的:(STL)