0

我的目标很简单。向用户询问包含大写/小写字母、数字和标点符号的简单文本文件。然后获取文件,打开它,并扫描上述每个元素,并记录每个元素。最后,为每个输出一个总数(即包含的总大写字母、总小写字母、总数字和总标点符号)。

不幸的是,我的问题并不是那么简单。我对 C 将输入文件传递给 fgetc 之类的函数然后对其进行测试以增加计数器的方式究竟是如何(即概念和语法)感到困惑。我的程序崩溃了,我的想法很少。我想我最初的问题是,为什么它会崩溃?我的代码如下;

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>


int main()
{
    char Character = 0;
    int words = 0;
    int upper_case = 0;
    int lower_case = 0;
    int punctuation = 0;
    int digits = 0;
    int entered_words = 0;
    char user_filename[100];
    char user_filecontent[100];



    printf("Enter the filename of the file containing text and/or digits:\n");
    printf("(hit the Enter key when done)\n");
    gets(user_filename);

    FILE *fp;
    fp = fopen (user_filename, "r");


    if (fp == NULL)
    {
        printf("\nError, Unable to open the file for reading\n");
    }


    while((fp = fgetc(fp)) != EOF)
    {


            while((Character=fgetc(fp)) != '\n');
            {

                if (isalnum (Character))
                {
                    if (!entered_words)
                    {
                        entered_words = 1;
                        words++;
                    }
                }


                else
                {
                    if (entered_words)
                    {
                        entered_words = 0;
                    }
                }


                if (isupper(Character))
                {
                    upper_case++;
                }


                else if (islower(Character))
                {
                    lower_case++;
                }


                else if (isdigit(Character))
                {
                    digits++;
                }


                else if (ispunct(Character))
                {
                    punctuation++;
                }

            }

    fclose(fp);
    }

    printf("Total number of words is %d.\n", words);
    printf("Total number of digits are %d.\n", digits);
    printf("Total number of uppercase letters is %d.\n", upper_case);
    printf("Total number of lowercase letters is %d.\n", lower_case);
    printf("Total number of punctuation characters is %d.\n", punctuation);

return 0;
}
4

3 回答 3

1
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>


int main()
{
    char Character = 0;
    int words = 0;
    int upper_case = 0;
    int lower_case = 0;
    int punctuation = 0;
    int digits = 0;
    int entered_words = 0;
    char user_filename[100];
    char user_filecontent[100];



    printf("Enter the filename of the file containing text and/or digits:\n");
    printf("(hit the Enter key when done)\n");
    gets(user_filename);

    FILE *fp;
    fp = fopen (user_filename, "r");


    if (fp == NULL)
    {
        printf("\nError, Unable to open the file for reading\n");
    }


    while((Character = fgetc(fp)) != EOF)
    {

                if (isalnum (Character))
                {
                    if (!entered_words)
                    {
                        entered_words = 1;
                        words++;
                    }
                }


                else
                {
                    if (entered_words)
                    {
                        entered_words = 0;
                    }
                }


                if (isupper(Character))
                {
                    upper_case++;
                }


                else if (islower(Character))
                {
                    lower_case++;
                }


                else if (isdigit(Character))
                {
                    digits++;
                }


                else if (ispunct(Character))
                {
                    punctuation++;
                }
    }

    fclose(fp);

    printf("Total number of words is %d.\n", words);
    printf("Total number of digits are %d.\n", digits);
    printf("Total number of uppercase letters is %d.\n", upper_case);
    printf("Total number of lowercase letters is %d.\n", lower_case);
    printf("Total number of punctuation characters is %d.\n", punctuation);

return 0;
}

我删除了内部的 while 循环,并将 fclose 函数调用移到了循环之外。

于 2012-08-12T19:39:10.180 回答
0

您在 while 测试中分配给您FILE *fp的结果:fgetc

while((fp = fgetc(fp)) != EOF)

这可能是您崩溃的原因。fp是您调用后文件的句柄fopen,之后您只需要将其传递给接受 a FILE *、likefgetc和. 的函数fclose

此外,您要调用fgetc两次:一次在while测试中,然后立即在另一次while测试中进行。即使您解决了fp问题,您也会以这种方式丢失字符。为什么需要内循环?

于 2012-08-12T19:24:54.213 回答
0

男人 fgetc 告诉

fgetc() 从流中读取下一个字符,并将其作为 unsigned char 转换为 int 或文件末尾的 EOF 或错误返回。

您必须使用 int 变量来存储此结果

于 2012-08-12T19:25:32.860 回答