i want a dynamic array of my struct Data
to contais the element of the file.I don't know the dimension of the file before so i need a dynamic array to cointains it. Should i use an array of struct or do you suggest a linked list?(the purpose is to create a quick sort and sort the element of the file) Why doesn't print anything? I did something wrong with malloc e realloc?
manager.h:
#ifndef MANAGER_H
#define MANAGER_H
#include <stdio.h>
#include <stdlib.h>
struct Data
{
int id;
char field1[50];
int field2;
float field3;
};
extern struct Data* records;
int readFile();
#endif
managaer.c
#include "manager.h"
struct Data* records;
int readFile(){
FILE *file = fopen("records.csv","r");
if(file == NULL){
printf("Error opening file.\n");
return 1;
}
records = malloc(sizeof(struct Data));
int num=0;
do{
fscanf(file,"%d,%49[^,],%d,%f\n",&records[num].id,records[num].field1,&records[num].field2,&records[num].field3);
num++;
records = realloc(records,num*sizeof(struct Data));
}while(num<5);
fclose(file);
for (int i = 0; i < num; i++)
{
printf("\n%d %s %d %f",records[i].id,records[i].field1,records[i].field2,records[i].field3);
}
return 0;
}