| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 6637 人关注过本帖, 1 人收藏
标题:如何读取一个文件把文件每行内容赋值给一个结构数组元素
取消只看楼主 加入收藏
khaz
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:130
专家分:188
注 册:2011-4-21
结帖率:77.78%
收藏(1)
已结贴  问题点数:20 回复次数:4 
如何读取一个文件把文件每行内容赋值给一个结构数组元素
例如:
结构为:
struct emp{
char name[20];
long age;
char phone[12];
}
文件第一列为名字 第二列为年龄 第三列为手机号
emp.txt:
fred|30|18677442345
barney|29|13766554321
jq|36|18977890987
我想通过编写C程序,把这个文件的内容循环读取到一个结构数组去,每次读取一行,遇到文件尾结束读取,每个数组元素对应为一个人。
如何编写,请教了。


[ 本帖最后由 khaz 于 2011-4-22 22:51 编辑 ]
搜索更多相关主题的帖子: 手机号 名字 元素 
2011-04-22 22:46
khaz
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:130
专家分:188
注 册:2011-4-21
收藏
得分:0 
谢谢,我是想按行读入到数组里,我就假定emp.txt只有3行了,所以行数下面的程序写死了,看看我这样写可以吗?报错信息为:Segmentation fault

错在哪?
程序代码:
#include<stdio.h>
#include<unistd.h>
struct emp{
        char name[20];
        long age;
        char phone[12];
};

int main(){
        int i;
        char *str;
        FILE * fp = fopen("emp.txt", "r");
        struct emp a[3];
        while(fgets(str,50,fp)!=NULL){
                for(i=0;i<3;i++){
                sscanf(str, "%s|%ld|%s", a[i].name, &a[i].age, a[i].phone);
                }
        }
}


[ 本帖最后由 khaz 于 2011-4-23 10:05 编辑 ]
2011-04-23 09:42
khaz
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:130
专家分:188
注 册:2011-4-21
收藏
得分:0 
谢谢 各位大虾的批评 我学习和考虑一下
2011-04-23 10:26
khaz
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:130
专家分:188
注 册:2011-4-21
收藏
得分:0 
感谢5楼,也要感谢voidx和6楼,再次提醒了空间分配的错误。
根据各位大虾的意见,我修改了一下,并增加了结果输出:
方法一:
[view@localhost c]$cat emp.txt
fred|30|18677442345
barney|29|13766554321
jq|36|18977890987

[view@localhost c]$cat 3.c
#include<stdio.h>
struct emp{
        char name[20];
        long age;
        char phone[12];
};

int main(){
        int i,ret;
        char str[50];
        FILE * fp = fopen("emp.txt", "r");
        struct emp a[3];
        i=0;
        while(fgets(str,50,fp)!=NULL){
                if((ret=sscanf(str, "%[^|]|%ld|%s", a[i].name, &a[i].age, a[i].phone))<3){
                fprintf(stderr,"sscanf() err!,ret=%d\n",ret);
                fclose(fp);
                exit(1);
        }
                i++;
        }
        for(i=0;i<3;i++){
        printf("emp%d name is %s,age is %ld,phone is %s\n",i,a[i].name,a[i].age,a[i].phone);
        }
        fclose(fp);
        return 0;
}


[view@localhost c]$cc -o 3 3.c
[view@localhost c]$./3
emp0 name is fred,age is 30,phone is 18677442345
emp1 name is barney,age is 29,phone is 13766554321
方法二:
[view@localhost c]$cat emp.txt
fred 30 18677442345
barney 29 13766554321
jq 36 18977890987
[view@localhost c]$cat 4.c
#include<stdio.h>
struct emp{
        char name[20];
        long age;
        char phone[12];
};

int main(){
        int i,ret;
        char *str=(char *)malloc(50);
        FILE * fp = fopen("emp.txt", "r");
        struct emp a[3];
        i=0;
        while(fgets(str,50,fp)!=NULL){
                if((ret=sscanf(str, "%s %ld %s", a[i].name, &a[i].age, a[i].phone))<3){
                fprintf(stderr,"sscanf() err!,ret=%d\n",ret);
                fclose(fp);
                exit(1);
        }
                i++;
        }
        for(i=0;i<3;i++){
        printf("emp%d name is %s,age is %ld,phone is %s\n",i,a[i].name,a[i].age,a[i].phone);
        }
        fclose(fp);
        return 0;
}


[view@localhost c]$cc -o 4 4.c
[view@localhost c]$./4
emp0 name is fred,age is 30,phone is 18677442345
emp1 name is barney,age is 29,phone is 13766554321
emp2 name is jq,age is 36,phone is 18977890987
程序虽然简单,但是实用性很强,工作中要用到。以前习惯了用shell处理文本,用C倒是第一次。

[ 本帖最后由 khaz 于 2011-4-23 11:08 编辑 ]
2011-04-23 10:51
khaz
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:130
专家分:188
注 册:2011-4-21
收藏
得分:0 
程序代码:
#include<stdio.h>
struct emp{
        char name[20];
        long age;
        char phone[12];
};

int main(){
        int i,ret,line,col;
        char str[50];
        FILE * f;
        FILE * fp = fopen("emp.txt", "r");
        f=popen("wc -l emp.txt|awk '{print $1}'","r");
        fscanf(f,"%d",&line);
        printf("line=%d\n",line);
        struct emp a[line];
        f=popen("awk -F'|' '{print NF}' emp.txt|sort -u","r");
        fscanf(f,"%d",&col);
        printf("col=%d\n",col);

        i=0;
        while(fgets(str,50,fp)!=NULL){
                if((ret=sscanf(str, "%[^|]|%ld|%s", a[i].name, &a[i].age, a[i].phone))<col){
                fprintf(stderr,"sscanf() err!,ret=%d\n",ret);
                fclose(fp);
                exit(1);
        }
                i++;
        }
        for(i=0;i<line;i++){
        printf("emp%d name is %s,age is %ld,phone is %s\n",i,a[i].name,a[i].age,a[i].phone);
        }
        fclose(fp);
        return 0;
}


我又改了一下,调用了shell去计算了文件行数和列数,虽然取列数有点多余,可毕竟是为了练手。而且用C计算有点麻烦,同事跟我说,C语言之父说过,C编程中有半打是用shell、perl等去处理的。我开始试图使用system函数,可是system函数返回值只是0,根据网上资料,我想取得shell的运行结果就用了popen,
程序执行结果为:
line=3
col=3
emp0 name is fred,age is 30,phone is 18677442345
emp1 name is barney,age is 29,phone is 13766554321
emp2 name is jq,age is 36,phone is 18977890987
2011-04-23 11:45
快速回复:如何读取一个文件把文件每行内容赋值给一个结构数组元素
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020398 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved