merging two files and create a new file in C.

September 6, 2010 at 05:49:35
Specs: Windows XP
I need to merge two files and create a new file in c language.
my 1st file containing
abc
def
ghi
my 2nd file containing
123
456
789
I need the output like
abc
123
def
456
ghi
789

can anyone code for this in c? Thanks in advance.


See More: merging two files and create a new file in C.

Report •


#1
September 6, 2010 at 07:10:28
This is not do_your_homework_for_free.com
The only way you will learn programming is to do it.
If you have a specific question you may ask.
No one will write the code for you.


Report •

#2
September 6, 2010 at 23:00:05
If they're plain text files, then a very basic approach would be to create three FILE vars - the two input files and the output file and a couple of input file char[] buffers.

To merge, read both input files at the "same" time; you can use either fgets or fscanf; however, fgets will store any '\n' chars, which may affect the output file. To validate either, test fgets against being NULL and fscanf returns the number of successfully converted items.

while(input1 is valid && input2 is valid) {
   fprintf(output, "%s\n%s\n", in_buf1, in_buf2);
}

If there is a requirement to pick up any "leftovers", just read the two input file in succession after reading them both...

while(input1 is valid) {
   write output file using in_buf1
}

while(input2 is valid) {
   write output file using in_buf2
}

HTH - give it a shot. :)

-------------------------------------------
I have no aspiration for preaching. ;-)


Report •

Related Solutions


Ask Question