从文本文件中读取结构

时间:2013-12-15 19:49:39

标签: c file

我需要从文件中读取struct,但我的代码无效。 如何到达文件结尾

#include<stdio.h>

typedef struct elements {
   char CallType;
   int noofcparty,nooPartyBcalling,id,number_of_packets,roaming_option;

} Elements;

int main()
{  
    Elements e;
    FILE *ptr_file;
    char buf[1000];
    ptr_file =fopen("save_data.txt","r");
    fclose(ptr_file);
    getchar();
    getchar();
    return 0;
}

1 个答案:

答案 0 :(得分:0)

我假设文件是​​CSV(以逗号分隔的值)。 例如:b,100,200,800,45。

char line[200];
int max_size_line=200;
while(fgets(line,max_size_line,ptr_file)!=0)//read line by line until the end
    {    sscanf(line,"%c,%d,%d,%d,%d,%d",&CallType,&noofcparty,&nooPartyBcalling,&id,&number_of_packets,&roaming_option);
          }