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

时间:2018-12-09 14:39:19

标签: c arrays file struct

我必须阅读这种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返回值?

1 个答案:

答案 0 :(得分:0)

另一种方法是使用fgets读取一行,为每行重新分配并使用sscanf解析该行。对于大量的行,可以对行块进行重新分配,并跟踪分配的行数和使用的行。当使用的行数等于分配的行数时再次分配。

#include <stdio.h>
#include <stdlib.h>

typedef struct  {
    int cod_event;
    int x;
    int y;
    int z;
    int day;
    int month;
    int year;
    int hour;
    int minute;
}Eventi_astronomici;

int main ( void) {
    char line[100] = "";
    int count = 0;
    int each = 0;
    FILE *pf = NULL;
    Eventi_astronomici *all_data_astro = NULL;
    Eventi_astronomici *temp = NULL;

    if ( NULL == ( pf = fopen ( "astrodata.txt", "r"))) {
        fprintf ( stderr, "could not open file\n");
        return 0;
    }

    while ( fgets ( line, sizeof line, pf)) {
        if ( NULL == ( temp = realloc ( all_data_astro, sizeof ( *all_data_astro) * (count + 1)))) {
            fprintf ( stderr, "realloc problem\n");
            fclose ( pf);
            free ( all_data_astro);
            return 0;
        }
        all_data_astro = temp;
        if ( 9 == sscanf ( line, "%d;%d;%d;%d;%d/%d/%d;%d:%d"
        , &all_data_astro[count].cod_event
        , &all_data_astro[count].x
        , &all_data_astro[count].y
        , &all_data_astro[count].z
        , &all_data_astro[count].day
        , &all_data_astro[count].month
        , &all_data_astro[count].year
        , &all_data_astro[count].hour
        , &all_data_astro[count].minute )) {
            count++;
        }
        else {
            printf ( "problem with record\n");
        }
    }

    fclose ( pf);

    for ( each = 0; each < count; ++each) {
        printf("%d,%d,%d,%d,%d/%d/%d,%d:%d\n"
        , all_data_astro[each].cod_event
        , all_data_astro[each].x
        , all_data_astro[each].y
        , all_data_astro[each].z
        , all_data_astro[each].day
        , all_data_astro[each].month
        , all_data_astro[each].year
        , all_data_astro[each].hour
        , all_data_astro[each].minute);
    }

    free ( all_data_astro);

    return 0;
}