Link to home
Start Free TrialLog in
Avatar of justinY
justinY

asked on

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;
......
                     
Avatar of brettmjohnson
brettmjohnson
Flag of United States of America image

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

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

ASKER CERTIFIED SOLUTION
Avatar of Indrawati
Indrawati

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Member_3480671
Member_3480671

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.