GRASS GIS 8 Programmer's Manual 8.2.1RC1(2022)-exported
copy_file.c
Go to the documentation of this file.
1
2/****************************************************************************
3 *
4 * MODULE: GRASS GIS library - copy_file.c
5 * AUTHOR(S): Paul Kelly
6 * PURPOSE: Function to copy one file to another.
7 * COPYRIGHT: (C) 2007 by the GRASS Development Team
8 *
9 * This program is free software under the GNU General Public
10 * License (>=v2). Read the file COPYING that comes with GRASS
11 * for details.
12 *
13 *****************************************************************************/
14
15#include <stdio.h>
16#include <errno.h>
17#include <string.h>
18
19#include <grass/gis.h>
20
21/**
22 * \brief Copies one file to another
23 *
24 * Creates a copy of a file. The destination file will be overwritten if it
25 * already exists, so the calling module should check this first if it is
26 * important.
27 *
28 * \param infile String containing path to source file
29 * \param outfile String containing path to destination file
30 *
31 * \return 1 on success; 0 if an error occurred (warning will be printed)
32 **/
33
34int G_copy_file(const char *infile, const char *outfile)
35{
36 FILE *infp, *outfp;
37 int inchar, outchar;
38
39 infp = fopen(infile, "r");
40 if (infp == NULL) {
41 G_warning("Cannot open %s for reading: %s", infile, strerror(errno));
42 return 0;
43 }
44
45 outfp = fopen(outfile, "w");
46 if (outfp == NULL) {
47 G_warning("Cannot open %s for writing: %s", outfile, strerror(errno));
48 fclose(infp);
49 return 0;
50 }
51
52 while ((inchar = getc(infp)) != EOF) {
53 /* Read a character at a time from infile until EOF
54 * and copy to outfile */
55 outchar = putc(inchar, outfp);
56 if (outchar != inchar) {
57 G_warning("Error writing to %s", outfile);
58 fclose(infp);
59 fclose(outfp);
60 return 0;
61 }
62 }
63 fflush(outfp);
64
65 fclose(infp);
66 fclose(outfp);
67
68 return 1;
69}
#define NULL
Definition: ccmath.h:32
int G_copy_file(const char *infile, const char *outfile)
Copies one file to another.
Definition: copy_file.c:34
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:204