Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Merging two files using C program

  1. Sep 21, 2007 #1
    How can i use c program two merge two files. Does it have to do with "pointers" or am going wrong!
  2. jcsd
  3. Sep 21, 2007 #2
    Open file "first" and file "second" in read mode using the fopen function; also open file "merged" in write mode using the fopen function; then read from files "first" and "second" and write what you read to file "merged"; then close all three files and you are done.

    I'm sure there are more details involved that may or may not require pointers, depending on the exact nature of your assignment, but these basic steps do not use them.
  4. Sep 21, 2007 #3
    Thanks for your answer. Just can you please give a me an example of a sample program to open another one because it's the first time i hear about fopen function. But don't worry tell me ill figure it out!
  5. Oct 13, 2007 #4
  6. Oct 16, 2007 #5


    User Avatar
    Gold Member

    What platform are you programming in? You can make use of the linux system calls - open(), read(), and write(). Open() has the O_APPEND flag that opens a file in append mode, thereby adding data to the end of the file. You can use this to merge your two files. Read the manual pages of the respective functions for a detailed description. Then again fopen() does have th "a" flag for append.
    Last edited: Oct 16, 2007
  7. Oct 18, 2007 #6


    User Avatar
    Science Advisor
    Homework Helper

    I don't imagine you're allowed to be so sneaky as to make an OS call, like "copy first+second merged" (DOS./cmd) or "cat first second > merged" (bash)?
  8. Oct 22, 2007 #7
    This is a somewhat 'tedious' but portable way to do it in C.

    Opening the primary file for 'update' rather than 'append' has the effect of removing the EOF marker before appending new data.

    #include <stdio.h>
    #include <stdlib.h>
    #include <io.h>

    #ifdef BYTE
    #undef BYTE

    #define BYTE unsigned char

    BYTE *buf2;
    long size;
    FILE *fp1, *fp2;

    //open file-1 for update, file-2 for read

    //get size of file-2

    //allocate buffer, read-in file-2
    buf2=(BYTE *)malloc((size_t)size);

    //write the buffer to (end of) file-1

    //clean-up and close

    It'd be a good idea to do some error checking if you use this code for anything serious...
  9. Apr 2, 2008 #8

    Give the program to merge two files into a single one ic C Programming
  10. Apr 2, 2008 #9


    User Avatar
    Homework Helper

    Dos command:

    copy filein1/b+filein2/b fileout/b

    If this is a windows console program, you might want to use CreateFile and related commands instead fopen, but the program would be less "portable" to other OS's.
  11. Apr 4, 2008 #10
    Which part of

    did you not understand?
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook