gpt4 book ai didi

c - 从结构中输出字符串

转载 作者:行者123 更新时间:2023-11-30 17:04:21 25 4
gpt4 key购买 nike

我正在尝试创建一个程序,该程序从文件中获取输入,将每个单词放入“单词”结构中,然后输出每个单词频率的结果,但每当我尝试输出字符串时,它只会打印像?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 语言编程,所以我认为我缺少一些基本的东西。

最佳答案

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

.. 写入 null 越界。

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

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

注意奥拉夫评论。用 C 进行转换。另请注意,这不太可能是您唯一的错误。

关于c - 从结构中输出字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35851429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com