从结构输出字符串

时间:2016-03-07 18:42:13

标签: c

我正在尝试创建一个从文件中获取输入的程序,将每个单词放入"单词"结构,然后以每个单词的频率输出结果,但每当我尝试输出字符串时,它只会输出类似?k @ ??我希望字符串在哪里。

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

typedef struct s_words {
        char *str; //stores the word; no pre-determined size
       int count;
        struct s_words* next;
} words;

words* create_words(char* word) {
       //allocate space for the structure
        words* newWord = malloc(strlen(word));
        if (NULL != newWord){
            //allocate space for storing the new word in "str"
            //if str was array of fixed size, storage wud be wasted
            newWord->str = (char *)malloc(strlen(word));
            strcpy(newWord->str,word); //copy “word” into newWord->str
            newWord->str[strlen(word)]='\0';
            newWord->count = 1; //initialize count to 1;
            newWord->next = NULL; //initialize next;            
        }
        return newWord;
}

//If the word is in the list, add 1 to count. 
words* add_word(words* wordList, char* word) {
      int found=0;
        words *temp=wordList;
      // search if word exists in the list; if so, make found=1
      while (temp!=NULL) {
        //  printf("looptest\n");
        if (strcmp(word,temp->str) == 0) { //use strcmp command
            //printf("looptest0\n");
            found=1;
            temp->count = temp->count + 1; //increment count;
            return wordList;
           //printf("looptest1\n");
         }
         else {
            temp = temp -> next;  //update temp
           //  printf("looptest2\n");
        }
       }
     //   printf("looptest3\n");
      //new word
        words* newWord = create_words(word);
      //  printf("looptest4\n");
            if (NULL != newWord) {
              //   printf("looptest5\n");
                newWord->next = wordList;
                wordList = newWord;
                //Insert new word at the head of the list
            }
            else{
                // printf("looptest6\n");
                temp = wordList;
                while(temp->next != NULL){
                    // printf("looptest7\n");
                    temp = temp->next;
                }
                temp->next = newWord;
            }
          return newWord;
}


int main(int argc, char* argv[]) {

    words *mywords;  //head of linked list containing words
        mywords=NULL;

    FILE *myFile;
    myFile = fopen(argv[1],"r");  //first parameter is input file
        if (myFile==0) {
        printf("file not opened\n");
        return 1;
    }
    else {
        printf("file opened\n");
    }

    //start reading file character by character;
    //when word has been detected; call the add_word function
    int ch, word = 0, k=0;
    char thisword[100];

        while ( (ch = fgetc(myFile)) != EOF )
        {
           // printf("%c",ch);
         if (ch==' ' || ch==',' || ch==';' || ch==':' || ch == '.')  //detect new word? Check if ch is a delimiter
         {
            // printf("\ncheck2\n");
            if ( word )  //make sure previous character was not delimiter
            {
               // printf("check\n");
               word = 0;
               thisword[k] = '\0'; //make the kth character of thisword as \0
              //  printf("test2\n");
               //now call add_word to add thisword into the list
               mywords = add_word(mywords,thisword);
              // printf("check3\n");
               k=0;
            }
            // printf("test\n");
         }
         else
         {
            word = 1;
             thisword[k] = ch; //make the kth character of thisword equal to ch
         k++;
         }
        if(ch == EOF){
            thisword[k] = '\0';
            mywords = add_word(mywords,thisword);
        }
      }

    printf("%s\n",mywords->str);
    printf("printing list\n");

    //Traverse list and print each word and its count to outputfile
    //output file is second parameter being passed
    //haven't started to deal with the output file
    words* temp = mywords;
    while(temp != NULL){
        printf("%s\tcount: %i\n",temp->str,temp->count);
        temp = temp->next;
    }
    printf("list complete\n");
    return 0;
}

这是我的所有代码,我无法弄清楚如何对问题进行错误测试,因为我无法弄清楚如何输出字符串。我今年才开始用C语言编程,所以我假设有一些基本的东西我不知道。

1 个答案:

答案 0 :(得分:0)

    newWord->str = (char *)malloc(strlen(word));
    strcpy(newWord->str,word); //copy “word” into newWord->str
    newWord->str[strlen(word)]='\0';

..写出null越界。

假设strlen()返回所需的值,你应该malloc一个额外的char:

    newWord->str = (char *)malloc(1+strlen(word));

注意奥拉夫的评论。在C中投射。另请注意,这不是您唯一的错误。

相关问题