从txt文档中逐行读取数据
相关函数
int sscanf (const char *str,const char * format,…)
函数功能:从一个字符串中读进与指定格式相符的数据.
参数@str 指定字符串
@format 参考scanf相关格式
test.txt 文档内容
zhangsan 10 100
lisi 15 98
wangwu 20 75
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX_LINE 1024
int age;
int score;
int main()
{
char buf[MAX_LINE];
FILE*fp;
int len;
if((fp=fopen("test.txt","r"))==NULL)
{
perror("fail to open");
exit(1);
}
while(fgets(buf,MAX_LINE,fp)!=NULL)
{
char name[100];
len=strlen(buf);
printf("buf=%s",buf);
buf[len-1]='\0';
printf("%s %d\n",buf,len-1);
sscanf(buf,"%s%d%d",name,&age,&score);
printf("name=%s,age=%d,score=%d\n",name,age,score);
}
return 0;
}