output file or fout question (online waiting)

Hi, experts,
I have a file like this

row_elem[0]                                row_elem[1]                                  row_elem[2]                            row_elem[3]
111                                              222                                                   12                                         333
aaa                                              bbb                                                    0                                          ccc
AAA                                             BBB                                                    11                                         CCC

I want to write output to one file by the following rules:
1. always write 01 records, for all the row_elem first , then
2. if row_elem[2] != 0, then write 02 records next to 01 records.

example output:
01 222333111           ( write 01 records,  always write 01 records)
02 333                      ( write 02 records, because row_elem != 0)
01 bbbcccaaa            ( write 01 records, no 02 records because row_elem[2] = 0)
01 BBBCCCAAA          ( write 01 records, always write 01 records)
02 CCC                     ( write 02 records, because row_elem != 0)

I know how to write 01 and 02 seperately by using fout << setw(2) << "01"
                                                                                <<setw(3) << row_elem[1]
                                                                                 <<setw(3) << row_elem[3]
                                                                                 <<setw(3) << row_elem[0]
                                                                                 <<endl;

BUT I DONT KNOW HOW TO MIX 01 and 02 records like example output above.
ONLINE WAITING. THANKS
justinYAsked:
Who is Participating?
 
itsmeandnobodyelseCommented:
>>> where is the problem?

      string line;
      ifstream ifs("input.txt");
      ofsstream ofs("output.txt");
      while (getline(ifs, line))
      {
            getFields(line, row_elem);
            ofs << "01 " << row_elem[1] << row_elem[3]<<row_elem[0] << endl;
            if (strcmp(row_elem[2], "0") != 0)
                    ofs << "02 " << row_elem[3] << endl;
      }
      ifs.close();
      ofs.close();

Regards, Alex
0
 
justinYAuthor Commented:
Thanks Alex, I am testing now. It seems working
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.