This blog is under construction

Tuesday 30 July 2013

C program to merge alternate lines from two files

Write a C program to merge alternate lines from two files.


  #include <stdio.h>
  #include <string.h>
  #define MAX 256

  int main() {
        FILE *fp1, *fp2, *fp3;
        char line1[MAX], line2[MAX];
        char fname1[MAX], fname2[MAX], temp[] = "temp.txt";

        /* get the first file name from the user */
        printf("Enter your first file name:");
        scanf("%s", fname1);

        /* get the second file name from the user */
        printf("Enter your second file name:");
        scanf("%s", fname2);

        /* open first input file in read mode */
        fp1 = fopen(fname1, "r");

        /* error handling */
        if (!fp1) {
                printf("Unable to open first input file!!\n");
                return 0;
        }

        /* error handling */
        fp2 = fopen(fname2, "r");

        /* unable to open second input file */
        if (!fp2) {
                printf("Unable to open second input file!!\n");
                fclose(fp1);
                return 0;
        }

        /* open temporary file in write mode */
        fp3 = fopen(temp, "w");

        /* error handling */
        if (!fp3) {
                printf("Unable to open temporary file to write!!\n");
                fclose(fp1);
                fclose(fp2);
                return 0;
        }

        /* copying contents of given two input files */
        while ((!feof(fp1)) && (!feof(fp2))) {
                /* copy a line from first input file */
                fgets(line1, MAX, fp1);
                if (!feof(fp1)) {
                        fprintf(fp3, "%s", line1);
                }

                /* copy a line from second input file */
                fgets(line2, MAX, fp2);
                if (!feof(fp2)) {
                        fprintf(fp3, "%s", line2);
                }

                strcpy(line1, "\0");
                strcpy(line2, "\0");
        }

        /* close the opened input files */
        fclose(fp1);
        fclose(fp2);
        fclose(fp3);

        /* remove the given input files */
        remove(fname1);
        remove(fname2);


        /* rename the temporary file */
        rename(temp, fname1);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ cat first.txt 
  That man has reached 
  immortality who is 
  disturbed by nothing material.

  jp@jp-VirtualBox:~/$ cat second.txt 
  The will is not free - it 
  is a phenomenon bound by 
  cause and effect - but there is
  something behind the will which is free.

  jp@jp-VirtualBox:~/$ ./a.out
  Enter your first file name: first.txt
  Enter your second file name: second.txt

  jp@jp-VirtualBox:~/$ cat first.txt 
  That man has reached 
  The will is not free - it 
  immortality who is 
  is a phenomenon bound by 
  disturbed by nothing material.
  cause and effect - but there is
  something behind the will which is free.






SEE ALSO

    1 comment:

    1. Yes this site is very good for programmer.
      some program are written about file operation using c/c++ language you may follow the link.

      http://www.secufoon.com/category/educational-activity/programming/

      ReplyDelete