用fscanf函数来读取
#include "stdio.h"
#define TOTALLINE 4//你的文件有多少行就定义成多少,我例子4 行
struct Mystruct
{
int time;
char type;
int num;
};
int main()
{
FILE *fp;
Mystruct s[TOTALLINE];
int i;
if((fp=fopen("in.txt","rt"))==NULL)
{
printf("cannot open file\n");
return 1;
}
for(i=0;i { fscanf(fp,"t=%d",&s[i].time); fscanf(fp,","); //如果是t=6,r=b8的格式,改成fscanf(fp,"r=%c",&s[i].type); fscanf(fp,"%c",&s[i].type); fscanf(fp,"%d",&s[i].num); fscanf(fp,"\n"); } for(i=0;i { printf("Time:%d\nType:%c\nNum:%d\n",s[i].time,s[i].type,s[i].num); } fclose(fp); return 0; } 结果: