简体   繁体   中英

How do you read a file of strings into a linked list? (C)

My struct is defined as follows:

typedef struct node {
  char * word;
  int wordLength;
  int level;
  struct node * parent;
  struct node * next;
}Node;

I'm attempting to create a linked list of of the struct above, where "word" is a string, which is read from a file. Below is the function that I'm using to create the list. The seems to work fine, and prints the words out, but then, when I try to print it out in main() , it doesn't print anything.

void GetWords(char * dictionary, Node * Word, Node * Start)
{
  FILE *fp;
  char * currentWord = (char *)malloc(sizeof(char));
  fp = fopen(dictionary, "r");
  ErrorCheckFile(fp);
  if(fscanf(fp, "%s", currentWord) == 1){
    Start = Word = AllocateWords(currentWord);
  }

  while((fscanf(fp, "%s", currentWord)) != EOF){
    Word->next = AllocateWords(currentWord);
    Word = Word->next;
    printf("%s: %d\n", Word->word, Word->wordLength);
  }



  fclose(fp);
}

Do I need to return to the start of the list? If so, how would I do this? In this function, I have "Start" pointing to the first word in the file, do I need this? I'm attempting to print them, just to be certain that the file is being stored correctly in the list.

My main() function is:

int main(int argc, char ** argv)
{
  Node * Word = (Node *)malloc(sizeof(Node));
  Node * Start = (Node *)malloc(sizeof(Node));
  GetWords(argv[1], Word, &Start);


  printf("Start: %s\n", Start->word);
  printf("Word: %s\n", Word->word);
  while(Word->next != NULL){
    printf("%s\n", Word->word);
  }

  return 0;
}

The print statements are just in there to check the list is printing. As it stands, Start->word is printing out the last word in the file and Word->word is printing (null) and the while loop is not executing at all.

My AllocateWords() function is as follows:

Node * AllocateWords(char * string)
{
  Node * p;
  p = (Node *)malloc(sizeof(Node));
  if(p == NULL){
    fprintf(stderr, "ERROR: Cannot allocate space...\n\n");
    exit(1);
  }
  p->word = string;
  p->wordLength = strlen(p->word);
  p->parent = NULL;
  p->next = NULL;
  return p;
}

Use call-by-reference instead of call-by-value for the Start pointer.

main() function:

Node * Start

GetWords(..., &Start)

and

 void GetWords(char * dictionary, Node * Word, Node ** Start)
 {

  ....
  *Start = Word = AllocateWords(currentWord);
  ....

You also need to fix the size of currentWord . If the maximum word length is 255 chars, use:

char * currentWord = (char *)malloc(256*sizeof(char));
...
if(fscanf(fp, "%255s", currentWord) == 1){
...
while((fscanf(fp, "%255s", currentWord)) != EOF){

You need also to fix your main() function. You do not need to allocate the current word pointer Word or the Start node pointer.

...
Node * Word = (Node *) NULL;
Node * Start = (Node *) NULL;
GetWords(argv[1], Word, &Start);

printf("Start: %s\n", Start->word);
Word = Start;
while(Word->next){           // identical to while(World->next!=NULL){      
  Word=Word->next;
  printf("%s\n", Word->word);
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM