帮我看看怎么读取文件中的内容
学号| 名字| 语文| 数学| 英语
1| yusumao|22.00|33.00|44.00
2| yusumao|22.00|33.00|44.00
3| yusumao|22.00|33.00|44.00
4| yusumao|22.00|33.00|44.00
我想读取这个文件里面的学号和名字和语文和数学和英语。帮我看看怎么读取,如果可以的话,写一个程序来读取,我就把分全部给他
[解决办法]
//ls的是MFC的
//下面没写全,没调试,仅示意
#include <stdio.h>
typedef struct score
{
char *stuNum;
char *name;
float chinese;
float math;
float english;
}S,*pS;
int main()
{
FILE *fp;
char ch;
char chinese[5],math[5],english[5];
pS info=(pS)malloc(sizeof(S));
if(info=NULL)exit(0);
info-> stuNum= " ";
info-> name= " ";
info-> chinese=0.0;
info-> math=0.0;
info-> english=0.0;
if((fp=fopen( "d:\\score.txt ", "r+ "))==NULL)exit(0);
ch=fgetc(fp)
//while(ch!=EOF){
do//readline
{
*(info-> stuNum)++=ch;
if(ch== '| '){
*(info-> stuNum)++=ch;
if(ch== '| '){
*chinese++=ch;
if(ch== '| '){
*math++=ch;
if(ch== '| '){
*english++=ch;
}
}
}
}
if(ch= '\n ')break;
ch=fgetc(fp);
}while(1);
//}//没有对数据进行转换处理
//printf( "学号|名字| 语文| 数学| 英语 ");
printf( "%s|%s|%s|%s|%s\n ",info-> stuNum,info-> stuNum,chinese,math,english);
getch();
return 0;
}
[解决办法]
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int num=0;
char name[32];
float a=0,b=0,c=0;
FILE *file;
file=fopen( "test.txt ", "r ");
if(!file)return 0;
while(!feof(file))
{
fscanf(file, "%d|%[^|]|%f|%f|%f\n ",&num,name,&a,&b,&c);
printf( "%d,%s,%f,%f,%f\n ",num,name,a,b,c);
}
fclose(file);
}
[解决办法]
看看我的,说明:
1.使用了单向链表,如果你要想操作更方便,可改成双向链表
2.使用了状态机,用于parser文件格式
3.测试用的文件最后一行是\n
4.纯CRT实现,VC6.0测试
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct _student_score
{
int no;
char name[32];
float chinese;
float math;
float english;
_student_score* next;
}student_score, *pstudent_score;
pstudent_score std_sc;
typedef enum _ST_STATUS{ST_NO, ST_NAME, ST_CHN, ST_MATH, ST_ENG, ST_NONE}ST_STATUS;
void print()
{
printf( "\t学号|\t名字|\t语文|\t数学|\t英语\n ");
while(std_sc-> next)
{
printf( "\t%d,\t%s,\t%.2f,\t%.2f,\t%.2f\n ",
std_sc-> next-> no, std_sc-> next-> name, std_sc-> next-> chinese, std_sc-> next-> math, std_sc-> next-> english);
std_sc = std_sc-> next;
}
}
int main()
{
FILE *file;
char ch;
char buf[64];
int index = 0;
ST_STATUS st_status = ST_NO;
pstudent_score new_st = new student_score;
student_score one_buf;
memset(&one_buf, 0, sizeof(student_score));
new_st-> next = NULL;
std_sc = new_st;
if((file=fopen( "test.txt ", "r+ "))==NULL)
{
return -1;
}
fread(&ch,1,1,file);
while(!feof(file))
{
switch(st_status)
{
case ST_NO:
if (isgraph(ch) != 0 && ch != ' ')
{
if (ch == '| ')
{
buf[index] = 0;
index = 0;
st_status = ST_NAME;
one_buf.no = atoi((const char*)buf);
}
else buf[index ++] = ch;
}
break;
case ST_NAME:
if (isgraph(ch) != 0 && ch != ' ')
{
if (ch == '| ')
{
buf[index] = 0;
index = 0;
st_status = ST_CHN;
strcpy(one_buf.name, buf);
}
else buf[index ++] = ch;
}
break;
case ST_CHN:
if (isgraph(ch) != 0 && ch != ' ')
{
if (ch == '| ')
{
buf[index] = 0;
index = 0;
st_status = ST_MATH;
one_buf.chinese = (float)atof((const char*)buf);
}
else buf[index ++] = ch;
}
break;
case ST_MATH:
if (isgraph(ch) != 0 && ch != ' ')
{
if (ch == '| ')
{
buf[index] = 0;
index = 0;
st_status = ST_ENG;
one_buf.math = (float)atof((const char*)buf);
}
else buf[index ++] = ch;
}
break;
case ST_ENG:
if (ch != ' ')
{
if (ch == '\n ')
{
buf[index] = 0;
index = 0;
st_status = ST_NO;
one_buf.english = (float)atof((const char*)buf);
new_st-> next = new student_score;
memcpy(new_st-> next, &one_buf, sizeof(student_score));
new_st = new_st-> next;
new_st-> next = NULL;
memset(&one_buf, 0, sizeof(student_score));
}
else buf[index ++] = ch;
}
break;
default:
st_status = ST_NONE;
}
fread(&ch,1,1,file);
}
fclose(file);
print();
system( "pause ");
return 0;
}