#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*
3.从键盘输入一个字符串,将其中小写字母全部换成大写字母,然后输入到一个磁盘文件test中保存。输入字符串以“!”结束
*/
int main()
{
FILE *fp;
char str[100];
char ch;
if((fp=fopen("E:\\test.txt","w"))==NULL)
{
printf("file cannot open!");
exit(0);
}
ch=getchar();
while(ch!='!')
{
if(ch>='a'&&ch<='z')
ch-=32;
fputc(ch,fp);
ch=getchar();
}
fclose(fp);
fp=fopen("E:\\test.txt","r");
fgets(str,strlen(str)+1,fp);
printf("%s",str);
return 0;
}
/*
4.有两个磁盘文件A和B,各存放一行字母,今要求把这两个文件中的信息合并(按字母顺序排列),输出到新文件C中
*/
int main()
{
FILE *fp;
char str[100],ch;
int i,j,n;
if((fp=fopen("E:\\A.txt","r"))==NULL)
{
printf("file A cannot open!");
exit(0);
}
for(i=0;(ch=fgetc(fp))!=EOF;i++)
{
str[i]=ch;
putchar(ch);
}
putchar(10);
fclose(fp);
j=i;
if((fp=fopen("E:\\B.txt","r"))==NULL)
{
printf("file B cannot open!");
exit(0);
}
for(;(ch=fgetc(fp))!=EOF;j++)
{
str[j]=ch;
putchar(ch);
}
fclose(fp);
putchar(10);
n=j;
for(i=0;i<n;i++)
{
for(j=0;j<n-1-i;j++)
{
if(str[j]>str[j+1])
{
char temp=str[j];
str[j]=str[j+1];
str[j+1]=temp;
}
}
}
printf("fileC:\n");
if((fp=fopen("E:\\C.txt","w"))==NULL)
{
printf("file C cannot open!");
exit(0);
}
for(i=0;i<n;i++)
{
putc(str[i],fp);
putchar(str[i]);
}
fclose(fp);
putchar(10);
return 0;
}
/*
5.有5个学生,每个学生有3门课的成绩,从键盘输入学生数据(学号,姓名,3门课程成绩),计算平均成绩,将原有数据和平均分数放入磁盘文件stud中
*/
#define SIZE 5
struct Student
{
char num[10];
char name[8];
int score[3];
float avg;
}stu[SIZE];
int main()
{
FILE *fp;
int i;
printf("请输入5个学生的学号,姓名,3门成绩:\n");
for(i=0;i<SIZE;i++)
{
scanf("%s%s%d%d%d",stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
stu[i].avg=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0;
}
fp=fopen("E:\\stud.txt","w");
for(i=0;i<SIZE;i++)
{
fwrite(&stu[i],sizeof(struct Student),1,fp);
printf("学号:%8s 姓名:%8s 成绩1:%4d 成绩2:%4d 成绩3:%4d 平均分:%4.2f\n",stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].avg);
}
}
/*
105 Wei 60 59 58 59.00
103 Chen 70 69 68 69.00
102 Wang 80 79 78 79.00
101 Li 98 89 88 91.67
104 Ma 100 99 98 99.00
*/
/*
6.将第5题的"stud"文件中的学生数据,按平均分排序,将已排序的学生数据存入一个新文件"stu-sort"中
*/
#define SIZE 5
struct Student
{
char num[10];
char name[8];
int score[3];
float avg;
}stu[SIZE];
int main()
{
FILE *fp;
int i,j;
if((fp=fopen("E:\\stud.txt","r"))==NULL)
{
printf("file stud.txt cannot open!\n");
exit(0);
}
for(i=0;fread(&stu[i],sizeof(struct Student),1,fp)!=0;i++)
{
printf("\n%8s%8s",stu[i].num,stu[i].name);
for(j=0;j<3;j++)
printf("%4d ",stu[i].score[j]);
printf("%4.2f",stu[i].avg);
}
putchar(10);
fclose(fp);
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE-i-1;j++)
{
if(stu[j].avg>stu[j+1].avg)
{
struct Student temp=stu[j];
stu[j]=stu[j+1];
stu[j+1]=temp;
}
}
}
if((fp=fopen("E:\\stu-sort.txt","w"))==NULL)
{
printf("file stu-sort.txt cannot open!\n");
exit(0);
}
for(i=0;i<SIZE;i++)
{
fwrite(&stu[i],sizeof(struct Student),1,fp);
printf("\n%8s%8s",stu[i].num,stu[i].name);
for(j=0;j<3;j++)
printf("%4d ",stu[i].score[j]);
printf("%4.2f",stu[i].avg);
}
putchar(10);
fclose(fp);
return 0;
}
/*
7.将第6题已排序的学生成绩文件进行插入排序。插入一个学生的3门课程成绩,程序先计算新插入学生的平均成绩,然后将它按成绩的高低顺序插入,插入后建立一个新文件。
*/
//160 Tan 98 97 98
struct Student
{
char num[10];
char name[8];
int score[3];
float avg;
}stu[10];
int main()
{
FILE *fp;
int i,j,k;
struct Student student;
scanf("%s%s%d%d%d",student.num,student.name,&student.score[0],&student.score[1],&student.score[2]);
student.avg=(student.score[0]+student.score[1]+student.score[2])/3.0;
printf("orginal data:\n");
if((fp=fopen("E:\\stu-sort.txt","r"))==NULL)
{
printf("file stu-sort.txt cannot open!\n");
exit(0);
}
for(i=0;fread(&stu[i],sizeof(struct Student),1,fp)!=0;i++)
{
printf("\n%8s%8s",stu[i].num,stu[i].name);
for(j=0;j<3;j++)
printf("%4d ",stu[i].score[j]);
printf("%4.2f ",stu[i].avg);
}
fclose(fp);
for(k=0;stu[k].avg<student.avg&&k<5;k++);
printf("\nnow:\n");
fp=fopen("E:\\stu-sort_1.txt","w");
for(i=0;i<k;i++)
{
fwrite(&stu[i],sizeof(struct Student),1,fp);
printf("\n%8s%8s",stu[i].num,stu[i].name);
for(j=0;j<3;j++)
printf("%4d ",stu[i].score[j]);
printf("%4.2f ",stu[i].avg);
}
printf("\n");
fwrite(&student,sizeof(struct Student),1,fp);
printf("%8s%8s%4d %4d %4d %4.2f ",student.num,student.name,student.score[0],student.score[1],student.score[2],student.avg);
for(i=k;i<5;i++)
{
printf("\n%8s%8s",stu[i].num,stu[i].name);
for(j=0;j<3;j++)
printf("%4d ",stu[i].score[j]);
printf("%4.2f ",stu[i].avg);
}
printf("\n");
fclose(fp);
return 0;
}
/*
11.从键盘输入若干行字符(每行长度不一样),输入后把它们存储到一个磁盘文件里,再从该文件中读入这些数据,将其中的小写字母转换成大写字母后在显示屏上输出
*/
int main()
{
FILE *fp;
char str[100],ch;
int i=0;
if((fp=fopen("E:\\zifu.txt","wb"))==NULL)
{
printf("zifu.txt file cannot open!\n");
exit(0);
}
while(1)
{
printf("请输入字符:");
gets(str);
fputs(str,fp);
fputs("\n",fp);
printf("\ncontinue?(Y/N)");
ch=getchar();
if(ch=='n'||ch=='N')
break;
getchar();
}
fclose(fp);
fp=fopen("E:\\zifu.txt","rb");
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);
}
return 0;
}
C语言文件操作习题汇总
最新推荐文章于 2024-06-25 11:24:47 发布