文件打开,关闭,读,写fopen,fclose,fscanf,fprintf

来源:互联网 发布:恒大网络监控员 编辑:程序博客网 时间:2024/06/02 13:16

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


void main()
{
FILE *fp;
fp = fopen("c:\\tpm\\students.txt","r+");
if(fp==NULL)
{
printf("Fail to open the file.");
return;
}
char szName[30],szGendar[30];
int nId,nBirthYear,nBirthMonth,nBirthDay;
float fGPA;
while(fscanf(fp,"%s%d%s%d%d%d%f\n",szName,&nId,szGendar,&nBirthYear,&nBirthMonth,&nBirthDay,&fGPA)!=EOF)
{
printf("%s %d %s %d %d %d %f\n",szName,nId,szGendar,nBirthYear,nBirthMonth,nBirthDay,fGPA);
}
char *sz="tom";
char *sz2 ="df";
nId=1;nBirthYear=2;nBirthMonth=3;nBirthDay=4;fGPA=2.44;
fprintf(fp,"%s %d %s %d %d %d %f\n",sz,nId,sz2,nBirthYear,nBirthMonth,nBirthDay,fGPA);
fclose(fp);
}