Input line by line from an input file and tokenize using strtok() and the output into an output file

前端 未结 2 1643
轻奢々
轻奢々 2020-12-14 03:41

What I am trying to do is to input a file LINE BY LINE and tokenize and output into an output file.What I have been able to do is input the first line in the file but my pro

2条回答
  •  生来不讨喜
    2020-12-14 04:24

    You are using C runtime library when C++ makes this neater.

    Code to do this in C++:

    ifstream in;     //delcraing instream
    ofstream out;    //declaring outstream
    
    string oneline;
    
    in.open("infile.txt");  //open instream
    out.open("outfile.txt");  //opens outstream
    while(getline(in, oneline))
    {
        size_t begin(0); 
        size_t end;
        do
        {
            end = oneline.find_first_of(" ,", begin);
    
            //outputs file into copy file
            out << oneline.substr(begin, 
                        (end == string::npos ? oneline.size() : end) - begin) << ' ';
    
            begin = end+1;
            //pointer moves to second token after first scan is over 
        }
        while (end != string::npos);
    }
    
    in.close();      //closes in file
    out.close();      //closes out file
    

    Input:

    a,b c

    de fg,hijklmn

    Output:

    a b c de fg hijklmn

    If you want newlines, add out << endl; at the appropriate point.

提交回复
热议问题