output file or fout question (online waiting)

Posted on 2004-10-28
Last Modified: 2010-04-01
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]

BUT I DONT KNOW HOW TO MIX 01 and 02 records like example output above.
Question by:justinY
    LVL 39

    Accepted Solution

    >>> 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;

    Regards, Alex

    Author Comment

    Thanks Alex, I am testing now. It seems working

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One C++ Help from Top Experts

    Solve your toughest problems, fast.
    C++ experts are online now and ready to help you.

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    Some Windows API functions expect you to provide a pointer to a CALLBACK function that the system will need to call as part of the operation.  Such API functions as SetTimer, timeSetEvent, CreateThread, EnumWindows, LineDDA, even window message hand…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now