[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

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
0
justinY
Asked:
justinY
1 Solution
 
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

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now