处理文件时各种问题-C90
问题(实际上是各种问题):
如果选择!= 0 insert_text 函数被称为或 add_word_ind_in_dictionary 被称为不想要的。
- 我想创建一个保存文件的函数 ..
- 。 /em>函数..在此函数的情况下,我想计算文件中有多少个不同的单词,但我不知道文件中每个字符串的长度,所以我猜 fgets 无法在这种情况下使用。
情况 https://prnt.sc/3bkso7_ud2xg
和其他文件,用单词,特殊字符,单词之间的空格等文件。
代码:
#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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
add_word
函数将指针返回到本地变量。当功能结束时,局部变量立即停止存在(变量的寿命与函数返回一起结束)。任何指向本地变量的指针都将无效,并且不能使用。无论如何使用它会导致不确定的行为。
我建议您更改
add_word
函数以接受数组作为参数:此更改意味着您必须更改调用它的方式,并且需要在调用函数中定义数组。
例如:
Your
add_word
function return a pointer to a local variable. Local variables cease to exists immediately when the function ends (the life-time of the variable ends together with the function return).Any pointer to a local variable will become invalid and can't be used. Using it anyway will lead to undefined behavior.
I suggest you change the
add_word
function to accept the array as an argument instead:This change means you have to change how you call it, and that the array needs to be defined in the calling function instead.
For example: