C programming, dynamic allocation + linked lists

I am having problems with this code and I am not sure what I am doing wrong

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

typedef struct flight_struct{
    char flightNum[7];
    char originAirport[5];
    char destAirport [5];
    int timestamp;
    struct flight_struct *next;
} flightRec;

int main(){
struct flight_struct *head; // unchanging first node.
struct flight_struct *tail; //the conductor.
struct flight_struct *p; // first new struct
FILE* binFile = fopen("acars.bin","r");
FILE* DataOut;

    p =(struct flight_struct*) malloc(sizeof(*p) + 1); //malloc the first struct\

    fread(p,sizeof(*p),1,binFile);  //read the file into it.

    head = p; //make head point to that struct
    tail = p; //make tail point to that struct

//  fclose(binFile);

    while (feof(binFile) == 0){
    flight_struct *temp = (struct flight_struct*) malloc(1*sizeof(*temp) + 1); //malloc a new struct
    fread(temp,sizeof(*temp),1,binFile); //read the next struct from acars.bin into the structure you malloc'ed
    temp -> next = NULL; // add that struct to your linked list using the next memeber of the struct
    tail -> next = temp; // set tail to point to the element you just added
    tail = tail -> next;
    } //while not eof on acars file

    tail = head;

    while(tail -> next != 0 ){  
        int t;
        t = tail -> timestamp;
        time_t tim = t;
        printf("%s, %s, %s, %s\n\n",tail -> flightNum,tail -> originAirport,tail -> destAirport,asctime(gmtime(&tim)));
        tail = tail -> next;
    } //starting at head traverse the list printing the leemnts of each strucure
}

      

Now what I get as a result is What i'm getting

What I have to get is What I should be getting

To be honest, I don't know what I am doing wrong and the help would be nice. That being said, I cannot use arrays, so a linked list is the only way to do it.

+3


source to share


3 answers


Here's a solution that works:

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

typedef struct flight_struct{
    char flightNum[7],originAirport[5], destAirport [5];
    size_t timestamp;
    struct flight_struct *next;
} flightRec;


int readFlight(FILE* file, flightRec *fr);

int main() {
    FILE *f = fopen("input.txt", "r");
    if (f != NULL) {
        while (feof(f) == 0) {
            flightRec fr;
            if (readFlight(f, &fr) == 0) {

                printf("%s, %s, %s, %s\n", fr.flightNum, fr.originAirport,
                        fr.destAirport, ctime((time_t*)(&fr.timestamp)));
            }
            else {
                fprintf(stderr, "Couldn't read the file information");
                break;
            }
        }
    }

    fclose(f);
    return 0;
}

int readFlight(FILE* file, flightRec *fr) {
    struct tm tstr;
    char *buffer = NULL, time[40];
    size_t len = 0;
    getline(&buffer, &len, file);

    int v = sscanf(buffer, "%6s%*[^ ]%4s%*[^ ]%4s%*[^a-zA-Z]%[^\n\r]", fr->flightNum, fr->originAirport,
                                                fr->destAirport, time);
    free(buffer);
    if (v == 4) {
        strptime(time, "%a %b %d %T %Y", &tstr);
        tstr.tm_isdst = -1;
        fr->timestamp = (size_t)mktime(&tstr);
        return 0;
    }
    return -1;
}

      

Input file:

XE4608, KIAH, KRSW, Mon Oct 21 15:25:00 2013
XE4232, KSDH, KASW, Sat Mar 29 16:38:00 2014
XE3453, MASH, KRSW, Wed Jan 01 19:10:00 2014
ZF4608, SAAH, KRSW, Tue Mar 25 18:49:00 2014

      



Output:

XE4608, KIAH, KRSW, Mon Oct 21 15:25:00 2013

XE4232, KSDH, KASW, Sat Mar 29 16:38:00 2014

XE3453, MASH, KRSW, Wed Jan  1 19:10:00 2014

ZF4608, SAAH, KRSW, Tue Mar 25 18:49:00 2014

      

Made the structure change to timestamp

be size_t

, not int

.

+1


source


You shouldn't have pointers in your file.

Because the order of the items in the file automatically sets the order in your list (obviously). I doubt the file contains pointers to sctructure and everything is fine (pointers may differ across architectures, ta-da).

What to do?

Given data structure:

struct flight_struct {
  char flightNum[7];
  char originAirport[5];
  char destAirport [5];
  int timestamp;
}

      



Implement list structure:

struct list {
  flight_struct* data;
  list* next;
  list* prev; //if you want bi-directional list
}

      

And load from file only data structure to list structure.

The pointers in the binary are incorrect and can cause a lot of problems. Did you create a file with objects yourself or from another source?

+2


source


Are you sure the file contains the complete structure? Even with pointer space?

And not your connection? It shouldn't be:

temp-> next = null;
if ( tail == null )
{
    head = temp;
    tail = temp;
}
else
{ 
   tail-> next = temp;
   tail = temp;
}

      

0


source







All Articles