gpt4 book ai didi

c - “如何让我的程序从文件中读取可见字符和空格

转载 作者:太空宇宙 更新时间:2023-11-04 06:47:37 25 4
gpt4 key购买 nike

我应该“修复”给我的代码,以使其在文件中显示正确数量的可见字符(也包括空格)。正确的数字应该是 977。我以前从未处理过文件,我不明白我需要做什么才能显示正确的数字。

 * Driver Menu System for Homework
* Andrew Potter - Mar 5, 2019 <-- Please put your name/date here
*/

#include <stdio.h>//header file for input/output -
#include <stdlib.h>
#include <ctype.h>
// since you will place all your assigned functions (programs) in this file, you do not need to include stdio.h again!

int menu(void); //prototype definition section
void hello(void);
void countall(void);

int main(void)
{
int selection = menu();

while(selection != 99) {

switch(selection) {

case 1:
hello();
break;

case 2:
countall();
break;

case 3:

break;

case 4:

break;

default:
printf("Please enter a valid selection.\n");
}

selection = menu();
}

return 0;
}

int menu(void) {
int choice;
printf("***************************\n");
printf(" 1. Hello \n");
printf(" 2. Countall\n");
printf(" 3. \n");
printf(" 4. \n");
printf("99. Exit\n");
printf("Please select number and press enter:\n");
printf("***************************\n");
scanf("%d", &choice);
getchar();
return choice;
}

void hello(void) {
printf("Hello, World!!!\n");
}

//*****Andrew 5/1/19*****

#define SLEN 81 /* from reverse.c */
/* original header: int count(argc, *argv[]) */
void countall(void)
{
int ch; // place to store each character as read
FILE *fp; // "file pointer"
long unsigned count = 0;
char file[SLEN]; /* from reverse.c */

/*Checks whether a file name was included when run from the command prompt
* The argument count includes the program file name. A count of 2 indicates
* that an additional parameter was passed
if (argc != 2)
{
printf("Usage: %s filename\n", argv[0]);
exit(EXIT_FAILURE);
}
* The following uses the second parameter as the file name
* and attempts to open the file
if ((fp = fopen(argv[1], "r")) == NULL)
{
printf("Can't open %s\n", argv[1]);
exit(EXIT_FAILURE);
} */

/*************************************
Code from reverse.c included to make the program work from within our IDE
*************************************/
puts("Enter the name of the file to be processed:");
scanf("%s", file);

if ((fp = fopen(file,"rb")) == NULL) /* read mode */
{
printf("count program can't open %s\n", file);
exit(EXIT_FAILURE);
}

/* EOF reached when C realizes it tried to reach beyond the end of the file! */
/* This is good design - see page 573 */
while ((ch = getc(fp)) != EOF)
{
if (isprint(ch)) {
count++;
}
else if (isprint(ch)) {
count++;
}
putc(ch,stdout); // same as putchar(ch);
count++;
}
fclose(fp);
printf("\nFile %s has %lu characters\n", file, count);
}

我预计我会使用 isprint 和 isspace 的组合得到正确数量的可见字符,但我通常得到 2086。赋值说明是:“Word 识别 977 个字符,包括空格。您当前的 countall() 认为有 1043 个。对您的代码进行必要的更正以仅计算可见字符和空格!(提示:查看教科书中的 567。) "在我编辑任何代码之前,计数是 1043,现在我得到 2020。我需要 977。

最佳答案

isprint() 返回一个 bool 结果 - 如果字符不是“可打印的”则为零,如果是则为非零。因此 isprint(ch) != '\n' 没有意义。你在问题中的完整表达更没有意义,但我会在最后继续讨论。

isprint() 本身为所有可打印字符返回 true(非零),因此您不需要其他测试。此外,您在每个条件 block 中无条件地递增 count,因此您对每个字符进行计数,并且对某些字符进行两次计数。

你只需要:

if( isprint(ch) )
{
count++;
}
putc( ch, stdout ) ;

虽然您的代码显然是一个不完整的片段,但不清楚您在哪里或如何阅读 ch。您需要一个 getc() 或其中的等效项。

while( (ch = getc(fp)) != EOF ) 
{
if( isprint(ch) )
{
count++;
}
putc( ch, stdout ) ;
}

目前尚不清楚您是否需要计算所有空格(包括空格、制表符和换行符)或如您所述仅计算“空格”。如果是这样,请明确 isprint() 将匹配空格,但不匹配控制字符换行符或制表符。 isspace() 匹配所有这些,但不应单独计入 isprint(),因为“空格”在空白和可打印集中。如果要计算换行符和制表符(不太可能;“垂直制表符”),则:

while( (ch = getc(fp)) != EOF ) 
{
if( isprint(ch) || isspace(ch) )
{
count++;
}

putc( ch, stdout ) ;
}

您似乎误解了 C 的另一个方面是 bool 表达式的工作原理。要测试单个变量的多个值,您必须编写:

if( var == x || var == y || var == z )

你写了:

if( var == x || y || z )

当你大声朗读它时,这在英语(或其他自然语言)中可能有意义,但在 C 中它意味着:

if( var == (x || y || z ) )

评估 (x || y || z )truefalse 并将其与 var 进行比较>.

可能值得考虑现有解决方案的语义,以说明为什么它实际上可以编译,但会产生错误的结果。

首先,

  isprint(ch) != '\n' || '\t' || '\0' 

等同于 isprint(ch) != true,原因如前所述。因此,您为所有 不可可打印的字符递增计数器。

然后在这里:

  isspace(ch) == NULL

NULL 是表示无效指针的宏,isspace() 不返回指针。但是 NULL 将隐式转换为零(或 false)。因此,在这里您为 所有 不是空格的可打印字符递增计数器。

最后,您无条件地计算这里的每个字符:

    putc(ch,stdout);  // same as putchar(ch);
count++;

所以你的结果将是:

number-of-non-printing-characters + 
number-of-printing-characters - number-of-spaces +
total-number-of-characters

这是我认为的 (2 x file-length) - number-of-spaces

最后请注意,如果您以“二进制”模式打开具有 CR+LF 行尾的文本文件(Windows 上文本文件的常规方式),isspace() 将为每个新的字符计数两个字符-线。请务必以“文本”模式打开(无论平台如何)。

关于c - “如何让我的程序从文件中读取可见字符和空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55996853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com