}中的位置(即记录号),并向文件t2.dat输出该记录号。 //the number is:88 #include \"stdio.h\" #include \"stdlib.h\" #include \"math.h\" void main() {FILE *fp,*fp1; double num; int i=0;
if((fp=(fopen(\"d:\\\\12345678\\\\dat2.dat\{
printf(\"cannot open the file!\\n\"); exit(0); }
if((fp1=(fopen(\"d:\\\\12345678\\\2.dat\{
printf(\"cannot open the file!\\n\"); exit(0); }
while(feof(fp)==0) {
fscanf(fp,\"%6lf\\n\ i++; }
fclose(fp);
22 / 30
if(num==2.58) {
fprintf(fp1,\"the number is:%d\printf(\"the num is:%d\break; }
fclose(fp1); }
34C2. 已知在正文文件da1.dat中,每个纪录只有两项数据,第一项为一整数表示学生的
学号,第二项为形如xx.x的一个实数,试统计计算并向文件t2.dat输出全部学生的平均成绩V与90分以上(含90分)的学生人数N。
//average= 65.5,>90=72 #include \"stdio.h\" #include \"stdlib.h\" struct stu{ int num; float score; }stud; void main() {
FILE *fp,*fp1; int N=0,n=0; float sum=0,V=0;
if((fp=fopen(\"D:\\\\12345678\\\\da1.dat\{
printf(\"cannot open the file!\\n\"); exit(0); }
if((fp1=fopen(\"D:\\\\12345678\\\2.dat\{
printf(\"cannot open the file!\\n\"); exit(0); }
while(feof(fp)==0) { } V=sum/n;
fprintf(fp1,\"average=%5.1f,>90=%d\fclose(fp); fclose(fp1); }
35C3. 已知在正文文件ch1.dat中, 每个记录的数据是有一个由字母组成的字符个数不多
23 / 30
fscanf(fp,\"%4d %5f\\n\printf(\"%4d %5.1f\\n\sum+=stud.score; n++;
if(stud.score>=90)
N++;
于10个的字符串,如:″absolute″。试查找文件中从第150个记录到第250个记录间出现的第一个最大的字符串,并向文件t2.dat输出查找结果和该字符串中的字符个数n。
// str=mwnhkkevej,num=10#include \"stdio.h\"
#include \"stdlib.h\" void main() {
FILE *fp,*fp1; char ch[10],str[10]; int n=0,i=0,num=0;
if((fp=fopen(\"D:\\\\ 12345678\\\\ch1.dat\{
printf(\"cannot open the file!\\n\"); exit(0); }
if((fp1=fopen(\"D:\\\\12345678\\\2.dat\{
printf(\"cannot open the file!\\n\"); exit(0); }
while(feof(fp)==0) { }
fprintf(fp1,\"str=%s,num=%d\fclose(fp); fclose(fp1); }
n++;
fscanf(fp,\"%s\\n\//printf(\"%s\\n\if(n>=150&&n<=250)
{for(i=0;i<10;i++) }
if(ch[i]=='\\0') break; { num=i;
for(i=0;i<=num;i++) str[i]=ch[i]; } if(num36C4. 已知在正文文件ch1.dat中, 每个记录的数据是有一个由字母组成的字符个数不多于10个的字符串,如:″absolute″。统计在该文件中只有4个字符的字符串的个数n1和
24 / 30
字符串的最后一个字符是f的字符串的个数n2,并将统计结果存入文件t2.dat中。
// n1=54,n2=17 #include \"stdio.h\" #include \"stdlib.h\" void main() {
FILE *fp,*fp1; char ch[10]; int n1=0,n2=0,i=0;
if((fp=fopen(\"D:\\\\12345678\\\\ch1.dat\{
printf(\"cannot open the file!\\n\"); exit(0); }
if((fp1=fopen(\"D:\\\\12345678\\\2.dat\{
printf(\"cannot open the file!\\n\"); exit(0); }
while(feof(fp)==0) { }
fprintf(fp1,\"n1=%d,n2=%d\fclose(fp); fclose(fp1); }
fscanf(fp,\"%s\\n\printf(\"%s\\n\for(i=0;i<10;i++) if(ch[i]=='\\0') break; if(i==4) n1++; if(ch[i-1]=='f') n2++;
38C6. 已知在正文文件da1.dat中,每个纪录只有两项数据,第一项为一整数表示学生的学号,第二项为形如xx.x的一个实数,试统计计算并向文件t2.dat输出成绩排序在前20名的学生的平均成绩V。 //average= 99.1 #include \"stdio.h\" #include \"stdlib.h\" void main() {
FILE *fp,*fp1;
25 / 30
int i=0,j=0,k=0,num[1000];
float temp=0,sum=0,V=0,score[1000];
if((fp=fopen(\"D:\\\\12345678\\\\da1.dat\{
printf(\"cannot open the file!\\n\"); exit(0); }
if((fp1=fopen(\"D:\\\\12345678\\\2.dat\{
printf(\"cannot open the file!\\n\"); exit(0); }
while(feof(fp)==0) { } k=i;
for(i=0;iif(score[i]temp=score[i]; score[i]=score[j]; score[j]=temp;fscanf(fp,\"%4d %5f\\n\printf(\"%d %f\\n\i++;
for(i=0;i<20;i++) sum+=score[i]; V=sum/20;
fprintf(fp1,\"average=%5.1f\fclose(fp); fclose(fp1); }
38C6. 已知在正文文件da1.dat中,每个纪录只有两项数据,第一项为一整数表示学生的学号,第二项为形如xx.x的一个实数,试统计计算并向文件t2.dat输出成绩排序在后20名的学生的平均成绩V。
//average= 31.9 #include \"stdio.h\" #include \"stdlib.h\" void main() {
FILE *fp,*fp1;
int i=0,j=0,k=0,num[1000];
26 / 30
float temp=0,sum=0,V=0,score[1000];
if((fp=fopen(\"D:\\\\12345678\\\\da1.dat\{
printf(\"cannot open the file!\\n\"); exit(0); }
if((fp1=fopen(\"D:\\\\12345678\\\2.dat\{
printf(\"cannot open the file!\\n\"); exit(0); }
while(feof(fp)==0) { } k=i;
for(i=0;iif(score[i]>score[j]) { }temp=score[i]; score[i]=score[j]; score[j]=temp;
fscanf(fp,\"%4d %5f\\n\printf(\"%d %f\\n\i++;
for(i=0;i<20;i++) sum+=score[i]; V=sum/20;
fprintf(fp1,\"average=%5.1f\fclose(fp); fclose(fp1); }
35C3. 已知在正文文件ch1.dat中, 每个记录的数据是有一个由字母组成的字符个数不多于10个的字符串,如:″absolute″。试查找文件中从第50个记录到第150个记录间出现的第一个最大的字符串,并向文件t2.dat输出查找结果和该字符串中的字符个数n。
//str=xpkaglnbzz,num=10#include \"stdio.h\"
#include \"stdlib.h\" void main() {
FILE *fp,*fp1; char ch[10],str[10]; int n=0,i=0,num=0;
if((fp=fopen(\"D:\\\\12345678\\\\ch1.dat\
27 / 30
{
printf(\"cannot open the file!\\n\"); exit(0); }
if((fp1=fopen(\"D:\\\\12345678\\\2.dat\{
printf(\"cannot open the file!\\n\"); exit(0); }
while(feof(fp)==0) { }
fprintf(fp1,\"str=%s,num=%d\fclose(fp); fclose(fp1); }
n++;
fscanf(fp,\"%s\\n\//printf(\"%s\\n\if(n>=50&&n<=150)
{for(i=0;i<10;i++) }
if(ch[i]=='\\0') break; { num=i;
for(i=0;i<=num;i++) str[i]=ch[i]; } if(num43C11. 已知 数据文件{}中已存储有50个学生的学号(整型)与一门课程的成绩(实型),编程将该文件中50个学生的学号与成绩分别赋给整型数组num和实型数组grade,计算并输出第21-30名学生的平均分。//avg=68.70 #include \"stdio.h\" #include \"stdlib.h\" struct stu{ int num; float grade; }stud; void main() { FILE *fp; int num[50],i;
28 / 30
float grade[50],avg=0;
if((fp=fopen(\"D:\\\\12345678\\\\dat3.dat\{
printf(\"cannot open the file!\\n\"); exit(0); }
for(i=0;i<50;i++) { }
printf(\"avg=%.2f\fclose(fp); }
fscanf(fp,\"%2d %2f\\n\printf(\"%2d %2.0f\\n\if(i>19&&i<30)
avg+=grade[i];
在文件中写入 * *** ***** ******* ********* ******* ***** *** *
#include #include #include void main() {FILE *p;
char ch,filename[10]; int i,a,j;
printf(\"请输入文件名:\"); scanf(\"%s\
if((p=fopen(\"d:\\\\c语言\\\\file\\\\1\{
printf(\"Cannot open the file!\"); exit(0);
29 / 30
}
ch=getchar(); ch=getchar(); for(a=0;a<2;a++) { for(i=-4;i<=4;i++) { for(j=1;j<10+abs(i);j++) { ch=(' ');fputc(ch,p); putchar(ch);
}
for(j=1;j<=9-abs(i)*2;j++)
{ ch=('*');fputc(ch,p);
putchar(ch);
} ch=('\\n'); fputc(ch,p); putchar(10); } fclose(p);
}
}
30 / 30