ox0spy@ox0spy-laptop:/code/c$ cat tt.txt
aa,female,21,54
abc,male,20,60
tt,male,22,58
ox0spy,male,23,80
mary,female,24,90
cc,female,22,88
每个字段代表的意思是:姓名,性别,年龄,成绩
请用c语言编写程序,打印成绩大于等于60分的同学的姓名和成绩.
ox0spy@ox0spy-laptop:/code/c$ cat parseCsv.c
/*************************************************************************
Author : ox0spy
Email : ossteerer@gmail.com
Blog : ox0spy.blogspot.com
Created Time : Tue 17 Mar 2009 11:35:04 PM CST
File Name : parseCsv.c
Description :
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main()
{
char filename[20] = "tt.txt";
FILE *fp;
char name[20];
char sex[8];
int age;
int grade;
if((fp = fopen(filename, "r")) == NULL)
{
printf("open file : %s error\n%s\n", filename, strerror(errno));
exit(1);
}
while(fscanf(fp, "%[^,],%[^,],%[^,],%d\n", name, sex, &age, &grade)
!= EOF)
{
if(grade >= 60)
printf("%s\t%d\n", name, grade);
}
fclose(fp);
exit(0);
}
No comments:
Post a Comment
您的评论将使我blog更有动力~