C 从文本文件读取结构
我需要从文件中读取结构,但我的代码不起作用。 我怎样才能到达文件的末尾C 从文本文件读取结构,c,file,C,File,我需要从文件中读取结构,但我的代码不起作用。 我怎样才能到达文件的末尾 #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
#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;
}
我假设文件是CSV逗号分隔值。 示例:b,100200800,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);
}
阅读和。如果您想在内存中存储和检索结构的二进制表示形式,可以使用fwrite和fread。