简体   繁体   English

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

[英]c Struct Array, Storing string and its occurrence and writing it to a file

so I'm having a little problem with my struct array not doing what its supposed to. 所以我的struct数组有一个小问题,它没有做应该做的事情。 I get no compiler warnings or errors when building the program. 生成程序时没有收到编译器警告或错误。

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;
}

These functions get called from a different file, The Add_To_Array is called when the program reads a new word form the text file. 这些函数从另一个文件中调用,当程序从文本文件中读取一个新单词时,将调用Add_To_Array。 That function is supposed to check if the word already exists in the struct array and if it does, it should just increment the counter. 该函数应该检查该单词是否已存在于struct数组中,如果存在,则应仅增加计数器。 If it doesn't, then it adds it. 如果没有,则将其添加。

The Print_All function is called after all the words have been stored in the struct array. 将所有单词存储在struct数组中后,将调用Print_All函数。 Its supposed to loop through them and print each word and their occurrence. 它应该遍历它们并打印每个单词及其出现。 In the text file, there are 2 of every words but my program outputs: 在文本文件中,除了我的程序输出外,每个单词中都有2个:

    this        13762753
document        -1772785369
contains        1129268256
two     6619253
of      5701679
every       5570645
word        3342389
doccontains     5374021

I don't know what to make of this as im really new to C programming... It's probably worth mentioning the if(Word_Foun==0) doesn't execute 我不知道该怎么做,因为这对C编程来说真的很新。可能值得一提的是if(Word_Foun == 0)不执行

StructPointer =  malloc(Size * sizeof(*StructPointer));

This will be the correct allocation. 这将是正确的分配。 Otherwise you will have erroneous behavior in your code. 否则,您的代码中会有错误的行为。 Also check the return value of malloc . 还要检查malloc的返回值。

StructPointer =  malloc(Size * sizeof(*StructPointer));
if(NULL == StructPointer){
   perror("malloc failure");
   exit(EXIT_FAILURE);
}

You are allocating for struct WordElement not a for a pointer to it. 您正在为struct WordElement分配一个指针,而不是为其分配指针。 You already have a pointer to struct WordElement all that you needed was memory for a struct WordElement . 您已经有了一个用于struct WordElement的指针,您所需要的只是一个struct WordElement内存。

Also in the loop you are accessing array index out of bound 同样在循环中,您正在访问数组索引

for(int i=0; i <= Array_Size && Word_Found!=1; i++)
               ^^^

It will be i < Array_Size . 这将是i < Array_Size

In case match occurs you want to set the variable Word_found to 1 . 如果发生匹配, Word_found将变量Word_found设置为1

if(strcmp(StructPointer[i].Word, Word) == 0){
     /* it macthed */
}

Also Writer = fopen(File_Name, "w"); 另外Writer = fopen(File_Name, "w"); you should check the return value of fopen . 您应该检查fopen的返回值。

  if(Writer == NULL){
     fprintf(stderr,"Error in file opening");
     exit(EXIT_FAILURE);
  }

Also when you are increasing the Array_index place a check whether it might access the array index out of bound. 另外,在增加Array_index时,请检查它是否可能超出范围访问数组索引。

The more global variable you use for achieving a small task would make it more difficult to track down a bug. 您用于完成小任务的全局变量越多,跟踪错误的难度就越大。 It is always problematic because the places from which data might change is scattered - making it difficult to manage. 总是有问题的,因为数据可能更改的地方分散了-使其难以管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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