-1

I'm trying to read every line of a file and insert on a linked list, but when passing the str[500] to the function doesn't access the memory address, here is my code

char str[500];
FILE *f1;
f1 = fopen("text.txt", "r");
while (!feof (f1)){
    fscanf (f1, "%s", str);
    insertFirst(str);
}
fclose(f1);

printList();

and here is my Linked list insert code

void insertFirst(char* name) {

struct node *link = (struct node*) malloc(sizeof(struct node));
strcpy(link->nodename,name);

link->next = head;
head = link;
}

my linked list structure

struct node {
char nodename[500];
struct node *next;
};

struct node *head = NULL;
struct node *current = NULL;

When I debug de code, on watches tables, the parameter of insertFirst function, char* name shows this: Error cannot access memory address 0x3847aef1

  • 1
    Note: please see [Why is `while ( !feof (file) )` always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) Also using `fscanf` will only read a whole line if it contains no whitespace, but you clearly are expecting to because of the `500` array length. I suggest using `fgets` (and removing the newline) or `fscanf` with the string set specifier, `" %500[^\n]"`. The space is to clear off any previous newline that was not read. – Weather Vane Jun 09 '18 at 16:57
  • Also, please check that `f1 != NULL` and that `fscanf` returned `1`. – Weather Vane Jun 09 '18 at 17:01
  • I get no such error executing this code. – Scott Hunter Jun 09 '18 at 17:08

1 Answers1

0

Firstly check the return value of fopen() whether its successful or not. open manual page of fopen() . for e.g

f1 = fopen("text.txt", "r");
if(f1 == NULL ) {
        fprintf(stderr,"file doesn't exist \n");
        return 0;
}

Secondly, read why feof() is wrong here http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong Instead of this check the return value of fscanf(). for e.g

while (fscanf (f1, "%s", str) == 1) {
        insertFirst(str);
}

Also typecasting of malloc is not required. here is the sample code

struct node {
        char nodename[500];
        struct node *next;
};
struct node *head = NULL;
struct node *current = NULL; 
void insertFirst(char* name) {
        struct node *link = malloc(sizeof(struct node)); /* create new node by dynamic memory */
        strcpy(link->nodename,name); /* copy the data */
        link->next = head; 
        head = link; /* update the head every time */
}
void printList(void) {
        struct node *temp = head; /* assign head to temp & do operation, don't modify head here since head is declared globally */
        while(temp) {
                printf("name : %s \n",temp->nodename);
                temp = temp->next;
        }
}
int main(void) {
        char str[500];
        FILE *f1 = fopen("text.txt", "r");
        if(f1 == NULL ) {
                fprintf(stderr,"file doesn't exist \n");
                return 0;
        }
        while (fscanf (f1, "%s", str) == 1) { /* it returns no of items readon success */
                insertFirst(str);/* pass the str read from file to insertFirst() function */
        }
        fclose(f1);
        printList();
        return 0;
}
Achal
  • 11,821
  • 2
  • 15
  • 37