| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- int main( int ac , char *av[] )
- {
- // print the help flag thing
- if ( (ac < 2 ) || ( ! strcmp (av[1], "--help") ) ) {
- printf ("%s\n", "This program appends a source file to a destination file.");
- printf ("%s\n", "syntax:");
- printf ("\t%s\n", "append DESTINATION SOURCE");
- } else {
- if (ac < 3) {
- printf ("%s\n", "Destination file must be provided.");
- printf ("%s\n", "See 'append --help'.");
- return 1;
- } else {
- // av[1] is destination
- // av[2] is source
- FILE *destination, *source;
- // open the source file
- source = fopen (av[2], "r");
- // error if there is no source
- if ( !source ) {
- printf ("Error: Source file '%s' not found.\n", av[2]);
- return 1;
- }
- // buffer for reading
- char buffer[128];
- // open (or create) the destination file
- destination = fopen (av[1], "a");
- // read into buffer and write to file
- while (fgets (buffer, sizeof (buffer), source)) {
- fprintf (destination, "%s", buffer);
- }
- // clean up
- fclose (source);
- fclose (destination);
- }
- }
- return 0;
- }
|