C语言文件操作的使用
五、实验步骤
- 将一个磁盘文件中的信息复制到另一个磁盘文件中。
#include <stdio.h> #include “ stdlib.h” void main( )
{
FILE*in,*out ; // 定义文件指针
char ch,infile[1 0],outfile[1 0];
printf("Enter the infile name:\n");
scanf("%s",infile);
printf("Enter the outfile name:\n");
scanf("%s",outfile);
if((in=fopen(infile," r"))==NULL)
{ // 判断文件是否正确读操作
printf("cannot open infile\n"); exit(0);
}
if((out=fopen(outfile,"w"))==NULL)
{ // 判断文件是否正确写操作
printf("cannot open outfile\n"); exit(0);
}
//判文件是否结束,如果不结束,则读文件in 的内容写入到文件out 之中
while(!feof(in)) fputc(fgetc(in),out);
fclose(in); //关闭文件
fclose(out);
}
运行情况如下:
Enter the infile name:
file1.c↙ (输入原有磁盘文件名)
Enter the outfile name:
file2.c↙ (输入新复制的磁盘文件名)
程序运行结果是将 fule1. c 文件中的内容复制到 fule2. c 中去。可以用下面命令验证:
c:>type file1.c
computer and c (file1.c中的信息)
c:>type file2.c
computer and c (file2.c 中的信息)
以上程序是按文本文件方式处理的。也可以用此程序来复制一个二进制文件,只需将两个 fopen 函数中的“r” 和 “w”分别改为“rb” 和 “wb” 即可。
分析以上各程序的算法(用框图表示),解释产生该结果现象的相关知识点及实现语句。
希望能帮我解答分析以上各程序的算法(用框图表示),解释产生该结果现象的相关知识点及实现语句。谢谢!