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