2019年春季学期第二周作业 基础作业 请在第一周作业的基础上,继续完成:找出给定的文件中数组的最大值及其对应的最小下标(下标从0开始)。并将最大值和对应的最小下标数值写入文件。 输入: 请建立以自己英文名字命名的txt文件,并输入数组元素数值,元素值之间用逗号分隔。 输出 在不删除原有文件内容的情况下,将最大值和对应的最小下标数值写入文件

~~~

include<stdio.h>

include<stdlib.h>

int main()
{
FILE*fp;
int i=0,max=0,j=0,maxb=0;
int b[10];
if((fp=fopen("D:\jiajia.txt","r+"))==NULL)
{
printf("File open error!\n");
exit(0);
}
for (i=0;i<10;i++)
{
fscanf(fp,"%d",&b[i]);
max=0;
}
for(j=0;j<10;j++)
if( b[j]>max){
max=b[j];

 maxb=j;

}

 printf("%d %d",max,maxb);
 
 fprintf(fp,"\n%d %d",max,maxb);
 
 if (fclose (fp)) {
 printf ("Can not close the file! \n");
exit (0); 

}
return 0;
}
~~~

猜你喜欢

转载自www.cnblogs.com/jiajia2333/p/10502677.html