首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将文本文件读入LinkedList

将文本文件读入LinkedList
EN

Stack Overflow用户
提问于 2013-10-15 05:24:09
回答 3查看 3.1K关注 0票数 0

我试图读入一个文本文件,并逐个单词地将其中的字符串添加到链表中。我是C语言的新手,不太懂指针。我在处理它时遇到了几个不同的错误,但现在我在插入方法中遇到了一个分段错误。这实际上是相当令人沮丧的。有人能解释一下我在这里做错了什么吗?

代码语言:javascript
复制
#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;
    }
EN

回答 3

Stack Overflow用户

发布于 2013-10-15 05:26:55

首先,检查fopen()等库函数的返回值。

其次,请参阅simonc的答案。

第三,在这个循环之后:

代码语言:javascript
复制
  while(currentPtr != NULL){
    currentPtr = currentPtr->nextPtr;
  }
  currentPtr->nextPtr = newPtr;

currentPtr为null,因此currentPtr->nextPtr = newPtr;将取消引用null指针。也许是像这样的

代码语言:javascript
复制
  while(currentPtr && currentPtr->nextPtr) {
    currentPtr = currentPtr->nextPtr;
  }
  currentPtr->nextPtr = newPtr;

更多的是你正在寻找的东西。

最后,

代码语言:javascript
复制
char fpeek(FILE *stream) {
    char c;
    c = fgetc(stream);
    ungetc(c, stream);
    return c;
}

应该是

代码语言:javascript
复制
int fpeek(FILE *stream) {
    int c;
    c = fgetc(stream);
    ungetc(c, stream);
    return c;
}

在main中

代码语言:javascript
复制
char fpeek(FILE *);

应该是

代码语言:javascript
复制
int fpeek(FILE *);
票数 3
EN

Stack Overflow用户

发布于 2013-10-15 05:28:57

我快速地看了一下你的代码,我非常确定segfault问题就在这里:

代码语言:javascript
复制
while(currentPtr != NULL){
   currentPtr = currentPtr->nextPtr;
}
currentPtr->nextPtr = newPtr;

这样做的目的是遍历列表,直到currentPtr等于null。然后,您尝试通过空指针(currentPtr->nextPtr)为结构字段赋值,这会导致分段错误。

票数 0
EN

Stack Overflow用户

发布于 2013-10-16 01:21:18

好的,就在这里:

代码语言:javascript
复制
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';
    }

当我运行完整的代码时,它会打印12345ooooooooooooooooooooooo...etc。在我的测试文件中,第一个单词是"Hello“,所以这就是‘0’的来源。如果外部循环是无限的,那么第二个while循环不是也会执行不止一次吗?我的意思是,为什么第二个print语句是唯一重复执行它自己的语句?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19369551

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档