C语言中的文件操作12.1请编写一个程序,把一个文件的内容复制到另一个文件中。
程序如下:#include<stdio.h>main(){char ch;FILE *fp1;FILE *fp2;if((fp1=fopen("C:\\Users\\acer\\Documents\\1.txt","r"))==NULL){printf("The file 1.txt can not open!");exit(0);}if((fp2=fopen("C:\\Users\\acer\\Documents\\2.txt","w"))==NULL){printf("The file 2.txt can not open!");exit(0);}ch=fgetc(fp1);while(!feof(fp1)){fputc(ch,fp2);ch=fgetc(fp1);}fclose(fp1);fclose(fp2);}运行结果:12.3请编写一个程序,比较两个文件,如果相等则返回0;否则返回1。
程序如下:#include<stdio.h>main(){FILE *f1,*f2;char a,b,c;int x;printf("input strings for file1\n");f1=fopen("file1","w");while((c=getchar())!= EOF)putc(c,f1);fclose(f1);printf("output strings for file1\n");f1=fopen("file1","r");while((c=getc(f1))!= EOF)printf("%c",c);fclose(f1);printf("\n\ninput strings for file2\n");f2=fopen("file2","w");while((c=getchar())!= EOF)putc(c,f2);fclose(f2);printf("\noutput strings for file2\n");f1=fopen("file2","r");while((c=getc(f2))!= EOF)printf("%c",c);fclose(f2);f2=fopen("file2","r");getch();}运行结果:12.4请编写一个程序,将一个文件添加到另一个文件中去。
程序如下:#include<stdio.h>#include<stdlib.h>int main(){FILE *f;system("mkdir D:\\abc\\def");if((f=fopen("D:\\abc\\1.txt","wb"))==NULL){printf("\nopen file error");getchar();exit(1);}fputs("test",f);fclose(f);return 0;}运行结果:打开文件:12.8请编写一个程序,创建一个有序文件,用来存储5种产品信息,这些信息包括产品编码,定价和库存数量,应显示恰当的错误消息。
程序如下:#include<stdio.h>main(){FILE *fp;int number,quantity,i;float price;char item[10];fp=fopen("PRODUCT","w");printf("input product data \n\n");printf("Item name Number Price Quantity\n");for(i=1;i<=5;i++){scanf("%s %d %f %d",item,&number,&price,&quantity);fprintf(fp,"%s %d %.2f %d\n",item,number,price,quantity);}fclose(fp);getch();}运行结果:打开文件:问题一:从一个磁盘文件顺序读入字符并在屏幕上显示。
Answer:#include <stdio.h>#include <stdlib.h>void main(){char ch;FILE *fp;if((fp=fopen("1d.txt","r"))==NULL) //从文件1.txt中读取数据。
{printf("The file can not be read!");exit(0);}ch=fgetc(fp);while(ch!='#'){putchar(ch);ch=fgetc(fp);}fclose(fp);}问题二:从键盘输入一些字符,逐个把它们送到磁盘上去,知道输入一个#为止。
Answer:#include <stdio.h>void main(){char ch;FILE *fp;if((fp=fopen("2.txt","w"))==NULL) //将屏幕中输入的字符输入到2.txt。
printf("The file can not open!");ch=fgetc(stdin);while(ch!='#'){fputc(ch,fp);ch=fgetc(stdin);}fclose(fp);}问题三:将一个磁盘文件中的数据复制到另一个磁盘文件中。
#include <stdio.h>#include <stdlib.h>void main(){char ch;FILE *fp1;FILE *fp2;if((fp1=fopen("1.txt","r"))==NULL) //将文件1.txt中的内容复制到2.txt中。
{printf("The file 1.txt can not open!");exit(0);}if((fp2=fopen("2.txt","w"))==NULL){printf("The file 2.txt can not open!");exit(0);}ch=fgetc(fp1);while(!feof(fp1)){fputc(ch,fp2);ch=fgetc(fp1);}fclose(fp1);fclose(fp2);}若从命令行DOS中运行可将程序修改如下:#include <stdio.h>#include <stdlib.h>void main(int argc,char * args[]) {char ch;FILE *fp1;FILE *fp2;if(argc!=3){printf("The parameters are wrong!");exit(0);}if((fp1=fopen(args[1],"r"))==NULL){printf("The file 1.txt can not open!"); exit(0);}if((fp2=fopen(args[2],"w"))==NULL){printf("The file 2.txt can not open!"); exit(0);}ch=fgetc(fp1);while(!feof(fp1)){fputc(ch,fp2);ch=fgetc(fp1);}fclose(fp1);fclose(fp2);}加入源文件为main.cpp,经编译以后会找到一个main.exe的文件,在命令行中切换到exe 文件所在的目录,并打入main 1.txt 2.txt即可。
可用type 1.txt和type 2.txt检验是否执行。
问题一:从键盘输入4个学生的有关数据,然后把他们转存到磁盘文件中去。
Answer:#include <stdio.h>#include <stdlib.h>struct student_type{char name[10];int num;int age;char addr[30];}stud[4];void save(){FILE *fp;if((fp=fopen("1.txt","wb"))==NULL){printf("The file can not be written!");exit(0);}for(int i=0;i<4;i++)fwrite(&stud[i],sizeof(struct student_type),1,fp);fclose(fp);}void main(){for(int j=0;j<4;j++)scanf("%s%d%d%s",stud[j].name,&stud[j].num,&stud[j].age,stud[j].addr); save();}问题二:将文件1.txt中的数据输出到屏幕上。
Answer:#include <stdio.h>#include <stdlib.h>struct student_type{char name[10];int num;int age;char addr[30];}stud[4];void print(){FILE *fp;if((fp=fopen("1.txt","rb"))==NULL){printf("The file can not be read!");exit(0);}for(int i=0;i<4;i++){fread(&stud[i],sizeof(struct student_type),1,fp);printf("%5s%5d%5d%5s\n",stud[i].name,stud[i].num,stud[i].age,stud [i].addr);}fclose(fp);}void main(){print();}注意:fread和fwrite一般使用于二进制文件的输入和输出。