File is given. It is necessary to count words from it and display (not repeating words) and their number.

I have a problem: the list is filled incorrectly, and the program crashes)

I got the following code:

#include <stdio.h> #include <stdlib.h> #include <conio.h> struct elem{ //Элемент списка char word[255]; int count; elem *next; }; elem * GDFF(char filename[], elem *dict); //Возвращает указатель на словарь. Get Dictionary From File. Получает словарь из файла. void printD(elem *dict); //Вывод на экран elem* addW(char word[255],elem *dict); elem *searchW(char t[],elem *dict); elem* freeD(elem *dict); void strcpy(char a[255],char b[]); void main(){ char fn[]=""; //Путь к файлу elem *dict=NULL; //Указатель на список dict=NULL; printf("Enter filename:\n"); scanf("%s",&fn); dict=GDFF(fn,dict); printD(dict); dict=freeD(dict); getch(); } void printD(elem *dict){ while(dict!=NULL){ printf("%s : %d\n",dict->word,dict->count); dict=dict->next; } } elem * GDFF(char filename[], elem *dict){ FILE *f=fopen(filename, "r"); elem *pointer; char t[255]; while(!feof(f)){ fscanf(f,"%s",&t); if(pointer=searchW(t,dict)){ pointer->count++; printf("plussed %s",pointer->word); }else{ dict=addW(t,dict); printf("added %s",dict->word); } //printf("%s\n",t); } return dict; } elem *searchW(char t[],elem *dict){ elem *p=dict; while(p!=NULL&&p->word!=t){ p=p->next; } return p; } elem* addW(char word[255],elem *dict){ elem *p=dict; elem *t=(elem*)malloc(sizeof(elem)); t->count=1; strcpy(t->word,word); t->next=NULL; if(p!=NULL){ while(p->next!=NULL){ p=p->next; } p->next=t; }else{ return t; } return dict; } elem* freeD(elem *dict){ while(dict!=NULL){ elem *t=dict; dict=dict->next; free(t); } return NULL; } void strcpy(char a[255],char b[]){ for(int i=0; i<255; i++){ a[i]=b[i]; } } 

Code corrected get the following

 #include <stdio.h> #include <stdlib.h> #include <conio.h> struct elem{ //Элемент списка char word[255]; int count; elem *next; }; elem * GDFF(char filename[], elem *dict); //Возвращает указатель на словарь. Get Dictionary From File. Получает словарь из файла. void printD(elem *dict); //Вывод на экран elem* addW(char word[255],elem *dict); elem *searchW(char t[],elem *dict); elem* freeD(elem *dict); void strcpy(char a[255],char b[]); bool equal(char a[255],char b[]); void main(){ char fn[255]=""; //Путь к файлу elem *dict=NULL; //Указатель на список dict=NULL; printf("Enter filename:\n"); scanf("%s",&fn); dict=GDFF(fn,dict); printD(dict); dict=freeD(dict); getch(); } void printD(elem *dict){ while(dict!=NULL){ printf("%s : %d\n",dict->word,dict->count); dict=dict->next; } } elem * GDFF(char filename[], elem *dict){ FILE *f=fopen(filename, "r"); elem *pointer; char t[255]; while(!feof(f)){ fscanf(f,"%s",&t); if(pointer=searchW(t,dict)){ pointer->count++; }else{ dict=addW(t,dict); printf("added %s",dict->word); } } return dict; } elem *searchW(char t[],elem *dict){ elem *p=dict; while(p!=NULL&&!equal(p->word,t)){ p=p->next; } return p; } elem* addW(char word[255],elem *dict){ elem *p=dict; elem *t=(elem*)malloc(sizeof(elem)); t->count=1; strcpy(t->word,word); t->next=NULL; if(p!=NULL){ while(p->next!=NULL){ p=p->next; } p->next=t; }else{ return t; } return dict; } elem* freeD(elem *dict){ while(dict!=NULL){ elem *t=dict; dict=dict->next; free(t); } return NULL; } void strcpy(char a[255],char b[]){ for(int i=0; i<255; i++){ a[i]=b[i]; } } bool equal(char a[255],char b[]) { for(int i=0; i<255&&a[i]&&b[i]; i++){ if(a[i]!=b[i]){ return false; } } return true; } 

    2 answers 2

    "Stack around the variable 'fn' was corrupted" - this error usually occurs when the array is exceeded. What is happening with you?

     char s[]="qwerty"; 

    If an array is created by this method, then the size of the array = the number of characters in this line + null-character, in your example it is empty => 1 character, and that service one. Enter the size of this array, and everything will be fine =)
    This is what concerns the departure program.

     p->word!=t 

    Strings cannot be compared in this way, it is necessary to compare independently, looking through all the characters, I do not remember the standard function, but this one will also come down

     bool equal(char a[],char b[]) { int i; for(i=0;a[i]&&b[i];i++) if(a[i]!=b[i]) return false; return (!a[i]&&!b[i]); } 
    • Thank you) It is somewhat inconvenient to work with an array of char) - Sergey
    • Use string type. - gecube

    the standard string comparison function int strcmp (char [] str1, char [] str2) returns 0 if the strings are equal, value <0 if 1st str1 is less than str2, and value> 0 if str1> str2.

    • The question of course has already been answered, but still this function can work with char? or only with string? if with the second it does not fit. - Sergey
    • yes, it works with char [] and with string - Gautama Buddha