There is a specific csv file containing lines in a specific format, and I defined a struct of it.
typedef struct {
int passengerId;
int survival; // survived = 1, dead = 0
enum PClass pclass; // 1st, 2nd, 3rd class
char* name;
enum Sex sex;
float age;
int sibsp; //number of siblings
int parch; //number of family members
char* ticket; //ticket number
double fare;
char* cabin;
char* embarked;
}SUVData;
and this struct is declared in main() with a double pointer format, initial size 1 - for implementation of 2-dimensional array :
int main() {
SUVData** data = (SUVData**)calloc(1, sizeof(SUVData*));
ReadData(data, "titanic.csv");
return 0;
}
and this is given as a parameter to the reading data function :
void ReadData(SUVData** dataset, char* filename) {
FILE* fp;
char line[1024];
int i = 0;
// temporary variables
int tempPassengerId;
int tempSurvival;
int tempPClass;
char* tempName = (char*)malloc(sizeof(char) * 100);
char* tempSex = (char*)malloc(sizeof(char) * 7);
float tempAge;
int tempSibsp;
int tempParch;
char* tempTicket = (char*)malloc(sizeof(char) * 20);
double tempFare;
char* tempCabin = (char*)malloc(sizeof(char) * 10);
char* tempEmbarked = (char*)malloc(sizeof(char) * 2);
fp = fopen(filename, "r");
if (fp == NULL) {
printf("file open error\n");
return;
}
printf("\nFile loaded\n");
while (fgets(line, 1024, fp)) {
line[strlen(line) - 1] = '\0';
printf("test line : %s\n", line);
// Format in temp variables
int t = sscanf(line, "%d,%d,%d,%[^,],%[^,],%f,%d,%d,%[^,],%lf,%[^,],%s", &tempPassengerId, &tempSurvival, &tempPClass, tempName, tempSex, &tempAge, &tempSibsp, &tempParch,
tempTicket, &tempFare, tempCabin, tempEmbarked);
// Re-arrange in dataset (where I'm having problem)
(*(*(dataset + i))).passengerId = tempPassengerId;
printf("test Id : %d\n", (*(*(dataset + i))).passengerId);
i++;
}
fclose(fp);
}
I was planning on assigning temporary variables inside struct SUVData, using code like this(as written above) : (*(*(dataset + i))).passengerId = tempPassengerId;
Tried to test if it's working, but no.