文件复制-字符串读写
#include <stdio.h>
#include <stdlib.h>
#define LEN 100
void copyFileStr(char *source, char *dest);
int main(void)
{
char sourceName[100], destName[100];
printf("input source file name:\n");
scanf("%s", sourceName);
printf("input destination file name:\n");
scanf("%s", destName);
copyFileStr(sourceName, destName);
return 0;
}
void copyFileStr(char *source, char *dest)
{
char buffer[LEN];
FILE *in, *out;
if ((in = fopen(source, "r")) == NULL)
{
fprintf(stderr, "can't open the file \"%s\"\n", source);
exit(1);
}
if ((out = fopen(dest, "w")) == NULL)
{
fprintf(stderr, "can't created the file \"%s\"\n", dest);
exit(2);
}
printf("\nCopying text file %s to file %s by line.\n", source, dest);
while (fgets(buffer, LEN, in) != NULL)
fputs(buffer, out);
if (fclose(in) != 0 || fclose(out) != 0)
fprintf(stderr, "Error in closing files\n");
}