GRASS Programmer's Manual  6.4.2(2012)
copy_file.c
Go to the documentation of this file.
00001 
00002 /****************************************************************************
00003  *
00004  * MODULE:       GRASS GIS library - copy_file.c
00005  * AUTHOR(S):    Paul Kelly
00006  * PURPOSE:      Function to copy one file to another.
00007  * COPYRIGHT:    (C) 2007 by the GRASS Development Team
00008  *
00009  *               This program is free software under the GNU General Public
00010  *               License (>=v2). Read the file COPYING that comes with GRASS
00011  *               for details.
00012  *
00013  *****************************************************************************/
00014 
00015 #include <stdio.h>
00016 #include <errno.h>
00017 #include <string.h>
00018 
00019 #include <grass/gis.h>
00020 
00034 int G_copy_file(const char *infile, const char *outfile)
00035 {
00036     FILE *infp, *outfp;
00037     int inchar, outchar;
00038 
00039     infp = fopen(infile, "r");
00040     if (infp == NULL) {
00041         G_warning("Cannot open %s for reading: %s", infile, strerror(errno));
00042         return 0;
00043     }
00044 
00045     outfp = fopen(outfile, "w");
00046     if (outfp == NULL) {
00047         G_warning("Cannot open %s for writing: %s", outfile, strerror(errno));
00048         return 0;
00049     }
00050 
00051     while ((inchar = getc(infp)) != EOF) {
00052         /* Read a character at a time from infile until EOF
00053          * and copy to outfile */
00054         outchar = putc(inchar, outfp);
00055         if (outchar != inchar) {
00056             G_warning("Error writing to %s", outfile);
00057             return 0;
00058         }
00059     }
00060     fflush(outfp);
00061 
00062     fclose(infp);
00063     fclose(outfp);
00064 
00065     return 1;
00066 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines