The problem(actually various problems):
if choice != 0 either the insert_text function is called or add_word_in_dictionary is called which is not wanted..
I want to create a function which saves a file.Thus, I created the save_file function but I am not that sure if it actually 'saves' the file..
the count_difrnt_words function..With this function I want to count how many different words are in a file but I do not know the length of each string-word in the file so I guess fgets can't be used in this occasion..
The englishWords.txt is a large file with words like this https://prnt.sc/3BKsO7_Ud2XG
and the other file that is used,is a 'common' .txt file with words,special characters,spaces between words etc..
The code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int get_choice(void);
int check_word(char *word);
void insert_text(FILE ** p_fp);
char *add_word(void);
void add_word_in_dictionary(FILE ** p_fp);
void save_file(FILE **fp);
int count_characters(FILE **fp);
int count_spaces(FILE **fp);
int main()
{
FILE *fp;
FILE *fp2; /*fp for Alice....txt and fp2 for englishWords.txt */
int choice = 0;
while(1)
{
choice = get_choice();
if(choice == 0)
{
insert_text(&fp);
}
if(choice == 1);
{
add_word_in_dictionary(&fp2);
}
if(choice == 2)
{
printf("\n You have entered correction mode but in this
version, nothing happens \n");
}
if(choice == 3)
{
save_file(&fp);
printf("\n The file has been saved \n");
}
if(choice == 4)
{
count_characters(&fp);
count_spaces(&fp);
count_difrnt_words(&fp);
}
if(choice == 5)
{
break;
}
}
printf("\n The program has ended \n");
return 0;
}
int get_choice(void) {
int choice = 0;
printf("\n Select a choice from the below \n");
printf("\n Select 0 to add text \n");
printf("\n Select 1 to add new words in the dictionary \n");
printf("\n Select 2 to enter enter correction mode \n");
printf("\n Select 3 to save the text \n");
printf("\n Select 4 to see the statistics about your text \n");
printf("\n Select 5 to exit the program\n");
scanf("\n%d", &choice);
return choice;
}
int check_word(char *word)
{
FILE *readfile;
char word1[40];
readfile = fopen("englishWords.txt","r");
if(!readfile)
{
printf("\n There was an error opening the file \n");
return;
}
while(fscanf(readfile,"%s",word1) != EOF)
{
if(strcmp(word,word1) == 0)
return 1;
else
{
return 0;
}
}
fclose(readfile);
}
void insert_text(FILE ** p_fp)
{
*p_fp = fopen("AlicesAdventuresInWonderland.txt", "a+");
fprintf(*p_fp, "%99s\n",add_word());
return;
}
char *add_word(void)
{
char string[100] = {""};
printf("\n Please enter the word \n");
scanf("\n%[^\n]", &string);
return string;
}
void add_word_in_dictionary(FILE ** p_fp)
{
*p_fp = fopen("englishWords.txt","a+");
fprintf(*p_fp, "%99s\n",add_word());
return;
}
void save_file(FILE **fp)
{
fclose(*fp);
return;
}
int count_characters(FILE **fp)
{
char ch;
int count_ch = 0;
*fp = fopen("AlicesAdventuresInWonderland.txt", "r");
if(*fp == NULL)
{
printf("\n There was an error opening the file \n");
return;
}
while(ch != EOF)
{
if(ch != ' ' && ch != '\n')
{
count_ch++;
}
ch=fgetc(*fp);
}
fclose(*fp);
return count_ch;
}
int count_spaces(FILE **fp)
{
int count_sp;
char c;
*fp = fopen("AlicesAdventuresInWonderland.txt","r");
while ((c = fgetc(*fp)) != EOF)
{
if (c == ' ')
count_sp++;
}
return count_sp;
}
int count_difrnt_words(FILE **fp)
{
int i;
int num = 0;
char str[15];
char c;
*fp = fopen("AlicesAdventuresInWonderland.txt","r");
while((c = fgetc(*fp)) != EOF)
{
fgets(str,15,*fp);
if(strcmp(fgets(str,15,*fp),fgets(str,15,*(fp+i))))
{
num++;
}
i++;
}
return num;
}