C语言中从文本文件到链表的字数计算

C语言中从文本文件到链表的字数计算,c,file-io,struct,linked-list,C,File Io,Struct,Linked List,我是C语言编程新手,在我的一个课程中,我们将编写代码,读取文本文件,将所有单词放入带有计数器的链表中,并将链表输出到文本文件中 我不确定我在下面的代码中做错了什么,它只计算了一些单词,似乎跳过了其他单词。(我添加了许多额外的打印,以尝试查看文字丢失的位置) 然后,当我把所有的单词添加到链表中时,我尝试将它们改成小写,但这似乎使它读的单词更少,我不知道为什么 如有任何帮助/建议,将不胜感激 #include <stdio.h> #include <stdlib.h> #in

我是C语言编程新手,在我的一个课程中,我们将编写代码,读取文本文件,将所有单词放入带有计数器的链表中,并将链表输出到文本文件中

我不确定我在下面的代码中做错了什么,它只计算了一些单词,似乎跳过了其他单词。(我添加了许多额外的打印,以尝试查看文字丢失的位置)

然后,当我把所有的单词添加到链表中时,我尝试将它们改成小写,但这似乎使它读的单词更少,我不知道为什么

如有任何帮助/建议,将不胜感激

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

typedef struct s_words {
    char *str; //word
    int count; //number of times word occurs
    struct s_words* next; //pointer to next word
} words;


words* create_words(char* word) {
    //+allocate space for the structure
    printf("%lu ", strlen(word));
    words* newWord = malloc(sizeof(words));
    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))+1);
        strcpy(newWord->str, word); //+copy “word” into newWord->str
        newWord->str[strlen(word)]='\0';
        printf(" Create: %s ", newWord->str);
        //+initialize count to 1;
        newWord->count = 1;
        //+initialize next;
        newWord->next = NULL;                   
    }
    return newWord;
}

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) 
    {

        if (strcmp(temp->str, word) == 0) 
        {  //+use strcmp command
            found=1;
            temp->count = temp->count+1; //+increment count;
            return wordList;
        }
        else 
        {
            //+update temp
            temp = temp->next;
        }
    }
    if (found==0) 
    {  //new word
        //printf("%s ", word);
        words* newWord = create_words(word);
        if (NULL != newWord) 
        {
            //+??  Insert new word at the head of the list
            newWord->next = wordList;
            printf(" NEW WORD: %s\n ", newWord->str);
        }
        return newWord;
    }
    //return wordList; //code never gets here, just added in case of error    
}




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

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

    FILE *myFile;
    FILE *myOutput;

    char* filename = argv[1];
    char* outputfile = argv[2];

    myFile = fopen(filename, "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 = 0, word = 1, k = 0;
    char thisword[100];
    //ch = putchar(tolower(ch));
    //ch = fgetc(myFile);
    while ((ch = fgetc(myFile)) != EOF )
    {
        //error handling

        if (ch == '.' || ch == ' ' || ch == ',' || ch == ':' || ch == ';' || ch == '\n')  //+detect new word? Check if ch is a delimiter
        { //when above if is true, new word created in next if:
            if ( word == 1 )  //+make sure previous character was not delimiter
            {
                word = 0;
                //+make the kth character of thisword as \0
                thisword[k] = '\0';

                //+now call add_word to add thisword into the list
                printf(" Add:%s ", thisword);
                mywords = add_word(mywords, thisword);
                printf(" Added:%s\n", mywords->str);

                k=0;
            }
        }
        else
        {
            word = 1;
            //make ch lowercase
            //ch = putchar(toupper(ch));
            //+?? //make the kth character of thisword equal to ch
            thisword[k] = ch;
            thisword[k] = putchar(tolower(thisword[k]));
            k++;
        }
    }
    if (word == 1) 
    {
        thisword[k] = '\0';
        //add thisword into the list
        printf("Last Word:%s ", thisword);
        mywords = add_word(mywords, thisword);      
    }

    words *currword;
    printf("printing list\n");

    //+Traverse list and print each word and its count to outputfile
    //+output file is second parameter being passed

    myOutput = fopen(outputfile, "w+");  //+first parameter is input file
    if (myOutput == 0) 
    {
        printf("output file not opened \n");
        return 1;
    }
    else 
    {
        printf("output file opened \n");
    }

    currword = mywords;

    while (currword->next != NULL)
    {
        //add word name then word count to file, then move to next
        fprintf(myOutput, "%s %d \n", currword->str, currword->count);
        printf("%s ", currword->str);
        currword = currword->next;
    }

    return 0;

}
#包括
#包括
#包括
#包括
typedef结构s_字{
char*str;//word
int count;//单词出现的次数
struct s_words*next;//指向下一个单词的指针
}文字;
单词*创建单词(字符*单词){
//+为结构分配空间
printf(“%lu”,strlen(word));
words*newWord=malloc(sizeof(words));
if(NULL!=新词)
{
//+为“str”中的新词分配存储空间
//+如果str是固定大小的数组,则可能会浪费存储空间
newWord->str=(char*)malloc((strlen(word))+1;
strcpy(newWord->str,word);//+将“word”复制到newWord->str
newWord->str[strlen(word)]='\0';
printf(“创建:%s”,newWord->str);
//+将计数初始化为1;
newWord->count=1;
//+下一步初始化;
newWord->next=NULL;
}
返回新词;
}
单词*添加单词(单词*单词列表,字符*单词)
{
int=0;
单词*temp=单词列表;
//+搜索列表中是否存在单词;如果存在,则使find=1
while(temp!=NULL)
{
如果(strcmp(temp->str,word)==0)
{//+使用strcmp命令
发现=1;
临时->计数=临时->计数+1;//+增量计数;
返回词表;
}
其他的
{
//+更新温度
温度=温度->下一步;
}
}
如果(找到==0)
{//新词
//printf(“%s”,单词);
words*newWord=创建单词(word);
if(NULL!=新词)
{
//+?在列表的开头插入新词
newWord->next=wordList;
printf(“新词:%s\n”,新词->str);
}
返回新词;
}
//return wordList;//这里没有代码,只是在出错时添加
}
int main(int argc,char*argv[])
{
words*mywords;//+包含单词的链接列表的标题
mywords=NULL;
文件*myFile;
文件*myOutput;
char*filename=argv[1];
char*outputfile=argv[2];
myFile=fopen(文件名,“r”);//+第一个参数是输入文件
如果(myFile==0)
{
printf(“文件未打开\n”);
返回1;
}
其他的
{
printf(“文件已打开”);
}
//+开始逐字读取文件;
//+当检测到单词时,调用add_word函数
int ch=0,word=1,k=0;
字符这个词[100];
//ch=putchar(tolower(ch));
//ch=fgetc(myFile);
而((ch=fgetc(myFile))!=EOF)
{
//错误处理
如果(ch='.||ch='.|ch='.,'.|ch='.:'.| ch='.'.\n')/+检测新词?检查ch是否是分隔符
{//如果上述if为真,则在下一个if中创建新词:
如果(word==1)/+请确保前一个字符不是分隔符
{
字=0;
//+将此单词的第k个字符设置为\0
这个词[k]='\0';
//+现在调用add_word将该单词添加到列表中
printf(“添加:%s”,thisword);
mywords=添加单词(mywords,ThisWords);
printf(“添加了:%s\n”,mywords->str);
k=0;
}
}
其他的
{
单词=1;
//使ch小写
//ch=putchar(toupper(ch));
//+??//使该单词的第k个字符等于ch
这个词[k]=ch;
这个词[k]=putchar(tolower(这个词[k]);
k++;
}
}
if(word==1)
{
这个词[k]='\0';
//将这个词添加到列表中
printf(“最后一个字:%s”,这个字);
mywords=添加单词(mywords,ThisWords);
}
单词*currword;
printf(“打印列表”);
//+遍历列表并将每个单词及其计数打印到outputfile
//+输出文件是正在传递的第二个参数
myOutput=fopen(outputfile,“w+”);//+第一个参数是输入文件
如果(myOutput==0)
{
printf(“输出文件未打开\n”);
返回1;
}
其他的
{
printf(“输出文件已打开\n”);
}
currword=mywords;
while(currword->next!=NULL)
{
//将单词名、单词数添加到文件中,然后移动到下一个
fprintf(myOutput,“%s%d\n”,currword->str,currword->count);
printf(“%s”,currword->str);
currword=currword->next;
}
返回0;
}

你肯定喜欢让事情对自己不利。。。您最大的四个问题是(1)未将
mywords
的地址传递给
add\u words
(2)未能在
add\u words
中处理新的/空的列表案例,(3)将新节点添加到列表的开头,以及(4)每次调用
add\u word
时覆盖列表地址(例如
mywords=add\u words…

解决这些问题并稍微整理一下语法分析,您应该能够在列表中找到所有单词。检查/测试以下各项:

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

typedef struct s_words {
    char *str;                  //word
    int count;                  //number of times word occurs
    struct s_words *next;       //pointer to next word
} words;

words *create_words (char *word)
{
    //+allocate space for the structure
    printf ("%lu ", strlen (word));
    words *newWord = malloc (sizeof (words));
    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)) + 1);
        strcpy (newWord->str, word);    //+copy “word” into newWord->str
        newWord->str[strlen (word)] = '\0';
        printf (" Create: %s ", newWord->str);
        //+initialize count to 1;
        newWord->count = 1;
        //+initialize next;
        newWord->next = NULL;
    }
    return newWord;
}

words *add_word (words **wordList, char *word)
{
    if (!*wordList) {       /* handle EMPTY list */
        printf ("NEW LIST\n");
        return *wordList = create_words (word);
    }

    words *temp = *wordList;
    //+ search if word exists in the list; if so, make found=1
    while (temp->next != NULL) {    /* iterate while temp->next != NULL */

        if (strcmp (temp->str, word) == 0) {    //+use strcmp command
            temp->count = temp->count + 1;      //+increment count;
            return *wordList;
        }
        else
            temp = temp->next;  //+update temp
    }
    words *newWord = create_words (word);
    if (NULL != newWord) {  /* insert at TAIL of list */
        temp->next = newWord; 
        printf (" NEW WORD: %s\n ", newWord->str);
    }
    return newWord;
}

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

    words *mywords;             //+head of linked list containing words
    mywords = NULL;
    char *delim = ". ,:;\t\n";

    FILE *myFile;
    FILE *myOutput;

    char *filename = argv[1];
    char *outputfile = argv[2];

    if (argc != 3) {
        fprintf (stderr, "error: insufficient input. usage: %s ifile ofile\n",
                argv[0]);
        return 1;
    }

    myFile = fopen (filename, "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 = 0, word = 1, k = 0;
    char thisword[100];
    while ((ch = fgetc (myFile)) != EOF) {  /* for each char    */
        if (strchr (delim, ch)) {           /* check if delim   */
            if (word == 1) {    /* if so, terminate word, reset */
                word = 0;
                thisword[k] = '\0';

                printf ("\nadd_word (mywords, %s)\n", thisword);
                /* do NOT overwrite list address each time,
                 * you must send ADDRESS of list to add_word
                 * to handle EMPTY list case.
                 */
                if (add_word (&mywords, thisword))
                    printf (" added: %s\n", mywords->str);
                else
                    fprintf (stderr, "error: add_word failed.\n");

                k = 0;
            }
        }
        else {  /* if not delim, add char to string, set word 1 */
            word = 1;
            thisword[k++] = tolower (ch);   /* make ch lowercase */
        }
    }
    if (word == 1) {    /* handle non-POSIX line-end */
        thisword[k] = '\0';
        //add thisword into the list
        printf ("\nadd_word (mywords, %s) (last)\n", thisword);
        if (add_word (&mywords, thisword))  /* same comment as above */
            printf (" added: %s\n", mywords->str);
        else
            fprintf (stderr, "error: add_word failed.\n");
    }

    words *currword;
    printf ("printing list\n");

    //+Traverse list and print each word and its count to outputfile
    //+output file is second parameter being passed

    myOutput = fopen (outputfile, "w+");        //+first parameter is input file
    if (myOutput == 0) {
        printf ("output file not opened \n");
        return 1;
    } else {
        printf ("output file opened \n");
    }

    currword = mywords;

    while (currword != NULL) {  /* just test currword here */
        //add word name then word count to file, then move to next
        fprintf (myOutput, "%s %d \n", currword->str, currword->count);
        printf ("%s ", currword->str);
        currword = currword->next;
    }

    putchar ('\n');
    return 0;
}
测试使用

$ cat ../dat/captnjack.txt
This is a tale
Of Captain Jack Sparrow
A Pirate So Brave
On the Seven Seas.
$ ./bin/llwordcount ../dat/captnjack.txt dat/llout.txt
$ cat dat/llout.txt
this 1
is 1
a 2
tale 1
of 1
captain 1
jack 1
sparrow 1
pirate 1
so 1
brave 1
on 1
the 1
seven 1
seas 1
输出文件

$ cat ../dat/captnjack.txt
This is a tale
Of Captain Jack Sparrow
A Pirate So Brave
On the Seven Seas.
$ ./bin/llwordcount ../dat/captnjack.txt dat/llout.txt
$ cat dat/llout.txt
this 1
is 1
a 2
tale 1
of 1
captain 1
jack 1
sparrow 1
pirate 1
so 1
brave 1
on 1
the 1
seven 1
seas 1
注意:对于打印/输出,只需
while(currword!=NULL)
遍历列表即可

现在,你应该真正考虑使用面向线的I