Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c结构数组,存储字符串及其出现项并将其写入文件_C_Arrays_Loops_Struct_C Strings - Fatal编程技术网

c结构数组,存储字符串及其出现项并将其写入文件

c结构数组,存储字符串及其出现项并将其写入文件,c,arrays,loops,struct,c-strings,C,Arrays,Loops,Struct,C Strings,因此,我的结构数组有一个小问题,它没有做它应该做的。我在构建程序时没有收到编译器警告或错误 int Array_Size=0;; int Array_Index=0; FILE *Writer; struct WordElement { int Count; char Word[50]; }; struct WordElement *StructPointer; //just a pointer to a structure int Create_Array(int Size

因此,我的结构数组有一个小问题,它没有做它应该做的。我在构建程序时没有收到编译器警告或错误

int Array_Size=0;;
int Array_Index=0;
FILE *Writer;
struct WordElement
{
    int Count;
    char Word[50];
};

struct WordElement *StructPointer; //just a pointer to a structure

int Create_Array(int Size){
StructPointer = (struct WordElement *) malloc(Size * sizeof(StructPointer));
Array_Size = Size;
return 0;
}

int Add_To_Array(char Word[50]){
    int Word_Found=0;
    for(int i=0; i <= Array_Size && Word_Found!=1; i++)
    {
        if(strcmp(StructPointer[i].Word, Word)) // This should only run if the word exists in struct array 
        {
            StructPointer[i].Count++;
            Word_Found=1;

        }

    }
    if(Word_Found==0) // if the above if statement doesnt evualate, this should run 
    {
        strcpy(StructPointer[Array_Index].Word, Word); //copying the word passed by the main function to the struct array at a specific index
        printf("WORD: %s\n", StructPointer[Array_Index].Word); // printing it just to make sure it got copied correctly
        Array_Index++;
    }

    return 0;
}

int Print_All(char File_Name[50])
{
    Writer = fopen(File_Name, "w");
    printf("Printing starts now: \n");
     for(int i=0; i < Array_Size; i++)
    {
        fprintf(Writer, "%s\t\t%d\n",StructPointer[i].Word, StructPointer[i].Count);

    } 
    free(StructPointer);
    return 0;
}
我不知道该怎么理解这个,因为我对C编程真的很陌生。。。可能值得一提的是if(Word_Foun==0)没有执行

StructPointer =  malloc(Size * sizeof(*StructPointer));
这将是正确的分配。否则,代码中将出现错误行为。还要检查
malloc
的返回值

StructPointer =  malloc(Size * sizeof(*StructPointer));
if(NULL == StructPointer){
   perror("malloc failure");
   exit(EXIT_FAILURE);
}
您正在为
struct WordElement
分配,而不是为指向它的指针分配。您已经有一个指向
struct WordElement
的指针,您所需要的只是
struct WordElement
的内存

此外,在循环中,您访问的数组索引是越界的

for(int i=0; i <= Array_Size && Word_Found!=1; i++)
               ^^^
同样
Writer=fopen(文件名,“w”)
您应该检查
fopen
的返回值

  if(Writer == NULL){
     fprintf(stderr,"Error in file opening");
     exit(EXIT_FAILURE);
  }
另外,当您增加
数组_索引时,请检查它是否会越界访问数组索引

为了完成一个小任务而使用的全局变量越多,跟踪bug就越困难。这总是有问题的,因为数据可能发生变化的地方是分散的,这使得管理起来很困难

这将是正确的分配。否则,代码中将出现错误行为。还要检查
malloc
的返回值

StructPointer =  malloc(Size * sizeof(*StructPointer));
if(NULL == StructPointer){
   perror("malloc failure");
   exit(EXIT_FAILURE);
}
您正在为
struct WordElement
分配,而不是为指向它的指针分配。您已经有一个指向
struct WordElement
的指针,您所需要的只是
struct WordElement
的内存

此外,在循环中,您访问的数组索引是越界的

for(int i=0; i <= Array_Size && Word_Found!=1; i++)
               ^^^
同样
Writer=fopen(文件名,“w”)
您应该检查
fopen
的返回值

  if(Writer == NULL){
     fprintf(stderr,"Error in file opening");
     exit(EXIT_FAILURE);
  }
另外,当您增加
数组_索引时,请检查它是否会越界访问数组索引


为了完成一个小任务而使用的全局变量越多,跟踪bug就越困难。这总是有问题的,因为数据可能发生变化的地方是分散的,这使得管理起来很困难。

不需要全局变量时,不要使用全局变量。修改函数,以便可以传递数组的长度,而不是使用全局变量作为长度。不需要全局变量时,不要使用它们。修改您的函数,以便可以传递数组的长度,而不是使用全局变量作为长度。感谢您的分析,编辑了您所说的所有内容,但我的代码仍然不执行if(Word_find==0)语句,并且输出仍然相同。我做错了什么?I@Hunt3R.:这意味着还有一些东西无法从您显示的代码中确定。如果您知道如何使用调试器,请使用一个建议—首先编写小部分,不要使用那么多全局变量。我很好奇你是否已经添加了所有提到的修改。如果您这样做了,它将至少显示错误。我再次检查了代码,在分配内存时,它为所有单词(包括重复的单词)分配了足够的内存,因此我创建了另一个变量,该变量仅在添加新词时递增。我在打印循环中使用了上述变量,现在它可以很好地打印单词(出于某种原因,添加了一个额外的单词,混合了两个单词),但计数仍然无法正确打印。另外,当我第二次(或更多次)运行该程序时,我得到错误“堆已损坏”,我是否没有正确释放内存?@Hunt3R.:这是分配和释放内存的问题。你会用valgrind吗?它将帮助您发现问题。如果你做不到,那就用修改过的代码问一个问题,以及你所遇到的错误。同时解释您在阅读错误消息时试图解决的问题。有人或我会回答这个问题。将malloc更改为:StructPointer=malloc((Size*sizeof(StructPointer)+1));当我试图在不初始化计数器的情况下增加计数器时,INT没有正确打印。修复了添加新词时将计数设置为1的问题。感谢您的分析,我编辑了您所说的所有内容,但我的代码仍然没有执行if(word_Found==0)语句,并且输出仍然相同。我做错了什么?I@Hunt3R.:这意味着还有一些东西无法从您显示的代码中确定。如果您知道如何使用调试器,请使用一个建议—首先编写小部分,不要使用那么多全局变量。我很好奇你是否已经添加了所有提到的修改。如果您这样做了,它将至少显示错误。我再次检查了代码,在分配内存时,它为所有单词(包括重复的单词)分配了足够的内存,因此我创建了另一个变量,该变量仅在添加新词时递增。我在打印循环中使用了上述变量,现在它可以很好地打印单词(出于某种原因,添加了一个额外的单词,混合了两个单词),但计数仍然无法正确打印。另外,当我第二次(或更多次)运行该程序时,我得到错误“堆已损坏”,我是否没有正确释放内存?@Hunt3R.:这是分配和释放内存的问题。你会用valgrind吗?它将帮助您发现问题。如果你做不到,那就用修改过的代码问一个问题,以及你所遇到的错误。同时解释您在阅读错误消息时试图解决的问题。有人或我会回答这个问题。将malloc更改为:StructPointer=malloc((Size*sizeof(StructPointer)+1));当我试图在不初始化计数器的情况下增加计数器时,INT没有正确打印。修正了添加新词时将计数设置为1的问题。