求解一道C语言基础编程题
写一个程序,完成文本文件的拷贝,即将当前目录下已经有的file1.txt拷贝一份为file2.txt文件(file2.txt拷贝时不存在)。...
写一个程序,完成文本文件的拷贝,即将当前目录下已经有的file1.txt拷贝一份为file2.txt文件(file2.txt拷贝时不存在)。
展开
1个回答
展开全部
~是缩进符号,自己替换成4个空格:
--------------------------------------------
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
~FILE *fp1, *fp2;
~char ch;
fp1 = fopen("file1.txt", "r");
~if (fp1 == NULL ) {
~~printf("file1.txt open error\r\n");
~~return -1;
~}
fp2 = fopen("file2.txt", "w")
~if (fp2==NULL ) {
~~printf("file2.txt open error\r\n");
~~fclose(fp1);
~~return -1;
~}
~for (ch = fgetc(fp1); ch != EOF; ch = fgetc(fp1)) {
~~fputc(ch, fp2);
~}
~fclose(fp1);
~fclose(fp2);
~return 0;
}
--------------------------------------------
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
~FILE *fp1, *fp2;
~char ch;
fp1 = fopen("file1.txt", "r");
~if (fp1 == NULL ) {
~~printf("file1.txt open error\r\n");
~~return -1;
~}
fp2 = fopen("file2.txt", "w")
~if (fp2==NULL ) {
~~printf("file2.txt open error\r\n");
~~fclose(fp1);
~~return -1;
~}
~for (ch = fgetc(fp1); ch != EOF; ch = fgetc(fp1)) {
~~fputc(ch, fp2);
~}
~fclose(fp1);
~fclose(fp2);
~return 0;
}
更多追问追答
追问
我想问一下,在for语句那里,ch=fgetc(fp)的资料上不是说从文件里获取一个字符赋值给ch吗?第二个ch=fgetc(fp1)又是什么意思呀?
追答
第二个ch=fgetc(fp1)是每次循环执行的啊,这个for循环的写法相当于:
ch = fgetc(fp1);
while (ch != EOF) {
fputc(ch, fp2);
ch = fgetc(fp1);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询