迷途的启木菌
2019-11-16 20:02
采纳率: 33.3%
浏览 442

文件格式输出函数——fprintf()的使用,运行后报错

在运行后,程序会出错无法运行,请问大佬们怎么解决。

#include<stdio.h>
#include<stdlib.h>
#define N 3

struct student
{
    char *number;
    char *name;
    int chinese, math, english;
}stu[N];

int main(void)
{
    FILE *fp;
    int i = 0;

    if (fopen_s(&fp, "d:\\wenjian\\test5.txt","wt") != NULL)
    {
        printf("cannot open file test5!\n");
        exit(1);
    }

    for (i = 0; i < N; i++)
    {
        printf("please put in number:\n");
        scanf_s("%s", stu[i].number, 20);
        printf("please put in name:\n");
        scanf_s("%s", stu[i].name, 20);
        printf("please put in chinese:\n");
        scanf_s("%d", &stu[i].chinese);
        printf("please put in math:\n");
        scanf_s("%d", &stu[i].math);
        printf("please put in english:\n");
        scanf_s("%d", &stu[i].english);
        fprintf(fp, "%s %s %d %d %d\n",
            stu[i].number, stu[i].name, stu[i].chinese, stu[i].math, stu[i].english);
    }
    fclose(fp);
    getchar();
    return 0;
}

图片说明

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题