文档视界 最新最全的文档下载
当前位置:文档视界 › C语言__第六讲文件操作练习

C语言__第六讲文件操作练习

例10.1 从键盘输入一些字符,逐个把它们送到磁盘上去,直到用户输入一个“#”为止。
#include
#include
int main()
{FILE *fp;
char ch,filename[10];
printf("请输入所用的文件名:");
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL) // 打开输出文件并使fp指向此文件
{
printf("无法打开此文件\n"); // 如果打开时出错,就输出"打不开"的信息
exit(0); // 终止程序*/
}
ch=getchar( ); // 此语句用来接收在执行scanf语句时最后输入的回车符
printf("请输入一个准备存储到磁盘的字符串(以#结束):");
ch=getchar( ); // 接收从键盘输入的第一个字符
while(ch!='#') // 当输入'#'时结束循环
{
fputc(ch,fp); // 向磁盘文件输出一个字符
putchar(ch); // 将输出的字符显示在屏幕上
ch=getchar(); // 再接收从键盘输入的一个字符

}
fclose(fp); // 关闭文件
putchar(10); // 向屏幕输出一个换行符,换行符的ASCII代码为10
return 0;
}
-------------------------------------
例10.2 将一个磁盘文件中的信息复制到另一个磁盘文件中。 今要求将上例建立的file1.dat文件中的内容复制到另一个磁盘文件file2.dat中。
#include
#include
int main( )
{FILE *in,*out;
char ch,infile[10],outfile[10]; // 定义两个字符数组,分别存放两个文件名
printf("输入读入文件的名字:");
scanf("%s",infile); // 输入一个输入文件的名字
printf("输入输出文件的名字:");
scanf("%s",outfile); // 输入一个输出文件的名字
if((in=fopen(infile,"r"))==NULL) // 打开输入文件 change to rb
{printf("无法打开此文件\n");
exit(0);
}
if((out=fopen(outfile,"w"))==NULL) // 打开输出文件 change to rb
{printf("无法打开此文件\n");
exit(0);
}
while(!feof(in)) // 如果未遇到输入文件的结束标志
{ch=fgetc(in); // 从输入文件读入一个字符,暂放在变量ch中
fputc(ch,out); // 将ch写到输出文件中
putchar(ch); // 将ch显示在屏幕上
}
putchar(10); // 显示完全部字符后换行
fclose(in); // 关闭输入文件
fclose(out); // 关闭输出文件
return 0;
}

--------------------------------------
例10.3 从键盘读入若干个字符串,对它们按字母大小的顺序排序,然后把排好序的字符串送到磁盘文件中保存


#include
#include
#include
int main()
{ FILE *fp;
char str[3][10],temp[10]; // str是用来存放字符串的二维数组,temp是临时数组
int i,j,k,n=3;
printf("Enter strings:\n"); // 提示输入字符串 */
for(i=0;igets(str[i]); // 输入字符串
for(i=0;i{k=i;
for(j=i+1;jif(strcmp(str[k],str[j])>0) k=j;
if(k!=i)
{strcpy(temp,str[i]);
strcpy(str[i],str[k]);
strcpy(str[k],temp);}
}
if((fp=fopen("D:\\CC\\string.dat","w"))==NULL) // 打开磁盘文件
{
printf("can't open file!\n");
exit(0);
}
printf("\nThe new sequence:\n");
for(i=0;i{fputs(str[i],fp);fputs("\n",fp); // 撤号
printf("%s\n",str[i]); // 在屏幕上显示字符串
}
return 0;
}
-------------
从文件string.dat中读回字符串,并在屏幕上显示,应如何编写程序?
#include
#include
int main()
{ FILE *fp;
char str[3][10];
int i=0;
if((fp=fopen("D:\\CC\\string.dat","r"))==NULL) // 注意文件名必须与前相同
{
printf("can't open file!\n");
exit(0);
}
while(fgets(str[i],10,fp)!=NULL)
{printf("%s",str[i]);
i++;}
fclose(fp);
return 0;
}

---------------------------------------------------------------------
例10.4 从键盘输入10个学生的有关数据,然后把它们转存到磁盘文件上去。
#include
#define SIZE 10
struct student_type
{char name[10];
int num;
int age;
char addr[15];
}stud[SIZE]; // 定义全局结构体数组stud,包含10个学生数据

void save( ) // 定义函数save,向文件输出SIZE个学生的数据
{FILE *fp;
int i;
if((fp=fopen ("stu.dat","wb"))==NULL) // 打开输出文件atu_list
{printf("cannot open file\n");
return;
}
for(i=0;iif(fwrite (&stud[i],sizeof (struct student_type),1,fp)!=1)
printf ("file write error\n");
fclose(fp);
}

int main()
{int i;
printf("Please enter data of students:\n");
for(i=0;iscanf("%s%d%d%s",stud[i].name,&stud[i].num,&stud[i].age,stud[i].addr);
save( );
return 0;
}
---------------------
#include
#include
#define SIZE 10
struct student_type
{char name[10];
int num;
int age;
char addr[15];
}stud[SIZE];

int main( )
{int i;
FILE *fp;
if((fp=fopen ("stu.dat","rb"))==NULL) // 打开输入文件atu.dat
{printf("cannot open file\n");
exit(0) ;
}
for(i=0;i{fread (&stud[i],sizeof(struct student_type),1,fp); // 从fp指向的文件读入一组数据


printf ("%-10s %4d %4d %-15s\n",stud[i].name,stud[i].num,stud[i]. age,stud[i].addr);
// 在屏幕上输出这组数据
}
fclose (fp); // 关闭文件"stu.dat"
return 0;
}

---------------
#include
#define SIZE 10
struct student_type
{char name[10];
int num;
int age;
char addr[15];
}stud[SIZE]; // 定义全局结构体数组stud,包含10个学生数据

void load( )
{FILE *fp;
int i;
if((fp=fopen("stu_list","rb"))==NULL) // 打开输入文件stu_list
{printf("cannot open infile\n");
return;
}
for(i=0;iif(fread(&stud[i],sizeof(struct student_type),1,fp)!=1) // 从stu_ list文件中读数据
{if(feof(fp))
{fclose(fp);
return;
}
printf("file read error\n");
}
fclose (fp);
}

void save( ) // 定义函数save,向文件输出SIZE个学生的数据
{FILE *fp;
int i;
if((fp=fopen ("stu.dat","wb"))==NULL) // 打开输出文件atu_list
{printf("cannot open file\n");
return;
}
for(i=0;iif(fwrite (&stud[i],sizeof (struct student_type),1,fp)!=1)
printf ("file write error\n");
fclose(fp);
}

int main()
{
load();
save( );
return 0;
}

------------------
例10.5 有一个磁盘文件,内有一些信息。要求第一次将它的内容显示在屏幕上,第二次把它复制到另一文件上。
#include
int main()
{FILE *fp1,*fp2;
fp1=fopen("file1.dat","r"); // 打开输入文件
fp2=fopen("file2.dat","w"); // 打开输出文件
while(!feof(fp1)) putchar(getc(fp1)); // 逐个读入字符并输出到屏幕
putchar(10); // 输出一个换行
rewind(fp1); // 使文件位置指示器返回文件头
while(!feof(fp1)) putc(getc(fp1),fp2); // 从文件头重新逐个读字符,输出到file2文件
fclose(fp1);fclose(fp2);
return 0;
}

--------------------
例10.6 在磁盘文件上存有10个学生的数据。要求将第1,3,5,7,9个学生数据输入计算机,并在屏幕上显示出来。
#include
#include
struct student_type // 学生数据类型
{ char name[10];
int num;
int age;
char addr[15];
}stud[10];

int main()
{ int i;
FILE *fp;
if((fp=fopen("stu.dat","rb"))==NULL) // 以只读方式打开二进制文件
{printf("can not open file\n");
exit(0);
}
for(i=0;i<10;i+=2)
{fseek(fp,i*sizeof(struct student_type),0); // 移动位置指针
fread(&stud[i], sizeof(struct student_type),1,fp); // 读一个数据块到结构体变量
printf("%-10s %4d %4d %-15s\n",stud[i].name,stud[i].num,stud[i].age,stud[i].addr); //

在屏幕输出
}
fclose(fp);
return 0;
}
===========================================================
练习
1. 从键盘输入一个字符串,将其中小写字母转为大写字母,然后输出到一个文件test中,输入的字符串以‘!’结束。
#include
#include
#include
int main ()
{
FILE *fp;
char str[100];
int i=0;
if ((fp=fopen("test","w"))==NULL)
{ printf("can not open file\n");
exit(0);
}
printf("input a string:\n");
gets(str);
while (str[i]!='!')
{if (str[i]>='a'&& str[i]<='z')
str[i]=str[i]-32;
fputc(str[i],fp);
i++;
}
fclose(fp);
fp=fopen("test","r");
fgets(str,strlen(str)+1,fp);
printf("%s\n",str);
fclose(fp);
return 0;
}
2. 两个文件A和B,各存放一行字母,现将两个文件信息合并(按字母顺序排列),输出到新文件c中。
#include
#include
int main ()
{
FILE *fp;
int i,j,n,i1;
char c[100],t,ch;
if ((fp=fopen("a1","r"))==NULL)
{ printf("\ncan not open file\n");
exit(0);
}
printf("file A :\n");
for (i=0;(ch=fgetc(fp))!=EOF;i++)
{
c[i]=ch;
putchar(c[i]);
}
fclose(fp);

i1=i;
if ((fp=fopen("b1","r"))==NULL)
{printf("\ncan not open file\n");
exit(0);
}
printf("\nfile B:\n");
for (i=i1;(ch=fgetc(fp))!=EOF;i++)
{c[i]=ch;
putchar(c[i]);
}
fclose(fp);

n=i;
for (i=0;ifor (j=i+1;jif (c[i]>c[j])
{t=c[i];
c[i]=c[j];
c[j]=t;
}
printf("\nfile C :\n");
fp=fopen("c1","w");
for (i=0;i{putc(c[i],fp);
putchar(c[i]);
}
printf("\n");
fclose(fp);
return 0;
}
-----------------
3.每个学生三门课成绩,从键盘输入学生数据(学号、姓名、3门课成绩),计算平均成绩,将所有数据存放到文件stud中。
#include
struct student
{char num[10];
char name[8];
int score[3];
float ave;
} stu[5];

int main()
{ int i,j,sum;
FILE *fp;
for(i=0;i<5;i++)
{printf("\ninput score of student %d:\n",i+1);
printf("NO.:");
scanf("%s",stu[i].num);
printf("name:");
scanf("%s",stu[i].name);
sum=0;
for (j=0;j<3;j++)
{printf("score %d:",j+1);
scanf("%d",&stu[i].score[j]);
sum+=stu[i].score[j];
}
stu[i].ave=sum/3.0;
}

/*将数据写入文件*/
fp=fopen("stud","wb");
for (i=0;i<5;i++)
if (fwrite(&stu[i],sizeof(struct student),1,fp)!=1)
printf("file write error\n");
fclose(fp);

fp=fopen("stud","r");
for (i=0;i<5;i++)
{fread(&stu[i],sizeof(struct student),1,fp);
printf("\n%s,%s,%d,%d,%d,%6.2f\n",stu[i].num,stu[i].name,stu[i].score[0],
stu[i].score[1],stu[i].score[2],stu[i].ave);}
return 0;
}
-------------
4. 讲第三题文件中学生数据按平均分排序处理,新的顺序存入文件stuout中。
#include
#include <

stdlib.h>
#define N 10
struct student
{char num[10];
char name[8];
int score[3];
float ave;
} st[N],temp;

int main()
{FILE *fp;
int i,j,n;

/*读文件*/
if ((fp=fopen("stud","r"))==NULL)
{printf("can not open.\n");
exit(0);
}
printf("File 'stud': ");
for (i=0;fread(&st[i],sizeof(struct student),1,fp)!=0;i++)
{printf("\n%8s%8s",st[i].num,st[i].name);
for (j=0;j<3;j++)
printf("%8d",st[i].score[j]);
printf("%10.2f",st[i].ave);
}
printf("\n");
fclose(fp);
n=i;

/*排序*/
for (i=0;ifor (j=i+1;jif (st[i].ave < st[j].ave)
{temp=st[i];
st[i]=st[j];
st[j]=temp;
}

/*输出*/
printf("\nNow:");
fp=fopen("stu_sort","w");
for (i=0;i{fwrite(&st[i],sizeof(struct student),1,fp);
printf("\n%8s%8s",st[i].num,st[i].name);
for (j=0;j<3;j++)
printf ("%8d",st[i].score[j]);
printf("%10.2f",st[i].ave);
}
printf("\n");
fclose(fp);
return 0;
}

------------------
5.对第四题排好序的文件做插入处理。
#include
#include
struct student
{char num[10];
char name[8];
int score[3];
float ave;
} st[10],s;

int main()
{FILE *fp,*fp1;
int i,j,t,n;
printf("\nNO.:");
scanf("%s",s.num);
printf("name:");
scanf("%s",https://www.docsj.com/doc/f94164488.html,);
printf("score1,score2,score3:");
scanf("%d,%d,%d",&s.score[0],&s.score[1],&s.score[2]);
s.ave=(s.score[0]+s.score[1]+s.score[2])/3.0;

/*从文件读数据*/
if((fp=fopen("stu_sort","r"))==NULL)
{printf("can not open file.");
exit(0);
}
printf("original data:\n");
for (i=0;fread(&st[i],sizeof(struct student),1,fp)!=0;i++)
{printf("\n%8s%8s",st[i].num,st[i].name);
for (j=0;j<3;j++)
printf("%8d",st[i].score[j]);
printf("%10.2f",st[i].ave);
}

n=i;
for (t=0;st[t].ave>s.ave && t
/*向文件写数据*/
printf("\nNow:\n");
fp1=fopen("sort1.dat","w");
for (i=0;i{fwrite(&st[i],sizeof(struct student),1,fp1);
printf("\n %8s%8s",st[i].num,st[i].name);
for (j=0;j<3;j++)
printf("%8d",st[i].score[j]);
printf("%10.2f",st[i].ave);
}
fwrite(&s,sizeof(struct student),1,fp1);
printf("\n %8s %7s %7d %7d %7d%10.2f",s.num,https://www.docsj.com/doc/f94164488.html,,s.score[0],
s.score[1],s.score[2],s.ave);

for (i=t;i{fwrite(&st[i],sizeof(struct student),1,fp1);
printf("\n %8s%8s",st[i].num,st[i].name);
for(j=0;j<3;j++)
printf("%8d",st[i].score[j]);
printf("%10.2f",st[i].ave);
}
printf("\n");
fclose(fp);
fclose(fp1);
return 0;
}
----------------------------------------------
6.从键盘输入若干行字符,存入一个文件中,再从该文件中读出这些数据,如有小写字母,转换为大写,显示输出。
#include
int main()
{ int i,flag;
char str[80],c;
FILE *f

p;
fp=fopen("text","w");
flag=1;
while(flag==1)
{printf("input string:\n");
gets(str);
fprintf(fp,"%s ",str);
printf("continue?");
c=getchar();
if ((c=='N')||(c=='n'))
flag=0;
getchar();
}
fclose(fp);
fp=fopen("text","r");
while(fscanf(fp,"%s",str)!=EOF)
{for (i=0;str[i]!='\0';i++)
if ((str[i]>='a') && (str[i]<='z'))
str[i]-=32;
printf("%s\n",str);
}
fclose(fp);
return 0;
}


相关文档