处理文件时各种问题-C90

发布于 2025-01-22 19:42:29 字数 4160 浏览 4 评论 0原文

问题(实际上是各种问题):

  1. 如果选择!= 0 insert_text 函数被称为或 add_word_ind_in_dictionary 被称为不想要的。

  2. 我想创建一个保存文件的函数 ..

  3. 。 /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):

  1. if choice != 0 either the insert_text function is called or add_word_in_dictionary
    is called which is not wanted..

  2. 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..

  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

书间行客 2025-01-29 19:42:29

您的add_word函数将指针返回到本地变量。当功能结束时,局部变量立即停止存在(变量的寿命与函数返回一起结束)。

任何指向本地变量的指针都将无效,并且不能使用。无论如何使用它会导致不确定的行为。

我建议您更改add_word函数以接受数组作为参数:

char *add_word(char *word)
{
    printf("\n Please enter the word \n");
    scanf(" %100[^\n]", word);
    return word;
}

此更改意味着您必须更改调用它的方式,并且需要在调用函数中定义数组。

例如:

void insert_text(FILE ** p_fp)
{
    *p_fp = fopen("AlicesAdventuresInWonderland.txt", "a+");

    char word[100];
    fprintf(*p_fp, "%s\n", add_word(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:

char *add_word(char *word)
{
    printf("\n Please enter the word \n");
    scanf(" %100[^\n]", word);
    return word;
}

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:

void insert_text(FILE ** p_fp)
{
    *p_fp = fopen("AlicesAdventuresInWonderland.txt", "a+");

    char word[100];
    fprintf(*p_fp, "%s\n", add_word(word));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文