#include<stdio.h>
#include<stdlib.h>
int main(void)
{
FILE *fp1;
FILE *fp2;
int ch;
char filename1[30];
sprintf(filename1, "%s%s", "hello", ".txt");
fp1 = fopen(filename1, "w");
fprintf(fp1, "%s", "i love you!");
fclose(fp1);
/*此处如果没有fclose,数据留在缓冲区中,虽然可以成功写入,但是无法成功读取*/
char filename2[30];
sprintf(filename2, "%s%s", "fishc", ".txt");
if ((fp1 = fopen("hello.txt","r")) == NULL)
{
printf("文件打开失败!\n");
exit(EXIT_FAILURE);
}
if ((fp2 = fopen(filename2,"w")) == NULL)
{
printf("文件打开失败!\n");
exit(EXIT_FAILURE);
}
while ((ch = fgetc(fp1)) != EOF)
{
fputc(ch,fp2);
}
fclose(fp1);
fclose(fp2);
return 0;
}
总而言之,写了fopen一定要写fclose,不然真的不安全~