我有下面的简单代码,但是当我在 unix 上使用 GCC 编译和运行时,出现分段错误。是因为文件命名还是将一个文件复制到其他文件。任何帮助表示赞赏..
#include <iostream>
#include <stdio.h>
using namespace std;
void copy(char *infile, char *outfile) {
FILE *ifp; /* file pointer for the input file */
FILE *ofp; /* file pointer for the output file */
int c; /* character read */
/* open i n f i l e for reading */
ifp = fopen (infile , "r" );
/* open out f i l e for writing */
ofp = fopen(outfile, "w");
/* copy */
while ( (c = fgetc(ifp)) != EOF) /* read a character */
fputc (c, ofp); /* write a character */
/* close the files */
fclose(ifp);
fclose(ofp);
}
main()
{
copy("A.txt","B.txt");
}