Thread: Linked List Print Error with Time Functions

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    3

    Linked List Print Error with Time Functions

    The program crashes when printing the timestamp. I believe the error is located in the function void flightRec_PrflightRecData(flightRecRead* thisFlight) which is designed to do three things:

    1. declare the time struct flighttime, flighttime is in POSIX format.
    2. Localtime converts the POSIX time to a human-readable time.
    3. The fourth specifier prints the converted time using asctime which prints it in Www Mmm dd hh:mm:ss yyyy format.

    The error is tb != NULL and shows other information specifying asctime. I am using Visual Studio 2013 Desktop Edition. This is for an intro to C class.

    What I have done to troubleshoot:
    1. Check for time header

    2. Checked pointers and addresses
    3. Turned deprecation off
    4. Checked the format specifier

    The program crash persists.

    Any assistance is appreciated. Thank you.

    Code:
    
    
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    
    
    
    typedef struct flightRec_struct { // declare a struct to match the format of the binary data
        char FlightNum[7];
        char OriginAirportCode[5];
        char DestAirportCode[5];
        int  timestamp;
    } flightRec;
    
    
    typedef struct flightRecRead_struct { // declare a struct to match the format of your linked list
        char FlightNum[7];
        char OriginAirportCode[5];
        char DestAirportCode[5];
        int  timestamp;
        struct flightRec* nextFlight_ptr;
    } flightRecRead;
    
    
    
    
    
    
    // Print dataVal
    void flightRec_PrflightRecData(flightRecRead* thisFlight) {
        struct tm *flightTime;
        flightTime = localtime(&thisFlight->timestamp);
        printf("%s \t %s \t %s \t %s\n", thisFlight->FlightNum, thisFlight->OriginAirportCode,
            thisFlight->DestAirportCode, asctime(flightTime));
        return;
    }
    
    
    // Grab location pointed by nextFlight_ptr
    flightRecRead* flightRec_GetNext(flightRecRead* thisFlight) {
        return thisFlight->nextFlight_ptr;
    }
    
    
    int main(void) {
        flightRec firstStruct;
        flightRecRead* headObj = NULL;
        flightRecRead* currObj = NULL;
        flightRecRead* tailObj = NULL;
        struct tm *flightTime;
        int i = 0;                               //loop index
    
    
    
    
        FILE* inFile = NULL;
        inFile = fopen("acars.bin", "rb");
        if (inFile == NULL) {
            printf("Could not open file acars.bin.\n");
            return -1;
        }
    
    
        if (!feof(inFile)) {
            fread(&firstStruct, sizeof(flightRec), 1, inFile); // 2. read the file into that struct
    
    
            headObj = (flightRecRead*)malloc(sizeof(flightRecRead)); // 3. make head point to that struct
            strcpy(headObj->FlightNum, firstStruct.FlightNum);
            strcpy(headObj->OriginAirportCode, firstStruct.OriginAirportCode);
            strcpy(headObj->DestAirportCode, firstStruct.DestAirportCode);
            headObj->timestamp = firstStruct.timestamp;
    
    
            tailObj = (flightRecRead*)malloc(sizeof(flightRecRead));  // 4. make tail point to that struct
            strcpy(tailObj->FlightNum, firstStruct.FlightNum);
            strcpy(tailObj->OriginAirportCode, firstStruct.OriginAirportCode);
            strcpy(tailObj->DestAirportCode, firstStruct.DestAirportCode);
            tailObj->timestamp = firstStruct.timestamp;
            headObj->nextFlight_ptr = tailObj;
            tailObj->nextFlight_ptr = NULL;
        }
    
    
        while (!feof(inFile)) {                       // 5. while not end-of-file on the acars file:
            fread(&firstStruct, sizeof(flightRec), 1, inFile); // 6. malloc a new struct
            currObj = (flightRecRead*)malloc(sizeof(flightRecRead));
            strcpy(currObj->FlightNum, firstStruct.FlightNum);
            strcpy(currObj->OriginAirportCode, firstStruct.OriginAirportCode);
            strcpy(currObj->DestAirportCode, firstStruct.DestAirportCode);
            currObj->timestamp = firstStruct.timestamp;
            currObj->nextFlight_ptr = NULL;
    
    
            tailObj->nextFlight_ptr = currObj;
            tailObj = currObj;
        }
    
    
    
    
        currObj = headObj;                           // Print the list
        printf("FlightNum \t OriginAirportCode \t DestAirportCode \t Time \t \n");
        while (currObj != NULL) {
            flightRec_PrflightRecData(currObj);
            currObj = flightRec_GetNext(currObj);
        }
    
    
        system("pause"); //return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > flightTime = localtime(&thisFlight->timestamp);
    You should check that flightTime is a valid pointer, and not NULL

    You should also check whether 'int' and 'time_t' are the same thing on your machine. Your timestamp is an integer, yet you're pretending it's a time_t with &thisFlight->timestamp

    > strcpy(currObj->FlightNum, firstStruct.FlightNum);
    Does your acars file format specify that each string is terminated with a \0?
    If it doesn't, then strcpy, and printing with "%s" is broken.

    FAQ > Casting malloc - Cprogramming.com
    FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com
    Since you're using visual studio, you need to make sure your source file is prog.c, and not prog.cpp (the default).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked list print help
    By Roadrunner2015 in forum C Programming
    Replies: 1
    Last Post: 03-29-2014, 12:12 AM
  2. Linked List won't print
    By jrclark in forum C Programming
    Replies: 9
    Last Post: 10-31-2012, 11:48 PM
  3. Linked List Run Time Error
    By Frost Drake in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2006, 02:30 AM
  4. file & linked list run time error
    By Micko in forum C Programming
    Replies: 9
    Last Post: 03-06-2004, 02:58 AM
  5. Linked List print out
    By axon in forum C++ Programming
    Replies: 6
    Last Post: 10-19-2003, 07:15 PM

Tags for this Thread