Lost_dream_man 2022-06-28 17:32 采纳率: 50%
浏览 12
已结题

fopen读取写入错误怎么回事


#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_DEPRECATE
#include<assert.h>
#include<stdio.h>
#include <string>
using namespace std;
#define size 50
int main()
{
    struct student {
        int nume_student;
        char name_student[20];
        char sex_student;
        int phone_number;
        int score_yuwen;
        float score_math;
        float score_english;
        float score_pinde;
        float total_score;
        int rank;
        char attach[50];
    }stu[size];
    struct num {
        char iuy[100];
        char tyu[100];
    }nty[size];
    int i, j;
    FILE* fp;
    fp = fopen("stu.txt", "r");
    if (fp == NULL)
    {
        printf("文件未能成功打开");

    }
    else
    {
        printf("文件成功打开\n");
    }
    for (i = 0; i < 50; i++)
    {
        fscanf(fp, "%s", nty[i].iuy);
        printf("%s\n", nty[i].iuy);
        fscanf(fp, "%s", stu[i].name_student);
        printf("%s\n", stu[i].name_student);
        fscanf(fp, "%c", stu[i].sex_student);
        printf("%c\n", stu[i].sex_student);
        fscanf(fp, "%s", nty[i].tyu);
        printf("%s\n", nty[i].tyu);
        fscanf(fp, "%d", stu[i].score_yuwen);
        printf("%d\n", stu[i].score_yuwen);
        fscanf(fp, "%f", stu[i].score_math);
        printf("%f\n", stu[i].score_math);
        fscanf(fp, "%f", stu[i].score_english);
        printf("%f\n", stu[i].score_english);
        fscanf(fp, "%f", stu[i].score_pinde);
        printf("%f\n", stu[i].score_pinde);
        fscanf(fp, "%s", stu[i].attach);
        printf("%s\n", stu[i].attach);

    }
    fclose(fp);
    return 0;
}

为什么在第49行会报错?

img

img

  • 写回答

1条回答 默认 最新

  • 快乐鹦鹉 2022-06-28 17:39
    关注

    fscanf(fp, "%d", stu[i].score_yuwen);
    改为
    fscanf(fp, "%d", &stu[i].score_yuwen);
    后面的都有问题,fscanf读入整型,浮点型,字符型时,都需要加&

    fscanf(fp, "%c", stu[i].sex_student);也要改为 fscanf(fp, "%c", &stu[i].sex_student);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 7月6日
  • 已采纳回答 6月28日
  • 创建了问题 6月28日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改