+-
C语言vs2019,txt文件格式化读取的问题

void showlist() {


//局部变量的初始化
char username[20] = { 0 };
char stunum[20] = { 0 };
int chinese = 0;
int math = 0;
int english = 0;

//FILE型指针的初始化
FILE * fp = NULL;
//打开文件
if ((fp = fopen("s1.txt", "r")) == NULL)
{
    perror("(tip:文件打开失败!请联系管理员,电话:152346789。)");
    exit(0);
}
printf_s("(tip:文件已经打开!)\n");
Sleep(1500);

system("cls");
//打印表头
printf_s("-------------------------------------------------------------------\n");
printf_s("%-10s %-10s %-10s %-10s %-10s %-12s|\n", "姓名", "学号", "语文", "数学", "英语", "总分", "平均分");
printf_s("-------------------------------------------------------------------\n");

//向txt文件读取数据
while (fscanf_s(fp, "%s %s %d %d %d %d", &username, &stunum, &chinese, &math, &english) != EOF)
{
    int sum = chinese + math + english;
    int avg = sum / 3;
    printf_s("%-10s %-10s %-10d %-10d %-10d %-10d %-10d %-12d|\n", username, stunum, chinese, math, english,sum,avg);
}

//打印表尾
printf_s("-------------------------------------------------------------------\n");
//暂停界面
system("pause");
//关闭文件
fclose(fp);

}

代码运行后,s1.txt里的文件没有读取出来,调试后,它说“写入位置时发生访问冲突”╥﹏╥...

fscanf_s(fp, "%s %s %d %d %d %d", &username, &stunum, &chinese, &math, &english)

格式串里面有6个参数,你实际只给了5个。

printf_s("%-10s %-10s %-10d %-10d %-10d %-10d %-10d %-12d|\n", username, stunum, chinese, math, english,sum,avg);

格式串里面有8个参数,后面只给了7个。