从文件中读入数据到结构数组中的问题

arden1019 2005-12-08 01:36:11
下面的程序读取文件“readfile”,并将各部分数据读到一个结构体数组中。
可是所有的namea都会被最后一次读入的数据覆盖。请各位老大帮我解惑,是不是我在结构体中使用了一个char *?



文件的格式如下:

7
1
arden
2
jane
3
tom

第一行为文件行数,从第二列起id是一行int namea是一行string。顺序读入结构体数组sda中。



/*
* readfile.c read a file to a struct array
*
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NUM 50

int tline; //行数

typedef struct {
int id;
char *namea;
}SD;

int main()
{
int n=1,k=0,j=0; //n表示文件行数,j表示结构体索引(从1开始)所以在赋值时使用j-1。

FILE *fp;
SD sda[MAX_NUM];
char string [50];


if((fp=fopen("c:\\readfile","r"))==NULL){
perror("sth wrong about open file");
exit(-1);
}

while(fgets(string,50,fp)!=NULL) {

if(n==1) {tline=atoi(string); n++;}
else{
j=n/2;


if ((n%2)==0){ //如果是偶数说明在读id
sda[j-1].id=atoi(string);
printf("j : %d \t n : %d \t sda[%d].id :%d\n",j,n,j-1,sda[j-1].id);
n++;

}
else //奇数是namea
{
sda[j-1].namea=string;
printf("j : %d \t n : %d \t sda[%d].namea :%s\n",j,n,j-1,sda[j-1].namea);
n++;
}




}//else
}

printf("the file has %d lines \n",tline);
printf("the %d student info list \n",(tline-1)/2);
for(k=0;k<=(tline-1)/2-1;k++)
{
printf("id : %d\tthe name :%s\n",sda[k].id,sda[k].namea);
}
system("pause");
return 0;
}
...全文
603 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
csucdl 2005-12-08
  • 打赏
  • 举报
回复
记得用strcpy等内存拷贝函数来保存读出的字符串
csucdl 2005-12-08
  • 打赏
  • 举报
回复
其中的原因 happy__888([顾问团]寻开心) 讲的很清楚
修改的办法一是为每个用到的结构体动态分配内存, 不过要记得用free释放
或者修改一下结构体中的 namea成员类型, 采用静态分配内存
char namea[LEN];
cunsh 2005-12-08
  • 打赏
  • 举报
回复
指针...
楼主.如果是C++的话使用string类呀.
suiliushui 2005-12-08
  • 打赏
  • 举报
回复
typedef struct {
int id;
char *namea;
}SD;


namea没有分配内存
可以改为
typedef struct {
int id;
char namea[50];
}SD;


sda[j-1].namea=string;不可以这么写





#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NUM 50

int tline; //ミミハ

typedef struct {
int id;
char namea[50];
}SD;

int main()
{
int n=1,k=0,j=0; //nア桄セホトシミミハ」ャjア桄セス盪ケフ衢」ィエモ1ソェハシ」ゥヒヤヤレクウヨオハアハケモテj-1。」

FILE *fp;
SD sda[MAX_NUM];
char string [50];


if((fp=fopen("fprintf.out","r"))==NULL){
perror("sth wrong about open file");
exit(-1);
}

while(fgets(string,50,fp)!=NULL) {

if(n==1) {tline=atoi(string); n++;}
else{
j=n/2;


if ((n%2)==0){ //ネ郢鈹ヌナシハヒオテレカチid
sda[j-1].id=atoi(string);
printf("j : %d \t n : %d \t sda[%d].id :%d\n",j,n,j-1,sda[j-1].id);
n++;

}
else //ニ賁ハヌnamea
{
//sda[j-1].namea=string;
strcpy( sda[j-1].namea, string );
printf("j : %d \t n : %d \t sda[%d].namea :%s\n",j,n,j-1,sda[j-1].namea);
n++;
}
}//else
}

printf("the file has %d lines \n",tline);
printf("the %d student info list \n",(tline-1)/2);
for(k=0;k<=(tline-1)/2-1;k++)
{
printf("id : %d\tthe name :%s\n",sda[k].id,sda[k].namea);
}
system("pause");
return 0;
}


arden1019 2005-12-08
  • 打赏
  • 举报
回复
sda[j-1].namea = string;
这句话,并没有把string当中的内容copy给了namea,只是把string的地址,赋值给了namea

这个是关于指针使用的最基本的注意项之一

补习指针的知识吧

----------
thx,
我明白了,结果是我结构体中的namea都指向了同一个地方。:)
屋顶上的老猫 2005-12-08
  • 打赏
  • 举报
回复
不用这么麻烦吧!如果你向把文件中的数据写进结构体,只需要在把数据写进文件时,按你需要的结构体形式处理一下就可以了!

例如:
struct source
{
char s[10];
int i[10];
float f[10];
};
main()
{
FILE *fp;
struct soucre SOURCE;
struct source DEST;

fp=fopen("c:\\test.txt","wb");
fwrite(&SOURCE,sizeof(SOURCE),1,fp);
fclose(fp);

fp=fopen("c:\\test.txt","rb");
fread(&DEST,sizeof(SOURCE),1,fp);
fclose(fp)

//处理下面的DEST就OK了
printf(DEST.s);
printf(DEST.i);
printf(DEST.f);

}
寻开心 2005-12-08
  • 打赏
  • 举报
回复
sda[j-1].namea = string;
这句话,并没有把string当中的内容copy给了namea,只是把string的地址,赋值给了namea

这个是关于指针使用的最基本的注意项之一

补习指针的知识吧
寻开心 2005-12-08
  • 打赏
  • 举报
回复
else //奇数是namea
{
sda[j-1].namea=string; // 这里有问题
...........
}

string是一个共享的变量,不能这样赋值
应该是这样
else
{
int n = strlen(string);
sda[j-1].namea = (char *)malloc(n);
strcpy(sda[j-1].namea, string, n);
.....
}


按照你原来的做法,只是把string这个数组的地址赋值给了所有的namea参数了
当然会出问题的
arden1019 2005-12-08
  • 打赏
  • 举报
回复
我感觉不是读取的问题吧?因为我在为结构体赋值后都会打印出结果看,当时的结果是正确的。
楼上的哥哥能不能把为什么用fscanf,或者我的代码中的问题告诉我么?
goodluckyxl 2005-12-08
  • 打赏
  • 举报
回复
用fscanf读取

70,026

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧