懵逼树上的我

导航

2019年春季学期第二周作业

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

 

    #include<stdio.h>
    #include<stdlib.h>
    int main (void)
    {
    FILE * fp;
    int a[10];
    int i,m,b=0;

    if((fp=fopen("D:\\脑壳痛\\yangshengqi.txt","r+"))==NULL)
    {
    printf("File open error!\n");
    exit(0);
    }
    for(i=0; i<6; i++)
    {
    fscanf(fp, "%d%c", &a[i]);
    printf("%d%c", a[i]);
    }
    m = a[0];
    for(i=0; i<6; i++)
    {
    if(m<a[i])
    {
    m = a[i];
    b = i;
    }
    }
    printf("%d %d", m, b);
    fprintf(fp,"%d %d", m, b);
    if(fclose(fp)){
    printf("Can not close the file!\n");
    exit(0);
    }
    return 0;
    }


实验思路:

遇到的问题:

运行时输出一串不知从哪来的数据

解决方法:

打开文件,发现里面储存的数据被改了

运行成功截图:

posted on 2019-03-09 16:38  懵逼树上的我  阅读(123)  评论(1编辑  收藏  举报