Frage

The code I am trying to implement is a method that reads a .txt file and converts the strings into nodes. Essentially, as I am reading the .txt file, I first check for non-letters (the word cannot start with a number, nor can the word have a non-alphanumerical in any index of the word). Once it finds the first letter, then the program exits the loop and enters another one that loops until it sees a space. When I successfully make a word (a word "ends" when there is a space found), I input the word into a linked list.

When I run this, I get a Bus Error: 10. I thought this would be due to the word[b] array, but when I malloc it, I still get the same error.

Thank you in advance!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define TRUE 1
#define FALSE 0


struct Node{
   char value[100];
   int numOccur;
   int numVariance;
   struct Node *next;
};

void testprint(struct Node * head){
    int data;
    data = head->value;
    strcpy(data,head->value);
    while(head != NULL){

        printf("%s\n", data);
        head = head->next;
    }

}

int main()
{
   struct Node *curr;
   struct Node *n;
   struct Node *head =0;
   struct Node *tail =0;
   struct Node *next;
   char word[100];
   int a;
   int x;



   FILE *file1;
   file1 = fopen("test1.txt", "r");           //opens text file

   if(file1 == NULL){
       fprintf(stderr,"Error: Could not open file");     //if file1 has error, returns    error message
   exit(1);
   }
   a = fgetc(file1);
   int b = 0;
   while(a != EOF){
       while(!isalpha(a)){
           a = fgetc(file1);
           continue;
       }

       n = (struct Node *) malloc( sizeof( struct Node));
       while(isalnum(a)){
           while( a != ' ' && a != EOF){
               word[b] = a;
               a = fgetc(file1);
               b++;
           }
           word[b] = '\0';
       }
       n->next = 0;
       if(head == 0){
           head = n;
           tail = n;
       }
       else{
           tail->next = n;
           tail = n;
       }
   }
    testprint(head);
    fclose(file1);
}
War es hilfreich?

Lösung

You should pay attention to the warnings from the compiler. (And it's a good idea always to compile with -Wall -Wextra to get more warnings.)

As Barney Hsiao has pointed out, testprint() is called with a FILE pointer instead of a Node pointer.


Unrelated, but if you need true and false constants, there is a standard header file that will give you these (in lowercase, ie. true, false).

#include <stdbool.h>

I get the following compiler warnings when compiling this code:

$ CFLAGS="-Wall -Wextra" make bus10
cc -Wall -Wextra    bus10.c   -o bus10
bus10.c: In function ‘testprint’:
bus10.c:20:14: warning: assignment from incompatible pointer type
bus10.c: In function ‘main’:
bus10.c:48:8: warning: array subscript has type ‘char’
bus10.c:54:8: warning: array subscript has type ‘char’
bus10.c:68:23: warning: assignment from incompatible pointer type
bus10.c:34:8: warning: unused variable ‘x’
bus10.c:31:17: warning: unused variable ‘next’
bus10.c:27:17: warning: unused variable ‘curr’
bus10.c:74:1: warning: control reaches end of non-void function

Now some of these are more serious than others. "array subscript has type 'char'" doesn't sound like a problem to me (to be honest, I don't know what it even means). "unused variable" means just that: you've created (declared) a variable and then you never use it. "control reaches end of non-void function" means you have a function that is declared to return a value but the code falls off the bottom without a return statement.

But "assignment from incompatible pointer type" is bad. The word incompatible is bad. Line 20 reads:

     head = head->next;

Line 68 reads:

        tail->next = n;

It's because

struct Node{
   ...
   struct node *next;
};

See? *next is not a struct Node but a struct node. Uppercase/Lowercase counts.


Ok. Here's a big one. And the compiler didn't say nothing. Well, to be fair, it did. But we didn't know what it meant. Line 33:

    char a;

What's wrong with that you say? It's a type that holds a character, right? The problem is here:

    a = fgetc(file1);

Do you see it yet? How about here?

    while(a != EOF){

EOF is a sentinel value that is too big to fit in a char. It's supposed to be like this. In order for the fgetc function to tell you that an End Of File condition occurred and not just another byte from the file it has to return a value outside of the range of a char (0..255 unsigned, -128..127 signed). The while condition will never fail because no char value can compare equal to EOF.

So declare a to be int, not char.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top