output file

Posted on 2004-11-16
Last Modified: 2010-04-01
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;
Question by:justinY
    LVL 23

    Expert Comment

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

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

    LVL 3

    Accepted Solution

    Maybe you can do something like:

            static const int NUM_FILES = 5;
          ifstream filesIn[NUM_FILES];
          ofstream fileOut("fileOut.txt");
          int ii;

          bool CanProceed = true;
          for(ii = 0; ii < NUM_FILES; ++ii)
                      CanProceed = false;
          if(CanProceed && !fileOut == false)
                string strLine;
                      CanProceed = false;
                      for(ii = 0; ii < NUM_FILES; ++ii)
                            getline(filesIn[ii], strLine);
                                  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)
    LVL 1

    Expert Comment

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

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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
    Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
    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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    779 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