qq_19983611
jiaaoyang
2018-11-27 10:34

关于c语言中fscanf的读取的问题,求大佬们帮下

  • c

1.在使用fscanf读取文件中的数据时报错图片说明
2.具体代码中fscanf中输入列表中使用的是指针。
3.具体代码附上

#include "stdio.h"
#include "string.h"
void RecordOutPut(int (*studentScore)[9],char (*studentName)[10],int studentnumber);
int  menuChoose(int (*pstudentScore)[9],char (*pstudentName)[10],int studentNumber);
void inputMenu(int a);
void inputRecord(int (*pstudentScore)[9],char (*pstudentName)[10],int studentNumber);
void fgetScore(int (*pstudentScore)[9],char (*pstudentName)[10],int studentNumber);
void main()
{
    int studentNumber=30;
    int (*pstudentScore)[9];
    char (*pstudentName)[10];
    int menu=1;
    while(menu==1)
    {
        menu=menuChoose(pstudentScore,pstudentName,studentNumber);
    }

}
int menuChoose(int (*pstudentScore)[9],char (*pstudentName)[10],int studentNumber)
{
    int n=0;
    int i=0;
    int menuNumber=0;
    printf("Please Choose The Operation.\n");
    printf("1.Input record\n");
    printf("2.Caculate total and average score of course\n");
    printf("3.Sort in descending order by score\n");
    printf("4.Sort in ascending order by number\n");
    printf("5.Search by number\n");
    printf("6.Statistic analysis\n");
    printf("7.Lise record\n");
    printf("0.Exit\n");
    printf("please enter your choice:");
    scanf("%d",&menuNumber);
    switch(menuNumber)
    {
        case 0:
            return 0;
            break;
        case 1:
            inputRecord(pstudentScore,pstudentName,studentNumber);
            return 1;
            break;
        case 2:
            fgetScore(pstudentScore,pstudentName,studentNumber);
            for(n=0;n<studentNumber;n++)
            {
                printf("%10s",*(pstudentName+n));
                for(i=0;i<=8;i++)
                {
                printf("\n%d",*(*(pstudentScore+n)+i));
                }
            }
            return 1;
            break;
        case 3:
            return 1;
            break;
        case 4:
            return 1;
            break;
        case 5:
            return 1;
            break;
        case 6:
            return 1;
            break;
        case 7:
            return 1;
            break;
        default:
            return 0;
            break;
    }
}
void inputRecord(int (*pstudentScore)[9],char (*pstudentName)[10],int studentNumber)
{
    int studentScore[30][9];
    char studentName[30][10];
    int n=0;
    int j=0;
    int k=0;
    int i=0;
    pstudentScore=studentScore;
    pstudentName=studentName;
    printf("Please input the student's count:");
    scanf("%d",&studentNumber);
    printf("\n");
    for(n=0;n<studentNumber;n++)
    {
        printf("请输入第%d个学生的姓名:",n+1);
        scanf("%s",studentName[n]);
        //printf("%s",*(pstudentName+n));
        for(j=0;j<=6;j++)
        {
            printf("请输入第%d个学生的",n+1);
            inputMenu(j);
            printf("成绩:");
            scanf("%d",&studentScore[n][j]);
            printf("\n");
        }
        studentScore[n][7]=studentScore[n][0]+studentScore[n][1]+studentScore[n][2]+studentScore[n][3]+studentScore[n][5]+studentScore[n][4];
        studentScore[n][8]=(double)studentScore[n][7]/6;
    }
    /*for(n=0;n<studentNumber;n++)
    {
        for(j=0;j<=8;j++)
        {
            printf("%d",studentScore[n][j]);
        }
        printf("\n");
    }*/
    RecordOutPut(pstudentScore,pstudentName,studentNumber);
}
void RecordOutPut(int (*studentScore)[9],char (*studentName)[10],int studentnumber)
{
    int n=0;
    int i=0;
    int j=0;
    int *score;
    int *name;
    char FileNameM[10];
    FILE *studentRecordOutPut;
    for(n=0;n<=9;n++)
    {
        FileNameM[n]=' ';
    }
    for(n=0;n<studentnumber;n++)
    {
        printf("%10s\n",*(studentName+n));
    }
    /*for(n=0;n<studentnumber;n++)
    {
        printf("%s",*(studentName+n));
        for(i=0;i<=8;i++)
        {
         printf("\n%d",*(*(studentScore+n)+i));
        }
    }*/
    printf("Please input the name of u want keep these record:");
    scanf("%s",&FileNameM);
    studentRecordOutPut=fopen(FileNameM,"w+");
    fprintf(studentRecordOutPut,"姓名    数学 语文 英语 物理 化学 生物 学号 总分 平均分\n");
    for(n=0;n<studentnumber;n++)
    {
    fprintf(studentRecordOutPut,"%-7s",*(studentName+n));
        for(i=0;i<=8;i++)
        {
            fprintf(studentRecordOutPut,"%5d",*(*(studentScore+n)+i));
        }
    fprintf(studentRecordOutPut,"\n");
    }
    fclose(studentRecordOutPut);
}
void inputMenu(int a)
{
    switch(a)
    {
        case 0:
            printf("数学");
            break;
        case 1:
            printf("语文");
            break;
        case 2:
            printf("英语");
            break;
        case 3:
            printf("物理");
            break;
        case 4:
            printf("化学");
            break;
        case 5:
            printf("生物");
            break;
        case 6:
            printf("学号");
            break;
    }
}
void fgetScore(int (*pstudentScore)[9],char (*pstudentName)[10],int studentNumber)
{
    FILE *studentScoreRead;
    int n=0;
    int i=0;
    int j=0;
    int k=0;
    char fileAdress[100];
    scanf("%s",fileAdress);
    studentScoreRead=fopen(fileAdress,"a+");
    for(j=0,n=0;n<studentNumber;n++,j++)
    {
        fscanf(studentScoreRead,"%s",*(pstudentName+n));
        for(i=0;i<=8;i++)
        {
            fscanf(studentScoreRead,"%5d",*(*(pstudentScore+n)+i));
        }
    }
    fclose(studentScoreRead);
}
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答