gpt4 book ai didi

c - 链表: Reading in txt file,节点不保存

转载 作者:行者123 更新时间:2023-11-30 14:25:40 27 4
gpt4 key购买 nike

我在编码方面相当陌生,并且被赋予了使用链表和结构的艰巨任务。任务是创建一个音乐数据库,以便您可以读取艺术家的专辑和轨道,并将它们存储在节点中。

目前,当我运行该程序时,当我从 .txt 文件读取数据时,不会存储任何节点。最终,我开始怀疑我对某些指针的使用是否正确,因为我遇到了段错误。我做错了什么,我可以修复它以使其正常工作吗?

我的代码(所需的库已包含):

结构体和宏定义:

#define LINEBUFFERSIZE 256

struct song
{
char *songName_p;
int trackNumber;
struct song *nextSong_p;
};

struct disc
{
char *discName_p;
int year;
struct song *song_p;
struct disc *nextDisc_p;

};

struct artist
{
char name[20];
char *artistName_p;
struct disc *disc_p;
struct artist *nextArtist_p;
};
struct artist *end = (struct artist *) NULL; //NEW
struct artist *startPtr = (struct artist *) NULL; //NEW
struct artist *find(struct artist *, char * );//NEW
//NEW

类型定义:

typedef struct artist artist_t;
typedef struct disc disc_t;
typedef struct song song_t;

typedef struct artist *artistNodePtr;
typedef struct disc *discNodePtr;
typedef struct song *songNodePtr;

函数定义:

void InsertArtist(struct artist *New);
artistNodePtr initializenode(char *name);
artistNodePtr findOrInsertArtist(artistNodePtr *sPtr, char *name);
discNodePtr findOrInsertDisc(discNodePtr *sPtr, char *discID, int releaseYear);
void findOrInsertSong(songNodePtr *sPtr, char *songID, int trackID);
void getNextLine(char buffer[], int bufferSize, FILE *fptr);
void printlist( struct artist *ptr );
void printnode(struct artist *ptr);

主要方法:

int main(int argc, char *argv[])
{
char name[20];
struct artist *newNodePointer;
char lineBuffer[LINEBUFFERSIZE];
artistNodePtr startPtr = NULL; /* initially the artist list is empty */
FILE *musicFile;
char *artistTemp, *discTemp, *yearTemp, *trackTemp, *songTemp;
int year, track, menu = 1;
artistNodePtr theArtist;
// discNodePtr theDisc;

if (argc==1)
{
printf(" Must supply a file name as command line argument/n");
return 0;
}

if ((musicFile = fopen(argv[1], "r")) == NULL)
{
printf ("Error opening music file. Program terminated/n");
return 0;
}

getNextLine(lineBuffer, LINEBUFFERSIZE, musicFile);
while (!feof(musicFile))
{
artistTemp = strtok(lineBuffer,";");
if (artistTemp == NULL)
{
printf("Error parsing input file; Program is terminated\n");
return 0;
}
discTemp = strtok(NULL ,";");
if (discTemp == NULL)
{
printf("Error parsing input file; Program is terminated\n");
return 0;
}
yearTemp = strtok(NULL ,";");
if (yearTemp == NULL)
{
printf("Error parsing input file; Program is terminated\n");
return 0;
}
trackTemp = strtok(NULL ,";");
if (trackTemp == NULL)
{
printf("Error parsing input file; Program is terminated\n");
return 0;
}
songTemp = strtok(NULL ,"\n");
if (songTemp == NULL)
{
printf("Error parsing input file; Program is terminated\n");
return 0;
}
year = atoi(yearTemp);
track = atoi(trackTemp);
theArtist = findOrInsertArtist(&startPtr, artistTemp);
// theDisc = findOrInsertDisc(&(theArtist->disc_p), discTemp, year);
//findOrInsertSong(&(theDisc->song_p), songTemp, track);
getNextLine(lineBuffer, LINEBUFFERSIZE, musicFile);
} /* end of while loop */

while (menu != 0)
{
printf("1 to display entire catalog \n");
printf("2 to display alls songs by a given artist\n");
printf("3 to display all songs on a given disc\n");
printf("0 to exit the library\n ");
scanf("%d", &menu);
switch (menu){
case 1: printlist(startPtr);
break;

case 2:
printf("enter an artist name");
scanf("%s", name );
newNodePointer = find(startPtr, name );
if (newNodePointer==NULL)
{
newNodePointer = initializenode(name );
InsertArtist(newNodePointer);
}

}
} /* end main */
}

函数定义:

artistNodePtr findOrInsertArtist(artistNodePtr *sPtr, char *name )
{

sPtr = initializenode(name);
InsertArtist(sPtr);

if(!sPtr)
sPtr = find(startPtr, name);
return sPtr;
}
void printlist( struct artist *ptr ){
while(ptr!= NULL){
printnode(ptr);
ptr = ptr->nextArtist_p;
}
}
void printnode(struct artist *ptr)
{
printf("Name %s\n", ptr->name);

}

artistNodePtr initializenode(char *name)
{

struct artist *newNodePtr = (artistNodePtr)malloc(sizeof(artist_t));
if (newNodePtr != NULL)
{
newNodePtr->artistName_p = (char*)malloc((strlen(name)+1)*sizeof(char));
if (newNodePtr->artistName_p != NULL)
{
strcpy(newNodePtr->artistName_p, name);
newNodePtr->nextArtist_p = NULL;
newNodePtr->disc_p = NULL;

return newNodePtr;
}
}
}

void InsertArtist(struct artist *New)
{
//NEW
struct artist *temp, *prev;

if(startPtr == NULL)
{
startPtr = New;
end = New;
startPtr->nextArtist_p = NULL;
return;
}
temp = startPtr;

while(strcmp(temp->name, New->name) < 0)
{
temp = temp->nextArtist_p;
if(temp == NULL)
break;
}
if(temp == startPtr)
{
New->nextArtist_p = startPtr;
startPtr = New;
}
else
{
prev = startPtr;
while(prev->nextArtist_p != temp)
{
prev = prev->nextArtist_p;
}
prev->nextArtist_p = New;
New-> nextArtist_p = temp;
if(end == prev)
end = New;
}
}

struct artist *find(struct artist *newNodePointer, char *name)
{
//NEW
while (strcmp(name, newNodePointer->name )!=0)
{
newNodePointer = newNodePointer->nextArtist_p;
if (newNodePointer == NULL)
break;
}
return newNodePointer;
}


void getNextLine(char buffer[], int bufferSize, FILE *fptr)
{
char temp;
int i = 0;

buffer[0] = fgetc(fptr);
while ( (!feof(fptr)) && (buffer[i] != '\n') && i<(bufferSize-1))
{
i = i +1;
buffer[i]=fgetc(fptr);
}

if ((i == (bufferSize-1)) && (buffer[i] != '\n'))
{
temp = fgetc(fptr);
while (temp != '\n')
{
temp = fgetc(fptr);
}
}

buffer[i] = '\0';
}

最佳答案

有很多类型错误会通过 -Wall 溢出(如 @user120115 所建议)。

一些立即跳出来的事情:

  • 不要强制转换 malloc(在 C 中;甚至不要在 C++ 中使用 malloc,除非有充分的理由避免)
  • 任何 while (!feof(stream)) 形式的循环都可能是错误的,因为 feof 不会预测 future 的 EOF,它只会告诉您为什么之前的 EOF 会发生变化。读取尝试失败(例如,为什么 getchar 返回 EOF)。 feof 的要点是区分由于文件末尾导致的“正常”读取失败和由于磁盘驱动器着火或其他原因导致的“异常”(ferror)读取失败.
  • InsertArtist 需要修改列表,因此它要么必须返回新列表,要么获取指向旧列表指针的指针,但它两者都没有。 (但是各种 findOrInsert 函数都可以!)

我也同意 @sarnold 的观点,即限制 typedef 的使用,尽管这确实是一个品味问题。

关于c - 链表: Reading in txt file,节点不保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10116613/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com