combining file contents with command line

This is a discussion on combining file contents with command line within the C Programming forums, part of the General Programming Boards category; I am trying to make a program that copies the contents of any number of files into one big file. ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    73

    combining file contents with command line

    I am trying to make a program that copies the contents of any number of files into one big file. No breaks or anything needs to exist in between the contents of the files. I have a good working start, but it will only copy the contents of the first file I designate and not the rest. Also, at the end of the file, it puts this weird y looking character on the end. I am guessing my problem lies in the fgetc and fputc functions and probably the way I have the loop set up to copy and paste the characters as a whole. Anyways, I point in the right direction would be nice.

    Code:
    //the program basically copies the contents of files designated in the command line argument into one big file
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
    
    FILE *concat, *fp;
    char c;
    int n;
    
    concat = fopen("concat.txt","w");
    
    for(n=1; argv[n] != NULL; n++){      //loops through all files specified in command line argument
      if(fopen(argv[n], "r") == NULL){     //if the file cant be opened  (aka NULL) then satte file cant be opened
        printf("Error. File ""%s"" cant be opened.\n", argv[n]);
        exit(0);
      }
      else{     //if the file can be opened, then open it in read mode
        fp=fopen(argv[n], "r");
        while(c != EOF){   //while the file isn't at the end, EOF, copy each character into c then put c into concat
          c = fgetc (fp);
          fputc(c, concat);
        }
        fclose(fp);
      }
    }
    
    fclose(concat);
    
          system("PAUSE");
          return 0;
    }

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    try something like this:
    Code:
    for (n=1;n<argc;n++) {
          if ((fp = fopen(argv[n],"rt")) == NULL) {
                printf("Unable to open %s, skipping it...\n",argv[n]);
                continue;
          }
          else {
                while ((c=fgetc(fp) != EOF)
                      fputc(c,concat);
    
                 fclose(fp);
          }
    }

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    That makes a lot more sense. Thank Salem. nonpuz, your fix helped too.

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    i overlooked that, heh.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21