java csv 数组_使用csv文件的输入填充结构数组

我必须阅读这种csv文件

565;4;6;8;11/10/2017;11:30

756;5;9;1;12/12/2017;10:40

765;-8;-2;-1;06/01/2018;23:23

我需要将这些数据放入此结构中

typedef struct {

int cod_event;

int x;

int y;

int z;

int day;

int month;

int year;

int hour;

int minute;

}Eventi_astronomici;

这是使用适当的尺寸初始化和重新分配的数组 . 所以我知道我的文件的维度,我只需要用这些整数填充这个数组

Eventi_astronomici *all_data_astro;

all_data_astro = calloc( 200 , sizeof(Eventi_astronomici));

if (all_data_astro == NULL)

{

printf("Malloc failed1!\n");

return -1;

}

char c;

for (c = getc(file_ptr); c != EOF; c = getc(file_ptr))

if (c == '\n') // Increment count if this character is newline

count = count + 1;

printf("%d\n", count);

all_data_astro = realloc( all_data_astro , count * sizeof( Eventi_astronomici));

if (all_data_astro == NULL)

{

printf("Realloc failed!\n");

return -1;

}

多数民众赞成我试图从文件中获取输入,在此之前我回放文件ptr . 然后我打印我的数组,看看fscanf是否有效

for( i = 0 ; i < count ; i++){

fscanf(file_ptr , "%d;%d;%d;%d;%d/%d/%d;%d:%d" ,&all_data_astro[i].cod_event, &all_data_astro[i].x ,

&all_data_astro[i].y,&all_data_astro[i].z,&all_data_astro[i].day ,

&all_data_astro[i].month,&all_data_astro[i].year,&all_data_astro[i].hour,&all_data_astro[i].minute );

}

for (i = 0; i < count; i++) {

printf("%d,%d,%d,%d,%d/%d/%d,%d:%d\n",all_data_astro[i].cod_event, all_data_astro[i].x ,

all_data_astro[i].y,all_data_astro[i].z,all_data_astro[i].day ,

all_data_astro[i].month,all_data_astro[i].year,all_data_astro[i].hour,all_data_astro[i].minute);

}

奇怪的是,即使fscanf无法工作,我也不会得到任何印刷品 . 所以程序可能在甚至到达下一个循环之前就阻塞了,也许我需要检查if中的fscanf返回值?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值