output file

Hi, experts

I have 5 files and want to write the outputs into one output file line by line. like the followings:

first line of file1
first line of file2
first line of file3
first line of file4
first line of file5
second line of file1
second line of file2
second line of file3
second line of file4
second line of file5
third line of file1
third line of file2
third line of file3
third line of file4
third line of file5
.....
can i use  the following to do it ? I guess not. if not how can I do it ?
fout << setw(2) << file1.elem1
       << setw(3) << file1.elem2
       << setw(3) << file1.elem3
        << endl;
fout <<  setw(2) << file2.elem1
       <<  setw(2) << file2.elem2
       <<  setw(2) << file2.elem3
        << endl;
......
                     
justinYAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

brettmjohnsonCommented:
You could use the most appropriate tool for the job - in this case, the paste command:

paste -d '\n' file1 file2 file3 file4 file5

0
IndrawatiCommented:
Maybe you can do something like:

        static const int NUM_FILES = 5;
      ifstream filesIn[NUM_FILES];
      filesIn[0].open("file1.txt");
      filesIn[1].open("file2.txt");
      filesIn[2].open("file3.txt");
      filesIn[3].open("file4.txt");
      filesIn[4].open("file5.txt");
      ofstream fileOut("fileOut.txt");
      int ii;

      bool CanProceed = true;
      for(ii = 0; ii < NUM_FILES; ++ii)
      {
            if(!filesIn[ii])
            {
                  CanProceed = false;
                  break;
            }
      }
      if(CanProceed && !fileOut == false)
      {
            string strLine;
            while(CanProceed)
            {
                  CanProceed = false;
                  for(ii = 0; ii < NUM_FILES; ++ii)
                  {
                        getline(filesIn[ii], strLine);
                        if(!filesIn[ii].fail())
                        {
                              fileOut << strLine << '\n';
                        }
                        if(!filesIn[ii] == false) //in case the total line numbers differ between files
                              CanProceed = true;
                  }
            }
      }
      for(ii = 0; ii < NUM_FILES; ++ii)
      {
            filesIn[ii].close();
      }
      fileOut.close();
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Member_3480671Commented:
this is much easier:

FILE *fp0,*fp1,*fp2,*fp3,*fp4;
fp0=fopen("file0.txt","w");          fp1=fopen("file1.txt","w");
fp2=fopen("file2.txt","w");          fp3=fopen("file3.txt","w");
fp4=fopen("file4.txt","w");

fprintf(fp0,"line 1 of file 1");        fprintf(fp1,"line 1 of file 2");
fprintf(fp2,"line 1 of file 3");        fprintf(fp3,"line 1 of file 4");
fprintf(fp4,"line 1 of file 5");

fprintf(fp0,"line 2 of file 1");        fprintf(fp1,"line 2 of file 2");
fprintf(fp2,"line 2 of file 3");        fprintf(fp3,"line 2 of file 4");
fprintf(fp4,"line 2 of file 5");

etc.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

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.