Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


output file or fout question (online waiting)

Posted on 2004-10-28
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 39

Accepted Solution

itsmeandnobodyelse earned 1000 total points
ID: 12437655
>>> 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

ID: 12438389
Thanks Alex, I am testing now. It seems working

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

636 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