Alvaro Carvajal Nakosmai Alvaro Carvajal Nakosmai - 19 days ago 8
C Question

C Pointer arithmetic to fill struct

I am currently trying to fill a struct like this

typedef struct
{
int year;
int month;
int day;
int hour;
int minute;
} tDateTime;

typedef struct sFlight
{
char code[MAX_NAME_LENGTH];
char arrival[MAX_NAME_LENGTH];
char departure[MAX_NAME_LENGTH];
int availableSeats;
tDateTime dateTime;
struct sFlight * nextFlight;
} tFlight;


Reading from a txt file. This is what I am currently doing

void ops_loadFlightsInformation(tFlight * firstFlight)
{
// Open the file with r to only read
FILE *file = fopen(OPS_FLIGHTS_FILE, "r");
// If file is not open return
if(file == NULL) {
return;
}

tFlight currentFlight;

// Loop until there are no flights
while(next != 0) {
strcpy(currentFlight.code, helpers_scanFromFile(file, MAX_NAME_LENGTH));
strcpy(currentFlight.departure, helpers_scanFromFile(file, MAX_NAME_LENGTH));
strcpy(currentFlight.arrival, helpers_scanFromFile(file, MAX_NAME_LENGTH));
fscanf(file, "%d\n", &currentFlight.availableSeats);
fscanf(file, "%d/%02d/%02d %02d:%02d\n", &currentFlight.dateTime.year, &currentFlight.dateTime.month, &currentFlight.dateTime.day, &currentFlight.dateTime.hour, &currentFlight.dateTime.minute);
fscanf(file, "%d\n", &next);

printf("%s \n", currentFlight.code);
}

printf("%s \n", firstFlight->code);

// Close the file handle
fclose(file);
}


I cant manage to think how to fill the struct pointers on the while loop. I tried different ways but I always end up with the

warning: X may be used uninitialized in this function


Or just editing the same pointer over and over so only editing the first element. The loop works fine currentFlight gets populated correctly but I dont know how to translate it to firstFlight struct and ->nextFlight pointers

Answer

You want to read the flights and put them in a list. So before reading the next flight, allocate a new piece of memory and put it in next. Then use pointer notation to address the members:

void ops_loadFlightsInformation(tFlight * firstFlight)
{   
    // Open the file with r to only read
    FILE *file = fopen(OPS_FLIGHTS_FILE, "r");
    // If file is not open return
    if(file == NULL) {
        return;
    }

    tFlight *currentFlight = firstFlight;

    // Loop until there are no flights
    while(!feof(file)) {
        currentFlight->next= malloc(sizeof(tFlight));
        currentFlight= currentFlight->next;
        strcpy(currentFlight->code, helpers_scanFromFile(file, MAX_NAME_LENGTH));
        strcpy(currentFlight->departure, helpers_scanFromFile(file, MAX_NAME_LENGTH));
        strcpy(currentFlight->arrival, helpers_scanFromFile(file, MAX_NAME_LENGTH));
        fscanf(file, "%d\n", &currentFlight->availableSeats);
        fscanf(file, "%d/%02d/%02d %02d:%02d\n", &currentFlight->dateTime.year, &currentFlight->dateTime.month, &currentFlight->dateTime.day, &currentFlight->dateTime.hour, &currentFlight->dateTime.minute);
        //fscanf(file, "%d\n", &next);  // what is this?????

        printf("%s \n", currentFlight->code);
    }

    printf("%s \n", firstFlight->code);

    // Close the file handle
    fclose(file);
}

(note: check code on scanf and malloc should be added. Assumes your helpers are correct and always read the strings. Assumes the file contains at least one flight.)

Comments