Thread: Merging multiple text files to a new file

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    7

    Merging multiple text files to a new file

    I am trying to write a code to merge my text files, which are named in
    the form: 0.txt, 1.txt, 2.txt, etc, into a new file. however, I am
    getting a segmentation fault. Any help would be much appreciated (I am
    fairly new to C as well so forgive my naivety)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
     int main()
     {
          int i, x;
          char append[5]=".txt";
          printf("Enter number of files to merge: ");
          gets(x);
          FILE *fs[x], *ft;
          char ch, file[25], filemerge[25];
          printf("Enter name of file which will store contents of files: ");
          gets(filemerge);
          ft = fopen(filemerge,"w");
          for (int i=0; i<x; i++)
          {
                sprintf(file, "%d", i);
                strcat(file[i], append);
                fs[i] = fopen(file,"r");
                while( ( ch = fgetc(fs[i]) ) != EOF )
                fputc(ch,ft);
          } 
          for (i=0; i<x; i++)
          {
                fclose(fs[i]);
          }
          fclose(ft);
          return 0;
     }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by aengland643 View Post
    I am fairly new to C as well so forgive my naivety
    The simple fact that you're not new to English is a relief.

    Your main problem is that you are trying to read an integer using a function that reads strings. (You should have got a warning about that.) To read a number as a number, use scanf:
    Code:
        scanf("%d", &x);
    The problem with scanf is that it leaves the newline in the input buffer, which can be a problem. You can eat the newline like so:
    Code:
        int ch;
        while ((ch = getchar()) != EOF && ch != '\n') ;
    25 spaces is a very small space for a file name. There's no reason not to make it 250 or more.

    gets is a deprecated function due to it's security issues. fgets should be used instead, but it stores the newline character at the end of the string (if there's room). A simplistic way to get rid of the newline if it's there is:
    Code:
        fgets(filemerge, sizeof filemerge, stdin);
        filemerge[strcspn(filemerge, "\n")] = '\0'; // include string.h for strcspn
    You need to check that the files actually opened by testing whether the file pointer returned by fopen is not NULL.

    fgetc returns an int, not a char, so ch should be an int. That way the value of EOF can be different from any possible char value (since an int is larger than a char).

    There's no reason to leave all the input files open until the end. Instead, close each of them as soon as you're done with them. That means that you don't need an array of file pointers for the input files, but just one.

    You don't need a separate strcat to add ".txt". You can do that in the sprintf.
    Code:
            sprintf(file, "%d.txt", i);
    A 6 space tab is strange. 8 is old-fashioned. 4 is normal. People are even using 2 these days.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAXFILENAME 250
    
    int main() {
        int nfiles;
        printf("Enter the number of files to merge: ");
        scanf("%d", &nfiles);
    
        int ch;
        while ((ch = getchar()) != EOF && ch != '\n') ;  // eat the newline
    
        char file_out[MAXFILENAME];
        printf("Enter the name of the output file: ");
        fgets(file_out, MAXFILENAME, stdin);
        file_out[strcspn(file_out, "\n")] = '\0';
    
        FILE *fout = fopen(file_out, "w");
        if (!fout) {
            perror("fopen output file");
            exit(EXIT_FAILURE);
        }
    
        for (int i = 0; i < nfiles; i++) {
            char file_in[MAXFILENAME];
            sprintf(file_in, "%d.txt", i);
    
            FILE *fin = fopen(file_in, "r");
            if (!fin) {
                perror("fopen input file");
                fprintf(stderr, "Problem file: %s\n", file_in);
                exit(EXIT_FAILURE);
            }
    
            while ((ch = fgetc(fin)) != EOF)
                fputc(ch, fout);
    
            fclose(fin);
        } 
    
        fclose(fout);
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O Merging Files Program
    By whiteboyfly in forum C Programming
    Replies: 12
    Last Post: 11-29-2013, 12:47 AM
  2. Reading multiple data from text files
    By Crashie in forum C++ Programming
    Replies: 2
    Last Post: 12-26-2009, 11:37 AM
  3. Help creating multiple text files
    By Tom Bombadil in forum C Programming
    Replies: 19
    Last Post: 03-28-2009, 11:21 AM
  4. Packed Files (Puting multiple files in one file)
    By MrKnights in forum C++ Programming
    Replies: 17
    Last Post: 07-22-2007, 04:21 PM

Tags for this Thread