[英]Reading a text file into a LinkedList
我正在嘗試讀取一個文本文件,並將其中的字符串逐字添加到鏈接列表中。 我是C語言的新手,不太了解指針。 我在處理它時遇到了一些不同的錯誤,但是現在我的insert方法遇到了分段錯誤。 實際上,這很令人沮喪。 有人可以在這里解釋我做錯了嗎?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct listNode { /* self-referential structure */
char data[50];
struct listNode *nextPtr;
};
typedef struct listNode LISTNODE;
typedef LISTNODE *LISTNODEPTR;
void insert(LISTNODEPTR *, char[]);
void printList(LISTNODEPTR);
char fpeek(FILE *);
main() {
FILE *fptr;
char file_name[20];
int nrchar = 0;
LISTNODEPTR startPtr = (struct listNode *) malloc(sizeof(struct listNode));
char word[50];
char c;
int i;
printf("What is the name of the file in which the text is stored?\n");
scanf("%s",file_name);
// printf("Type the number of characters per line");
//scanf("%d", &nrchar);
fptr = fopen(file_name,"r");
while(fpeek(fptr) != EOF) {
i = 0;
while(fpeek(fptr) != ' '){
word[i] = fgetc(fptr);
i++;
printf("%d", i);
}
word[strlen(word)] = '\0';
insert(&startPtr, word);
word[0] = '\0';
}
fclose(fptr);
printList(startPtr);
return 0;
}
/* Insert a new value into the list in sorted order */
void insert(LISTNODEPTR *sPtr, char value[])
{
LISTNODEPTR newPtr, currentPtr;
newPtr = malloc(sizeof(LISTNODE));
strcpy(newPtr->data, value);
newPtr->nextPtr = NULL;
currentPtr = *sPtr;
while(currentPtr != NULL){
currentPtr = currentPtr->nextPtr;
}
currentPtr->nextPtr = newPtr;
}
/* Return 1 if the list is empty, 0 otherwise */
int isEmpty(LISTNODEPTR sPtr)
{
return sPtr == NULL;
}
/* Print the list */
void printList(LISTNODEPTR currentPtr)
{
if (currentPtr == NULL)
printf("List is empty.\n\n");
else {
printf("The list is:\n");
while (currentPtr != NULL) {
printf("%s --> ", currentPtr->data);
currentPtr = currentPtr->nextPtr;
}
printf("EOF\n\n");
}
}
char fpeek(FILE *stream) {
char c;
c = fgetc(stream);
ungetc(c, stream);
return c;
}
首先,檢查您從fopen()等庫函數返回的值。
其次,請參見simonc的答案。
第三,在此循環之后:
while(currentPtr != NULL){
currentPtr = currentPtr->nextPtr;
}
currentPtr->nextPtr = newPtr;
currentPtr為null,因此currentPtr->nextPtr = newPtr;
將取消引用空指針。 也許像
while(currentPtr && currentPtr->nextPtr) {
currentPtr = currentPtr->nextPtr;
}
currentPtr->nextPtr = newPtr;
就是您想要的。
最后,
char fpeek(FILE *stream) {
char c;
c = fgetc(stream);
ungetc(c, stream);
return c;
}
應該
int fpeek(FILE *stream) {
int c;
c = fgetc(stream);
ungetc(c, stream);
return c;
}
和主要
char fpeek(FILE *);
應該
int fpeek(FILE *);
我快速瀏覽了一下您的代碼,我很確定此段錯誤在這里:
while(currentPtr != NULL){
currentPtr = currentPtr->nextPtr;
}
currentPtr->nextPtr = newPtr;
這是在列表中循環直到currentPtr
等於null。 然后,您嘗試通過空指針( currentPtr->nextPtr
)分配struct字段,這會導致分段錯誤。
好的,這就是這里:
while(fpeek(fptr) != EOF) {
i = 0;
while(fpeek(fptr) != ' '){
word[i] = fgetc(fptr);
i++;
printf("%d",i);
}
word[i] = '\0';
insert(&startPtr, word);
printf("%c", word[4]);
word[0] = '\0';
}
當我運行完整代碼時,它會顯示12345ooooooooooooooooooooooooooooo ...等。 在我的測試文件中,第一個單詞是“ Hello”,所以這就是無限個“ o”的來源。 如果外部循環是無限循環,那么第二個while循環是否還會執行多次? 我的意思是,為什么第二個打印語句是唯一重復的語句?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.