home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / APPND.ZIP / APPND.C
C/C++ Source or Header  |  1991-06-25  |  3KB  |  112 lines

  1. /***************************************************************************
  2.  
  3.         Program Name  : Appnd.C
  4.         Author        : Rick Winkelspecht
  5.         Date Written  : 24 June 1991
  6.         Compiler      : Borland C++ 2.0
  7.         Language      : C
  8.         Whatsitdo     : This program will input 2 files and append them to
  9.                                         a third.
  10.  
  11.                                         Done as a demonstration only.  Feel Free to use (and
  12.                                         abuse- ahem, improve) this code as you see fit.
  13.                                         Let's call it "I don't care-ware".  It is in the public
  14.                                         domain.  If it does damage to your machine or causes it
  15.                                         to turn into a pumpkin, I take no responsiblity.  I
  16.                                         don't even care if you erase my name from the Author
  17.                                         entry.  Have fun.... Hope this can help others learn
  18.                                         how to copy/append files in C.
  19.  
  20. ****************************************************************************/
  21.  
  22.  
  23.  
  24. #include <stdio.h>
  25. #include <string.h>
  26.  
  27. void give_help(void);                  /* Function Prototype */
  28.  
  29. void main(int argc, char *argv[])      /* argc and argv[] look for command */
  30. {                                      /* line options                     */
  31.     FILE *in1,*in2,*append3;
  32.  
  33.     char file1[80], file2[80], file3[80];
  34.     char ch;
  35.  
  36.     if(argc < 4)                         /* Assume no command line inputs    */
  37.         {
  38.         give_help();
  39.  
  40.         printf("\n\n Input File 1: ");     /* Ask for File names  */
  41.         gets(file1);
  42.  
  43.         printf("\n\n Input File 2: ");
  44.         gets(file2);
  45.  
  46.         printf("\n\n Append-to File 3: ");
  47.         gets(file3);
  48.         }
  49.     else
  50.         {
  51.         strcpy(file1,argv[1]);             /* Use Command Line file names */
  52.         strcpy(file2,argv[2]);
  53.         strcpy(file3,argv[3]);
  54.         }
  55.  
  56. /* Open all files and see if they exist */
  57.  
  58.     if((in1 = fopen(file1,"rb")) == NULL)       /* "rb" is read-binary  */
  59.         {
  60.         printf("Can't open input file %s",file1);
  61.         exit(1);                                  /* Exit Program */
  62.         }
  63.  
  64.     if((in2 = fopen(file2,"rb")) == NULL)
  65.         {
  66.         printf("Can't open input file %s",file2);
  67.         exit(1);                                  /* Exit Program */
  68.         }
  69.  
  70.     if((append3 = fopen(file3,"a+b")) == NULL)  /* w+b is write-append-binary */
  71.         {
  72.         printf("Can't open input file %s",file3);
  73.         exit(1);                                  /* Exit Program */
  74.         }
  75.  
  76.  
  77. /*  Read input file1 and output/append it to file3 */
  78.  
  79.  
  80.     while (!feof(in1))
  81.         {
  82.         ch = fgetc(in1);
  83.         if(ch != EOF)                  /*  Strip out EOF leftovers (ascii 255) */
  84.          fputc(ch, append3);
  85.         }
  86.  
  87.  
  88. /*  Read input file2 and output/append it to file3 */
  89.  
  90.     while (!feof(in2))
  91.         {
  92.         ch = fgetc(in2);
  93.         if(ch != EOF)
  94.             fputc(ch, append3);
  95.         }
  96.  
  97.  
  98. /*  All done now, close all files and quit */
  99.  
  100.     fclose(in1);
  101.     fclose(in2);
  102.     fclose(append3);
  103.  
  104. }
  105.  
  106. void give_help(void)
  107. {
  108.     printf("\n This program works two different ways.\n");
  109.     printf(" It will take input at the command line or will prompt\n");
  110.     printf(" for the input and output files.\n");
  111.     printf("\n Command Line: APPND infile1.txt infile2.txt outfile3.txt\n\n");
  112. }