-2

I was trying to create a single linked list that contains some words from a .txt file on my desktop, but when I run it in terminal, I got a segmentation fault. Later I compiled the code on Xcode and run it, I got this error message: Thread 1:EXC_BAD_ACCESS(code=1, address=0x7fff5fc00000) I'm a beginner, and I really need some help now. Thanks!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct word{
    int  num;
    char word[50];
    struct word *next;
} Word;
#define len sizeof(Word)

Word *create();
void print(Word *head);

int main()
{
    Word *head;
    head = create();
    print(head);
    return 0;
}

Word *create()
{
    Word *head, *p1, *p2;
    char word[50], c;
    int i = 0;
    FILE *fp = fopen("/Users/apple/Desktop/words", "r");

    head = p1 = p2 = NULL;

    while(fp != NULL){
        i = 0;
        p1 = (Word *)malloc(len);

    //get the English word
    while((c = fgetc(fp)) != '\n'){
        word[i++] = c;
    }
    word[i] = '\0';
    strcpy(p1->word, word);
    p1->next = NULL;

        if(head == NULL){
            head = p1;
            p2 = p1;
        } else {
            p2->next = p1;
            p2 = p1;
        }
        p1 = p1->next;
    }

    return head;
}

void print(Word *head)
{
    Word *p = head;
    while(p != NULL){
        printf("%s\n", p->word);
        p = p->next;
    }
}

And this is the content of the .txt file:

enter image description here

MD XF
  • 7,860
  • 7
  • 40
  • 71
Yan Chen
  • 11
  • 2
  • 1
    Welcome to SO. I would highly recommend spending some time learning how to use the debugger. That is the first step in figuring out your problem. – OldProgrammer Feb 18 '17 at 18:34
  • 3
    Please read [How to debug small program (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). SO is not a debugging service. Once you pinpoint the problem, if you don't understnad *why* it's a problem, then by all means ask a question. – StoryTeller - Unslander Monica Feb 18 '17 at 18:35
  • 1
    Start by using a debugger to catch the crash in action, so you locate *where* in your code it happens. Then when you know, and while still in the debugger, examine the values of all involved variables, to make sure they look okay. At the very least please give *us* that information (location and values). – Some programmer dude Feb 18 '17 at 18:36
  • You put an image in a .txt ile? – n. m. could be an AI Feb 18 '17 at 18:56
  • Post the text file like you posted the code: with four spaces before each word. Also, do you have an empty line at the end? – giusti Feb 18 '17 at 19:00

1 Answers1

1

Before your question gets deleted, here are some incorrect things that you are doing in your code and you should take a look at it even before you try to fix your segfault.

while(fp != NULL){
    i = 0;
    p1 = (Word *)malloc(len);

That's not how you read from a file. The file pointer fp is not expected change. If you want to know whether your file has finished, you have to check the output of the read functions. fscanf() and fgetc() return EOF when you try to read past the last character of the file.

Also, do not use feof(). It's almost always wrong.

while((c = fgetc(fp)) != '\n'){
    word[i++] = c;
}

fgetc() returns an (int), so you should declare int c. That's how you check if you have read everything. After you read the last word, fgetc() will return EOF. It is usually -1, but, regardless of its value, it doesn't fit into a (char). You need (int).

Also, you should be using fscanf() instead of reading it char-by-char (unless it is your assignment to do so). fscanf() reads until the end of the word and adds a 0 automatically.

This

p1 = (Word *)malloc(len);

will not help you. That len is not doing you any favors. Neither is (Word*). This is how you should do:

p1 = malloc(sizeof (Word));

or even better

p1 = malloc(sizeof *p1);

The last one doesn't even require you to know the type of p1.

Community
  • 1
  • 1
giusti
  • 3,156
  • 3
  • 29
  • 44