我的目标很简单。要求用户提供包含大写/小写字母,数字和标点符号的简单文本文件。然后取出文件,打开它,扫描每个上述元素,保持每个元素的计数。最后,输出每个的总数(即包含的大写字母总数,小写字母总数,总数字和总标点符号)。
不幸的是,我的问题并非如此简单。我真的很困惑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;
}
答案 0 :(得分: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函数调用移到循环外部。
答案 1 :(得分:0)
您正在考试时FILE *fp
fgetc
分配while((fp = fgetc(fp)) != EOF)
的结果:
fp
这可能是导致崩溃的原因。调用fopen
后,FILE *
是文件的句柄,之后您只需将其传递给fgetc
的函数,例如fclose
和{{ 1}}。
此外,您正在调用fgetc
两次:一次在while
测试中,然后在另一个while
测试中紧随其后。即使您修复了fp
问题,也会以这种方式丢失字符。你为什么需要内循环?
答案 2 :(得分:0)
man fgetc告诉
fgetc()从流中读取下一个字符并将其作为一个返回 unsigned char转换为int,或EOF在文件末尾或错误。
您必须使用int变量来存储此结果