• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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