首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

读取文本数据或参数到构造体

2013-08-16 
读取文本数据或参数到结构体我定义了这样一组结构:typedef struct student//学生数据结构体声明{int numbe

读取文本数据或参数到结构体
我定义了这样一组结构:

typedef struct student//学生数据结构体声明
{
int number;//学号
int score[3];//平时、期末和总评成绩
}STUDENT;


我想将文本中的一组数据:
[studentinfo]
stuudentsum=3

[student1]
number=00001
score1=88
score2=99

[student2]
number=00002
score1=99
score2=86

[student3]
number=00003
score1=78
score2=65

[student4]
number=00004
score1=78
score2=56

[student5]
number=00005
score1=35
score2=78

[student6]
number=00006
score1=98
score2=76

[student7]
number=00007
score1=78
score2=65

(score3由score1、score2计算得出)读取到结构中,我下面是这么定义函数的:请给指点一下
void readinfo(int student_sum, int student_number, int student_score[])//从文本读取
{
FILE *pRF;

if(NULL == (pRF=fopen(STUDENT_FILE, "rb")))
{
printf("open false!");
return;
}

//下面不知道怎么分类读取里面的数据到结构了。
}
C 数据结构
[解决办法]
你怎么写入文本STUDENT_FILE的,就怎么读出来呗
[解决办法]
推荐使用WinHex软件查看硬盘或文件或内存中的原始字节内容。

不要把
fopen("...","...");fscanf,fprintf,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待

fopen("...","...b");fread,fwrite,fclose  //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待
弄混了


[解决办法]
vs下用GetPrivateProfileInt函数读取ini文件中的数据到int
如果是linux下,自己找个读ini文件的源代码

[解决办法]
没实际编译链接调试,不保证对,仅供参考:
//in.txt:
//[studentinfo]
//stuudentsum=3
//
//[student1]
//number=00001
//score1=88
//score2=99
//
//[student2]
//number=00002
//score1=99
//score2=86
//
//[student3]
//number=00003
//score1=78
//score2=65
//
//[student4]
//number=00004
//score1=78
//score2=56
//
//[student5]
//number=00005
//score1=35
//score2=78


//
//[student6]
//number=00006
//score1=98
//score2=76
//
//[student7]
//number=00007
//score1=78
//score2=65
#include <stdio.h>
#include <string.h>
typedef struct student {
    int number;
    int score[3];//平时、期末和总评成绩
} STUDENT;
STUDENT s[8];
FILE *f;
char t[40];
char n[40];
int i;
int main() {
    f=fopen("in.txt","r");
    if (NULL==f) return 1;
    fgets(n,40,f);
    fgets(n,40,f);
    fgets(n,40,f);
    i=1;
    while (1) {
        sprintf(t,"[student%d]",i);
        if (1!=fscanf(f,t,n)) break;
        if (1!=fscanf(f,"number=%d",&s[i].number)) break;
        if (1!=fscanf(f,"score1=%d",&s[i].score[0])) break;
        if (1!=fscanf(f,"score2=%d",&s[i].score[1])) break;
        fgets(n,40,f);
        fgets(n,40,f);
        i++;
    }
    fclose(f);
    for (i=1;i<=7;i++) {
        s[i].score[2]=s[i].score[0]+s[i].score[1]
        printf("[student%d] number=%d score1=%d score2=%d score3=%d\n",i,s[i].number,s[i].score[0],s[i].score[1],s[i].score[2]);
    }
    return 0;
}


[解决办法]
引用:
推荐使用WinHex软件查看硬盘或文件或内存中的原始字节内容。

不要把
fopen("...","...");fscanf,fprintf,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待

fopen("...","...b");fread,fwrite,fclose  //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待
弄混了



使用fread函数就可以,详见http://baike.baidu.com/view/656689.htm
(注:把结构体堪称一个和int一样普普通通的变量类型来处理就行)
[解决办法]
引用:
没实际编译链接调试,不保证对,仅供参考:
//in.txt:
//[studentinfo]
//stuudentsum=3
//
//[student1]
//number=00001
//score1=88
//score2=99
//


//[student2]
//number=00002
//score1=99
//score2=86
//
//[student3]
//number=00003
//score1=78
//score2=65
//
//[student4]
//number=00004
//score1=78
//score2=56
//
//[student5]
//number=00005
//score1=35
//score2=78
//
//[student6]
//number=00006
//score1=98
//score2=76
//
//[student7]
//number=00007
//score1=78
//score2=65
#include <stdio.h>
#include <string.h>
typedef struct student {
    int number;
    int score[3];//平时、期末和总评成绩
} STUDENT;
STUDENT s[8];
FILE *f;
char t[40];
char n[40];
int i;
int main() {
    f=fopen("in.txt","r");
    if (NULL==f) return 1;
    fgets(n,40,f);
    fgets(n,40,f);
    fgets(n,40,f);
    i=1;
    while (1) {
        sprintf(t,"[student%d]",i);
        if (1!=fscanf(f,t,n)) break;
        if (1!=fscanf(f,"number=%d",&s[i].number)) break;
        if (1!=fscanf(f,"score1=%d",&s[i].score[0])) break;
        if (1!=fscanf(f,"score2=%d",&s[i].score[1])) break;
        fgets(n,40,f);
        fgets(n,40,f);
        i++;
    }
    fclose(f);
    for (i=1;i<=7;i++) {
        s[i].score[2]=s[i].score[0]+s[i].score[1]
        printf("[student%d] number=%d score1=%d score2=%d score3=%d\n",i,s[i].number,s[i].score[0],s[i].score[1],s[i].score[2]);
    }
    return 0;
}


是否可以把

if (1!=fscanf(f,"number=%d",&s[i].number)) break;
if (1!=fscanf(f,"score1=%d",&s[i].score[0])) break;
if (1!=fscanf(f,"score2=%d",&s[i].score[1])) break;

合并成一句话

if ( 3!=fscanf(f,"number=%d\nscore1=%d\nscore2=%d",
        &s[i].number,&s[i].score[0],&s[i].score[1]) ) break;

------解决方案--------------------


引用:
Quote: 引用:

没实际编译链接调试,不保证对,仅供参考:
//in.txt:
//[studentinfo]
//stuudentsum=3
//
//[student1]
//number=00001
//score1=88
//score2=99
//
//[student2]
//number=00002
//score1=99
//score2=86
//
//[student3]
//number=00003
//score1=78
//score2=65
//
//[student4]
//number=00004
//score1=78
//score2=56
//
//[student5]
//number=00005
//score1=35
//score2=78
//
//[student6]
//number=00006
//score1=98
//score2=76
//
//[student7]
//number=00007
//score1=78
//score2=65
#include <stdio.h>
#include <string.h>
typedef struct student {
    int number;
    int score[3];//平时、期末和总评成绩
} STUDENT;
STUDENT s[8];
FILE *f;
char t[40];
char n[40];
int i;
int main() {
    f=fopen("in.txt","r");
    if (NULL==f) return 1;
    fgets(n,40,f);
    fgets(n,40,f);
    fgets(n,40,f);
    i=1;
    while (1) {
        sprintf(t,"[student%d]",i);
        if (1!=fscanf(f,t,n)) break;
        if (1!=fscanf(f,"number=%d",&s[i].number)) break;
        if (1!=fscanf(f,"score1=%d",&s[i].score[0])) break;
        if (1!=fscanf(f,"score2=%d",&s[i].score[1])) break;
        fgets(n,40,f);
        fgets(n,40,f);
        i++;
    }
    fclose(f);
    for (i=1;i<=7;i++) {
        s[i].score[2]=s[i].score[0]+s[i].score[1]
        printf("[student%d] number=%d score1=%d score2=%d score3=%d\n",i,s[i].number,s[i].score[0],s[i].score[1],s[i].score[2]);
    }
    return 0;
}

如果文本里面的number score[1] score[2] 顺序调换后,while(1)还能如此检测到成绩吗?

不能。题目没明确说明可以调换顺序。

热点排行