0

I am trying to use time.h to convert a POSIX time into a time structure, thus making the string human readable. The string is located in a .bin file I have. I understand how to use it outside of a function, such as

 time_t rawtime;
 time (&rawtime);
 printf ("The current local time is: %s", ctime (&rawtime))

But I am struggling how to use in an array created by a structure. Here is my code.

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

typedef struct MyStruct_struct{
    char FlightNum[7];
    char OriginAirportCode[5];
    char DestAirportCode[5];
    unsigned timestamp;
} Flights;


Flights Order[5000];

int compare (const void *v1, const void *v2)
{
    int result;
    const Flights *ia = (Flights *)v1;
    const Flights *ib = (Flights *)v2;
    result = strcmp(ia->OriginAirportCode, ib->OriginAirportCode);
    return result;
}

int main()
{
    int i=0;
    int count = 0;
    Flights * File;
    FILE * bin;

    bin = fopen("acars.bin", "rb");

    while (!feof(bin)) {
        fread(Order + count, sizeof(Flights), 1, bin);
        ++count;
    }

    qsort(Order, count, sizeof(Flights), compare);

    for (i = 0; i < 10; i++)
    {
        time_t fdate = (time_t)Order[i]->timestamp;
        printf("%i) %s, %s, %s, %s\n",
                i,
                Order[i].FlightNum,
                Order[i].OriginAirportCode,
                Order[i].DestAirportCode,
                Order[i].timestamp,
                ctime(&fdate));

    }
    fclose(bin);

    return 0;
}

The errors I get are: Error 2 error C2232: '->timestamp' : left operand has 'struct' type, use '.'
3 IntelliSense: expression must have pointer type

If put ctime in the structure, I get an error as well (as expected).

Dummy00001
  • 16,630
  • 5
  • 41
  • 63
Patrick Daniels
  • 51
  • 1
  • 1
  • 7

0 Answers0