链接列表打印错误使用时间函数 [英] Linked List Print Error Using Time Functions

查看:118
本文介绍了链接列表打印错误使用时间函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序打印时间戳时崩溃。我相信错误位于函数void flightRec_PrflightRecData(flightRecRead * thisFlight),其目的是做三件事情:


  1. 申报时间结构flighttime,flighttime是POSIX格式。

  2. LOCALTIME转换POSIX时间到人类可读的时间。

  3. MM:
  4. 使用asctime它打印它万维网嗯DD HH第四符打印转换后的时间。SS yyyy格式

该错误是TB!= NULL,并显示其他信息,指定asctime。

我做了什么来解决:


  1. 检查时间标题

  2. 经过指针和地址

  3. 转身去precation关闭

  4. 经过格式说明

任何援助AP preciated。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&time.h中GT;
typedef结构flightRec_struct {//声明一个结构相匹配的二进制数据格式
    炭FlightNum [7];
    焦炭OriginAirport code [5];
    焦炭DestAirport code [5];
    INT时间戳;
} flightRec;typedef结构flightRecRead_struct {//声明一个结构来匹配您的链接列表的格式
    炭FlightNum [7];
    焦炭OriginAirport code [5];
    焦炭DestAirport code [5];
    INT时间戳;
    结构flightRec * nextFlight_ptr;
} flightRecRead;//打印dataVal
无效flightRec_PrflightRecData(flightRecRead * thisFlight){
    结构TM * flightTime;
    flightTime =本地时间(安培; thisFlight->时间戳);
    的printf(%S \\ t%S \\ t%S \\ t%S \\ n,thisFlight-> FlightNum,thisFlight-> OriginAirport code,
        thisFlight-> DestAirport code,asctime(flightTime));
    返回;
}//通过nextFlight_ptr指出抢位置
flightRecRead * flightRec_GetNext(flightRecRead * thisFlight){
    返回thisFlight-> nextFlight_ptr;
}诠释主要(无效){
    flightRec firstStruct;
    flightRecRead * headObj = NULL;
    flightRecRead * currObj = NULL;
    flightRecRead * tailObj = NULL;
    结构TM * flightTime;
    INT I = 0; //循环索引
    FILE * INFILE = NULL;
    INFILE = FOPEN(acars.bin,RB);
    如果(INFILE == NULL){
        的printf(无法打开文件acars.bin \\ n);
        返回-1;
    }    如果(!的feof(INFILE)){
        FREAD(安培; firstStruct,sizeof的(flightRec),1,INFILE); // 2.文件读入到该结构        headObj =(flightRecRead *)malloc的(的sizeof(flightRecRead)); // 3.化妆头指向该结构
        的strcpy(headObj-> FlightNum,firstStruct.FlightNum);
        的strcpy(headObj-> OriginAirport code,firstStruct.OriginAirport code);
        的strcpy(headObj-> DestAirport code,firstStruct.DestAirport code);
        headObj->时间戳= firstStruct.timestamp;        tailObj =(flightRecRead *)malloc的(的sizeof(flightRecRead)); // 4.化妆尾指向该结构
        的strcpy(tailObj-> FlightNum,firstStruct.FlightNum);
        的strcpy(tailObj-> OriginAirport code,firstStruct.OriginAirport code);
        的strcpy(tailObj-> DestAirport code,firstStruct.DestAirport code);
        tailObj->时间戳= firstStruct.timestamp;
        headObj-> nextFlight_ptr = tailObj;
        tailObj-> nextFlight_ptr = NULL;
    }    而{// 5.虽然没有档案结尾的ACARS文件(的feof(INFILE)!):
        FREAD(安培; firstStruct,sizeof的(flightRec),1,INFILE); // 6. malloc的一个新的结构
        currObj =(flightRecRead *)malloc的(的sizeof(flightRecRead));
        的strcpy(currObj-> FlightNum,firstStruct.FlightNum);
        的strcpy(currObj-> OriginAirport code,firstStruct.OriginAirport code);
        的strcpy(currObj-> DestAirport code,firstStruct.DestAirport code);
        currObj->时间戳= firstStruct.timestamp;
        currObj-> nextFlight_ptr = NULL;        tailObj-> nextFlight_ptr = currObj;
        tailObj = currObj;
    }
    currObj = headObj; //打印列表
    的printf(FlightNum \\ t OriginAirport code \\ t DestAirport code \\ t时间\\ t \\ n);
    而(currObj!= NULL){
        flightRec_PrflightRecData(currObj);
        currObj = flightRec_GetNext(currObj);
    }    系统(暂停); //返回0;
}


解决方案

当我编译一个64位的Mac上code。与GCC 5.1.0,它给了我一些错误。他们中的一些,不过,是我用的是非常严格的编译选项的后果:

  $ gcc的-O3 -g -std = C11 -Wall -Wextra -Wmissing的原型-Wstrict的原型\\
> -WOLD式高清-Werror timecrash.c -o timecrash
timecrash.c:25:6:错误:没有previous原型'flightRec_PrflightRecData'[-Werror =丢失的原型]
 无效flightRec_PrflightRecData(flightRecRead * thisFlight){
      ^
timecrash.c:在函数'flightRec_PrflightRecData:
timecrash.c:27:28:错误:从兼容的指针类型传递'本地时间'的参数1 [-Werror =不兼容的指针类型]
     flightTime =本地时间(安培; thisFlight->时间戳);
                            ^
4:0在文件从timecrash.c包括:
/usr/include/time.h:112:12:注:应为常量time_t的* {又名常量是long int *},但参数的类型为INT *
 结构TM *本地时间(常量的time_t *);
            ^
timecrash.c:在顶层:
timecrash.c:34:16:错误:没有previous原型'flightRec_GetNext'[-Werror =丢失的原型]
 flightRecRead * flightRec_GetNext(flightRecRead * thisFlight){
                ^
timecrash.c:在函数'flightRec_GetNext:
timecrash.c:35:12:错误:从回报不兼容的指针类型[-Werror =不兼容的指针类型]
     返回thisFlight-> nextFlight_ptr;
            ^
timecrash.c:在函数'主':
timecrash.c:68:33:错误:分配从兼容的指针类型[-Werror =不兼容的指针类型]
         headObj-> nextFlight_ptr = tailObj;
                                 ^
timecrash.c:81:33:错误:分配从兼容的指针类型[-Werror =不兼容的指针类型]
         tailObj-> nextFlight_ptr = currObj;
                                 ^
timecrash.c:44:9:错误:未使用的变量'我'[-Werror =未使用的变量]
     INT I = 0; //循环索引
         ^
timecrash.c:43:16:错误:未使用的变量'flightTime'[​​-Werror =未使用的变量]
     结构TM * flightTime;
                ^
CC1:所有的警告被视为错误
$

在'未使用的变量和无previous原型'错误(将是除了我用的警告 -Werror 来迫使被视为错误的警告)。

INT 作为对 time_t的替代滥用可能是你的问题太多。如果您编译为32位平台可能不再是一个问题。

其他不兼容的指针类型的警告令人担忧了。事实上,你的第二个结构定义不正确。您有:

  typedef结构flightRec_struct {
    炭FlightNum [7];
    焦炭OriginAirport code [5];
    焦炭DestAirport code [5];
    INT时间戳;
} flightRec;typedef结构flightRecRead_struct {
    炭FlightNum [7];
    焦炭OriginAirport code [5];
    焦炭DestAirport code [5];
    INT时间戳;
    结构flightRec * nextFlight_ptr;
} flightRecRead;

结构flightRec * 是指向不完全类型。这不是一个结构flightRec_struct ,也不是 flightRec (也不是结构flightRecRead_struct ,也不是 flightRecRead )。事实上,你需要它应该是结构flightRecRead_struct * 或你需要predeclare类型和使用 flightRecRead *

  typedef结构flightRecRead_struct flightRecRead;
结构flightRecRead_struct {
    炭FlightNum [7];
    焦炭OriginAirport code [5];
    焦炭DestAirport code [5];
    INT时间戳;
    flightRecRead * nextFlight_ptr;
};

您可以通过使用简化您的code的赋值部分:

 结构flightRecRead_struct
{
    flightRec飞行;
    flightRecRead * nextFlight_ptr;
};

然后,您可以分配整个 flightRec 结构在一个单一的线,而不是写出来的多个的strcpy()操作,等等。

在此,如果你需要使用 time_t的,这是不是大小为 INT 和相同二进制格式外部固定的,那么你可能需要毕竟做会员明智的复制。

您应该了解为什么 而(!的feof(文件) )永远是错的


半固定的code: timecrash.c

这code对我来说或多或少的作品。

 的#include<&ASSERT.H GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&time.h中GT;typedef结构flightRec_struct
{
    炭FlightNum [7];
    焦炭OriginAirport code [5];
    焦炭DestAirport code [5];
    time_t的时间戳;
} flightRec;typedef结构flightRecRead_struct flightRecRead;
结构flightRecRead_struct
{
    炭FlightNum [7];
    焦炭OriginAirport code [5];
    焦炭DestAirport code [5];
    time_t的时间戳;
    flightRecRead * nextFlight_ptr;
};静态的
无效flightRec_PrflightRecData(flightRecRead * thisFlight)
{
    结构TM * flightTime =本地时间(安培; thisFlight->时间戳);
    的printf(时间戳=为0x%.8lX \\ n,(长)thisFlight->时间戳);
    断言(flightTime!= 0);
    的printf(%S \\ t%S \\ t%S \\ t%S \\ n,thisFlight-> FlightNum,thisFlight-> OriginAirport code,
           thisFlight-> DestAirport code,asctime(flightTime));
}静态的
flightRecRead * flightRec_GetNext(flightRecRead * thisFlight)
{
    返回thisFlight-> nextFlight_ptr;
}INT主要(无效)
{
    flightRec firstStruct;
    flightRecRead * headObj = NULL;
    flightRecRead * currObj = NULL;
    flightRecRead * tailObj = NULL;
    为const char文件名[] =acars.bin;
    INT RECNUM = 0;
    FILE * INFILE = FOPEN(文件名,RB);    如果(INFILE == NULL)
    {
        fprintf中(标准错误,无法打开文件%s \\ n,文件名);
        返回-1;
    }    如果(的fread(安培; firstStruct,sizeof的(flightRec),1,INFILE)== 1)
    {
        的printf(记录%d个\\ n,++ RECNUM);
        headObj =(flightRecRead *)malloc的(的sizeof(flightRecRead));
        的strcpy(headObj-> FlightNum,firstStruct.FlightNum);
        的strcpy(headObj-> OriginAirport code,firstStruct.OriginAirport code);
        的strcpy(headObj-> DestAirport code,firstStruct.DestAirport code);
        headObj->时间戳= firstStruct.timestamp;
        flightRec_PrflightRecData(headObj);        tailObj =(flightRecRead *)malloc的(的sizeof(flightRecRead));
        的strcpy(tailObj-> FlightNum,firstStruct.FlightNum);
        的strcpy(tailObj-> OriginAirport code,firstStruct.OriginAirport code);
        的strcpy(tailObj-> DestAirport code,firstStruct.DestAirport code);
        tailObj->时间戳= firstStruct.timestamp;
        flightRec_PrflightRecData(tailObj);        headObj-> nextFlight_ptr = tailObj;
        tailObj-> nextFlight_ptr = NULL;
    }    而(的fread(安培; firstStruct,sizeof的(flightRec),1,INFILE)== 1)
    {
        的printf(记录%d个\\ n,++ RECNUM);
        currObj =(flightRecRead *)malloc的(的sizeof(flightRecRead));
        的strcpy(currObj-> FlightNum,firstStruct.FlightNum);
        的strcpy(currObj-> OriginAirport code,firstStruct.OriginAirport code);
        的strcpy(currObj-> DestAirport code,firstStruct.DestAirport code);
        currObj->时间戳= firstStruct.timestamp;
        currObj-> nextFlight_ptr = NULL;
        flightRec_PrflightRecData(currObj);        tailObj-> nextFlight_ptr = currObj;
        tailObj = currObj;
    }
    的printf(读完\\ n);    currObj = headObj;
    的printf(FlightNum \\ t OriginAirport code \\ t DestAirport code \\ t时间\\ t \\ n);
    RECNUM = 0;
    而(currObj!= NULL)
    {
        的printf(记录%d个\\ n,++ RECNUM);
        flightRec_PrflightRecData(currObj);
        currObj = flightRec_GetNext(currObj);
    }    FCLOSE(INFILE);    系统(暂停);
}

采样运行

数据生成假定 time_t的是一个8字节边界上(7空填充前字节吧)对齐的64位类型,在小尾数格式(如英特尔)。

  $ printf的BA7231 \\ 0LHR \\ 0 \\ 0LGW \\ 0 \\ 0 \\ 0 \\ 0 \\ 0 \\ 0 \\ 0 \\ 0 \\ 0 \\ XA2 \\ X93 \\ X84 \\ X75 \\ 0 \\ 0 \\ 0 \\ 0> acars.bin
$ printf的UA9240 \\ 0LAX \\ 0 \\ 0AMS \\ 0 \\ 0 \\ 0 \\ 0 \\ 0 \\ 0 \\ 0 \\ 0 \\ 0 \\ X72 \\ X93 \\ X84 \\ X75 \\ 0 \\ 0 \\ 0 \\ 0>> acars.bin
$ ODX acars.bin
0×0000:42 41 37 32 33 31 00 48 4C 52 00 00 47 4C 57 00 BA7231.LHR..LGW。
0×0010:00 00 00 00 00 00 00 00 93 A2 75 84 00 00 00 00 ...........ü....
0×0020:55 41 39 32 34 30 00 41 4C 58 00 00 41 53 4D 00 UA9240.LAX..AMS。
0x0030:00 00 00 00 00 00 00 00 72 93 84 75 00 00 00 00 ........ r..u ....
×0040:
$ ./timecrash
记录1
时间戳= 0x758493A2
BA7231 LHR LGW周三6月23日10时00分18秒2032时间戳= 0x758493A2
BA7231 LHR LGW周三6月23日10时00分18秒2032记录2
时间戳= 0x75849372
UA9240 LAX AMS周三06月23日9时59分三十秒2032读完
FlightNum OriginAirport code DestAirport code时间
记录1
时间戳= 0x758493A2
BA7231 LHR LGW周三6月23日10时00分18秒2032记录2
时间戳= 0x758493A2
BA7231 LHR LGW周三6月23日10时00分18秒2032记录3
时间戳= 0x75849372
UA9240 LAX AMS周三06月23日9时59分三十秒2032^ C
$

因为我的暂停 Mac OS X上不会终止,直到中断(或以其他方式发送的信号)的中断是必要的。

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.

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

Any assistance is appreciated.

#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;
}

解决方案

When I compile your code on a 64-bit Mac with GCC 5.1.0, it gives me a number of errors. Some of them, though, are consequences of the very stringent compilation options I use:

$ gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
>     -Wold-style-definition -Werror timecrash.c -o timecrash
timecrash.c:25:6: error: no previous prototype for ‘flightRec_PrflightRecData’ [-Werror=missing-prototypes]
 void flightRec_PrflightRecData(flightRecRead* thisFlight) {
      ^
timecrash.c: In function ‘flightRec_PrflightRecData’:
timecrash.c:27:28: error: passing argument 1 of ‘localtime’ from incompatible pointer type [-Werror=incompatible-pointer-types]
     flightTime = localtime(&thisFlight->timestamp);
                            ^
In file included from timecrash.c:4:0:
/usr/include/time.h:112:12: note: expected ‘const time_t * {aka const long int *}’ but argument is of type ‘int *’
 struct tm *localtime(const time_t *);
            ^
timecrash.c: At top level:
timecrash.c:34:16: error: no previous prototype for ‘flightRec_GetNext’ [-Werror=missing-prototypes]
 flightRecRead* flightRec_GetNext(flightRecRead* thisFlight) {
                ^
timecrash.c: In function ‘flightRec_GetNext’:
timecrash.c:35:12: error: return from incompatible pointer type [-Werror=incompatible-pointer-types]
     return thisFlight->nextFlight_ptr;
            ^
timecrash.c: In function ‘main’:
timecrash.c:68:33: error: assignment from incompatible pointer type [-Werror=incompatible-pointer-types]
         headObj->nextFlight_ptr = tailObj;
                                 ^
timecrash.c:81:33: error: assignment from incompatible pointer type [-Werror=incompatible-pointer-types]
         tailObj->nextFlight_ptr = currObj;
                                 ^
timecrash.c:44:9: error: unused variable ‘i’ [-Werror=unused-variable]
     int i = 0;                               //loop index
         ^
timecrash.c:43:16: error: unused variable ‘flightTime’ [-Werror=unused-variable]
     struct tm *flightTime;
                ^
cc1: all warnings being treated as errors
$

The 'unused variables' and 'no previous prototype' errors (would be warnings except that I used -Werror to force all warnings to be treated as errors).

But the misuse of int as a surrogate for time_t could be a problem for you too. It may be less of an issue if you compile for a 32-bit platform.

The other 'incompatible pointer type' warnings are worrying, too. In fact, your second structure definition is incorrect. You have:

typedef struct flightRec_struct {
    char FlightNum[7];
    char OriginAirportCode[5];
    char DestAirportCode[5];
    int  timestamp;
} flightRec;

typedef struct flightRecRead_struct {
    char FlightNum[7];
    char OriginAirportCode[5];
    char DestAirportCode[5];
    int  timestamp;
    struct flightRec* nextFlight_ptr;
} flightRecRead;

The struct flightRec * is a pointer to an incomplete type. It is not a struct flightRec_struct nor a flightRec (nor a struct flightRecRead_struct nor a flightRecRead). In fact, you need it to be either a struct flightRecRead_struct * or you need to predeclare the type and use a flightRecRead *:

typedef struct flightRecRead_struct flightRecRead;
struct flightRecRead_struct {
    char FlightNum[7];
    char OriginAirportCode[5];
    char DestAirportCode[5];
    int  timestamp;
    flightRecRead *nextFlight_ptr;
};

You could simplify the assignment part of your code by using:

struct flightRecRead_struct
{
    flightRec      flight;
    flightRecRead *nextFlight_ptr;
};

You could then assign the entire flightRec structure in a single line, rather than having to write out the multiple strcpy() operations, etc.

Against this, if you need to use time_t and that is not the same size as an int and the binary format is fixed externally, then you may need to do the member-wise copying after all.

You should learn why while (!feof(file)) is always wrong.


Semi-fixed code: timecrash.c

This code more or less works for me.

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

typedef struct flightRec_struct
{
    char FlightNum[7];
    char OriginAirportCode[5];
    char DestAirportCode[5];
    time_t timestamp;
} flightRec;

typedef struct flightRecRead_struct flightRecRead;
struct flightRecRead_struct
{
    char FlightNum[7];
    char OriginAirportCode[5];
    char DestAirportCode[5];
    time_t timestamp;
    flightRecRead *nextFlight_ptr;
};

static
void flightRec_PrflightRecData(flightRecRead *thisFlight)
{
    struct tm *flightTime = localtime(&thisFlight->timestamp);
    printf("timestamp = 0x%.8lX\n", (long)thisFlight->timestamp);
    assert(flightTime != 0);
    printf("%s \t %s \t %s \t %s\n", thisFlight->FlightNum, thisFlight->OriginAirportCode,
           thisFlight->DestAirportCode, asctime(flightTime));
}

static
flightRecRead *flightRec_GetNext(flightRecRead *thisFlight)
{
    return thisFlight->nextFlight_ptr;
}

int main(void)
{
    flightRec firstStruct;
    flightRecRead *headObj = NULL;
    flightRecRead *currObj = NULL;
    flightRecRead *tailObj = NULL;
    const char filename[] = "acars.bin";
    int recnum = 0;
    FILE *inFile = fopen(filename, "rb");

    if (inFile == NULL)
    {
        fprintf(stderr, "Could not open file %s.\n", filename);
        return -1;
    }

    if (fread(&firstStruct, sizeof(flightRec), 1, inFile) == 1)
    {
        printf("Record %d\n", ++recnum);
        headObj = (flightRecRead *)malloc(sizeof(flightRecRead));
        strcpy(headObj->FlightNum, firstStruct.FlightNum);
        strcpy(headObj->OriginAirportCode, firstStruct.OriginAirportCode);
        strcpy(headObj->DestAirportCode, firstStruct.DestAirportCode);
        headObj->timestamp = firstStruct.timestamp;
        flightRec_PrflightRecData(headObj);

        tailObj = (flightRecRead *)malloc(sizeof(flightRecRead));
        strcpy(tailObj->FlightNum, firstStruct.FlightNum);
        strcpy(tailObj->OriginAirportCode, firstStruct.OriginAirportCode);
        strcpy(tailObj->DestAirportCode, firstStruct.DestAirportCode);
        tailObj->timestamp = firstStruct.timestamp;
        flightRec_PrflightRecData(tailObj);

        headObj->nextFlight_ptr = tailObj;
        tailObj->nextFlight_ptr = NULL;
    }

    while (fread(&firstStruct, sizeof(flightRec), 1, inFile) == 1)
    {
        printf("Record %d\n", ++recnum);
        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;
        flightRec_PrflightRecData(currObj);

        tailObj->nextFlight_ptr = currObj;
        tailObj = currObj;
    }
    printf("Finished reading\n");

    currObj = headObj;
    printf("FlightNum \t OriginAirportCode \t DestAirportCode \t Time \t \n");
    recnum = 0;
    while (currObj != NULL)
    {
        printf("Record %d\n", ++recnum);
        flightRec_PrflightRecData(currObj);
        currObj = flightRec_GetNext(currObj);
    }

    fclose(inFile);

    system("pause");
}

Sample run

The data generation assumes time_t is a 64-bit type aligned on an 8-byte boundary (7 null padding bytes before it), and is formatted in little-endian (as on Intel).

$ printf "BA7231\0LHR\0\0LGW\0\0\0\0\0\0\0\0\0\xA2\x93\x84\x75\0\0\0\0" > acars.bin
$ printf "UA9240\0LAX\0\0AMS\0\0\0\0\0\0\0\0\0\x72\x93\x84\x75\0\0\0\0" >> acars.bin
$ odx acars.bin
0x0000: 42 41 37 32 33 31 00 4C 48 52 00 00 4C 47 57 00   BA7231.LHR..LGW.
0x0010: 00 00 00 00 00 00 00 00 A2 93 84 75 00 00 00 00   ...........u....
0x0020: 55 41 39 32 34 30 00 4C 41 58 00 00 41 4D 53 00   UA9240.LAX..AMS.
0x0030: 00 00 00 00 00 00 00 00 72 93 84 75 00 00 00 00   ........r..u....
0x0040:
$ ./timecrash
Record 1
timestamp = 0x758493A2
BA7231   LHR     LGW     Wed Jun 23 10:00:18 2032

timestamp = 0x758493A2
BA7231   LHR     LGW     Wed Jun 23 10:00:18 2032

Record 2
timestamp = 0x75849372
UA9240   LAX     AMS     Wed Jun 23 09:59:30 2032

Finished reading
FlightNum    OriginAirportCode   DestAirportCode     Time    
Record 1
timestamp = 0x758493A2
BA7231   LHR     LGW     Wed Jun 23 10:00:18 2032

Record 2
timestamp = 0x758493A2
BA7231   LHR     LGW     Wed Jun 23 10:00:18 2032

Record 3
timestamp = 0x75849372
UA9240   LAX     AMS     Wed Jun 23 09:59:30 2032

^C
$

The interrupt was necessary because my pause on Mac OS X doesn't terminate until interrupted (or otherwise sent a signal).

这篇关于链接列表打印错误使用时间函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆