学生成绩管理系统 输出乱码
#include<stdio.h>#include<string.h>
#include<stdlib.h>
#include<conio.h>
struct student
{
char chName[20]; //姓名
int nID; //学号
float fScores[3]; //3门课的成绩
};
void main()
{
FILE *pWrite,*pRead;
struct student tStu,tStu2;
char *pName = "letuknowit";
pWrite=fopen("data.txt","w");
if(NULL == pWrite)
{
return;
}
//初始化结构体信息,用于写入文件
memcpy(tStu.chName,pName,20);//从源pName所指的内存地址的起始位置开始拷贝20个字节到目标tStu.chName所指的内存地址的起始位置中
tStu.nID = 1;
tStu.fScores[0] = 89.0;
tStu.fScores[1] = 87.0;
tStu.fScores[2] = 88.0;
//写入数据到文件中
fprintf(pWrite,"%d %s %f %f %f\n",tStu.nID,tStu.chName,tStu.fScores[0],tStu.fScores[1],tStu.fScores[2]);
fclose(pWrite);
pRead=fopen("data","r");
if(NULL == pRead)
{
return;
}
//从文件中读取数据(主要下面参数中的&)
fscanf(pRead,"%d %s %f %f %f\n",&tStu2.nID,tStu2.chName,&tStu2.fScores[0],&tStu2.fScores[1],&tStu2.fScores[2]);
fclose(pRead);
//打印读取的数据到屏幕上
printf("%d %s %.lf %.lf %.lf\n",tStu2.nID,tStu2.chName,tStu2.fScores[0],tStu2.fScores[1],tStu2.fScores[2]);
}