我有以下代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "trips.h"
void load_fromfiles(char* argv[], tripnode* _triplist, stationnode* _stationlist)
{
FILE* tripsfptr=NULL;
tripsfptr=fopen(argv[3], "r");
if(tripsfptr==NULL)
{
printf("Error: File could not be opened\n");
exit(EXIT_FAILURE);
}
load_tripfile(&_triplist, tripsfptr);
fclose(tripsfptr);
}
void load_tripfile(tripnode** _triplist, FILE* _fp)
{
trip_data triptoread;
char separator[]=",/:";
char buffer[BUFSIZE]={'\0'};
char string1[BUFSIZE]={'\0'};
char string2[BUFSIZE]={'\0'};
char string3[BUFSIZE]={'\0'};
char* token=NULL;
char* help=NULL;
while(fgets(buffer, BUFSIZE, _fp)!=NULL)
{
sscanf(buffer, "%s %s %s", string1, string2, string3);
token=strtok(string1, separator);
triptoread.tripID= strtol(token,&help,10);
token=strtok(NULL, separator);
triptoread.timespan= strtol(token,&help,10);
token=strtok(NULL, separator);
triptoread.datebegin.month=strtol(token,&help,10);
token=strtok(NULL, separator);
triptoread.datebegin.day=strtol(token,&help,10);
token=strtok(NULL, separator);
triptoread.datebegin.year=strtol(token,&help,10);
token=strtok(string2, separator);
triptoread.timebegin.hour=strtol(token,&help,10);
token=strtok(NULL, separator);
triptoread.timebegin.minute=strtol(token,&help,10);
token=strtok(NULL, separator);
token=strtok(NULL, separator);
triptoread.stationstartID=strtol(token,&help,10);
token=strtok(NULL, separator);
triptoread.dateend.month=strtol(token,&help,10);
token=strtok(NULL, separator);
triptoread.dateend.day=strtol(token,&help,10);
token=strtok(NULL, separator);
triptoread.dateend.year=strtol(token,&help,10);
token=strtok(string3, separator);
triptoread.timeend.hour=strtol(token,&help,10);
token=strtok(NULL, separator);
triptoread.timeend.minute=strtol(token,&help,10);
token=strtok(NULL, separator);
token=strtok(NULL, separator);
triptoread.stationstopID=strtol(token,&help,10);
token=strtok(NULL, separator);
if(strcmp(token,"Casual")!=0 && strcmp(token,"Registered")!=0)
{
strcpy(triptoread.bikeID, token);
token=strtok(NULL, separator);
}
if(strcmp(token, "Casual")==0)
{
triptoread.user.type=0;
triptoread.user.birthyear=-1;
triptoread.user.gender=-1;
}
else
{
triptoread.user.type=1;
token=strtok(NULL, separator);
triptoread.user.birthyear=strtol(token,&help,10);
token=strtok(NULL, separator);
if(strcmp(token, "Male")==0)
triptoread.user.gender=1;
else
triptoread.user.gender=0;
}
*_triplist=InsertTripList(*_triplist, triptoread);
}
}
tripnode* NewTripNode(trip_data _trip)
{
tripnode* newnode=NULL;
newnode=(tripnode*)malloc(sizeof(tripnode));
if(newnode==NULL)
{
printf("Error: Memory not correctly allocated\n");
exit(EXIT_FAILURE);
}
newnode->trip_file=_trip;
newnode->next=NULL;
newnode->prev=NULL;
return newnode;
}
tripnode* InsertTripList(tripnode* _headtrip, trip_data _trip)
{
tripnode* newtailtrip=NULL;
newtailtrip=NewTripNode(_trip);
tripnode* aux=NULL;
if(_headtrip==NULL)
{
return newtailtrip;
}
aux=_headtrip;
while (aux->next!=NULL)
{
aux=aux->next;
}
aux->next=newtailtrip;
newtailtrip->prev=aux;
return _headtrip;
}
问题是:我有一行
_triplist = InsertTripList( _triplist,triptoread);
取消注释并
while (aux->next!=NULL)
{
aux=aux->next;
}
程序只显示黑屏和光标。 我试着打印一个单词,它只是无限地打印出来。 发生了什么事?
在这里,我有结构,我不知道它是否有帮助。 我还要注意,我没有任何问题
typedef struct
{
int hour;
int minute;
} ttime;
typedef struct
{
int day;
int month;
int year;
} ddate;
typedef struct
{
int type;
int birthyear;
int gender;
} person;
typedef struct
{
int tripID;
int timespan;
ttime timebegin;
ttime timeend;
ddate datebegin;
ddate dateend;
char bikeID[7];
person user;
int stationstartID;
int stationstopID;
} trip_data;
typedef struct trip
{
trip_data trip_file;
struct trip *prev;
struct trip *next;
} tripnode;