GRASS 8 Programmer's Manual
8.6.0dev(2026)-1878fdfec5
Loading...
Searching...
No Matches
copy_file.c
Go to the documentation of this file.
1
/****************************************************************************
2
*
3
* MODULE: GRASS library - copy_file.c
4
* AUTHOR(S): Paul Kelly
5
* PURPOSE: Function to copy one file to another.
6
* SPDX-FileCopyrightText: 2007 GRASS Development Team
7
* SPDX-License-Identifier: GPL-2.0-or-later
8
*
9
*****************************************************************************/
10
11
#include <
stdio.h
>
12
#include <errno.h>
13
#include <
string.h
>
14
15
#include <
grass/gis.h
>
16
17
/**
18
* \brief Copies one file to another
19
*
20
* Creates a copy of a file. The destination file will be overwritten if it
21
* already exists, so the calling module should check this first if it is
22
* important.
23
*
24
* \param infile String containing path to source file
25
* \param outfile String containing path to destination file
26
*
27
* \return 1 on success; 0 if an error occurred (warning will be printed)
28
**/
29
30
int
G_copy_file
(
const
char
*
infile
,
const
char
*outfile)
31
{
32
FILE
*
infp
, *
outfp
;
33
int
inchar
,
outchar
;
34
35
infp
=
fopen
(
infile
,
"r"
);
36
if
(
infp
==
NULL
) {
37
G_warning
(
"Cannot open %s for reading: %s"
,
infile
,
strerror
(
errno
));
38
return
0;
39
}
40
41
outfp
=
fopen
(outfile,
"w"
);
42
if
(
outfp
==
NULL
) {
43
G_warning
(
"Cannot open %s for writing: %s"
, outfile,
strerror
(
errno
));
44
fclose
(
infp
);
45
return
0;
46
}
47
48
while
((
inchar
=
getc
(
infp
)) !=
EOF
) {
49
/* Read a character at a time from infile until EOF
50
* and copy to outfile */
51
outchar
=
putc
(
inchar
,
outfp
);
52
if
(
outchar
!=
inchar
) {
53
G_warning
(
"Error writing to %s"
, outfile);
54
fclose
(
infp
);
55
fclose
(
outfp
);
56
return
0;
57
}
58
}
59
fflush
(
outfp
);
60
61
fclose
(
infp
);
62
fclose
(
outfp
);
63
64
return
1;
65
}
NULL
#define NULL
Definition
ccmath.h:32
AMI_STREAM
Definition
ami_stream.h:153
G_copy_file
int G_copy_file(const char *infile, const char *outfile)
Copies one file to another.
Definition
copy_file.c:30
G_warning
void G_warning(const char *,...) __attribute__((format(printf
gis.h
stdio.h
string.h
lib
gis
copy_file.c
Generated on Wed Sep 9 2026 06:57:42 for GRASS 8 Programmer's Manual by
1.9.8