开天辟地的卷毛 2022-04-26 17:11 采纳率: 85.4%
浏览 27
已结题

c语言有关于文件输入输出的问题

img


#include<stdio.h>
struct score
{
    float score1[10];
    float score2[10];
    float score3[10];
};
struct student
{
    char name[10];
    struct score scoremore;
};
int main()
{
    int i;
    struct student iboy;
    FILE* fp;
    if ((fp = fopen("c:\\f\\stduent.txt", "w")) == NULL)
    {
        printf("open file error!\n");
        exit(0);
    }
    printf("Please enter the student name and three score:\n");
    scanf("%s %.1f", iboy.name,iboy.scoremore.score1,iboy.scoremore.score2,
        iboy.scoremore.score3);
    printf("Please enter the student name and three score again(ctrl+z to quit):\n");
    while (!feof(stdin))
    {
        fprintf("%s %.2f %.2f %.2f", iboy.name, iboy.scoremore.score1,
            iboy.scoremore.score2, iboy.scoremore.score3);
        scanf("%s %.1f", iboy.name, iboy.scoremore.score1, iboy.scoremore.score2,
            iboy.scoremore.score3);
    }
    fclose(fp);
    return 0;
}

想问一下,我这段代码,运行的时候有点问题。好像不能达到题目的要求,该怎么解决

  • 写回答

1条回答 默认 最新

  • kinghero123456 2022-04-26 18:29
    关注
    
    #include<stdio.h>
    #include <stdlib.h>
    
    struct student
    {
        char name[10];
        float score[3];
    };
    int main()
    {
        struct student iboy;
        FILE* fp;
        if ((fp = fopen("c:\\f\\stduent.txt", "w")) == NULL)
        {
            printf("open file error!\n");
            exit(0);
        }
        printf("Please enter the student name and three score:\n");
        scanf("%s%f%f%f", iboy.name, &iboy.score[0], &iboy.score[1],
            &iboy.score[2]);
        printf("Please enter the student name and three score again(ctrl+z to quit):\n");
        while (!feof(stdin))
        {
            fprintf(fp,"%s %.2f %.2f %.2f\n", iboy.name, iboy.score[0], iboy.score[1],
                iboy.score[2]);
            scanf("%s%f%f%f", iboy.name, &iboy.score[0], &iboy.score[1],
                &iboy.score[2]);
        }
        fclose(fp);
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 5月18日
  • 已采纳回答 5月10日
  • 创建了问题 4月26日

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: