我是编程新手,这段代码不想工作,而且我已经没有想法了。它可以很好地读取文件,但不会计算任何内容。我知道它与 while 语句有关。这是针对两个单独的文件,但它们都需要显示在末尾。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void)
{
//setting names of ints and chars.
FILE *file_in;
int wordcount, linecount, charcount;
char letter;
char filename1[50];
char filename2[50];
//setting all counts to 0.
wordcount = 0;
linecount = 0;
charcount = 0;
//Gets the user to enter name of file, then puts it in string.
printf("\n Enter first text document\n");
scanf("%s", filename1);
printf("\n Enter second text document\n");
scanf("%s", filename2);
//opens then reads the first file.
file_in = fopen(filename1, "r");
// counts the number of words, then lines, then letters in doc 1.
while ((letter = getc(file_in)) != EOF);
{
if (isspace(letter) && !isspace(getchar()))
{
wordcount++;
}
if (letter == '\n');
{
linecount++;
}
if (letter == '-')
{
charcount++;
}
}
fclose(file_in);
//opens then reads the second file.
file_in = fopen(filename2, "r");
// counts the number of words, then lines, then letters in doc 2.
while ((letter = getc(file_in)) != EOF);
{
if (isspace(letter) && !isspace(getchar()))
{
wordcount++;
}
if (letter == '\n');
{
linecount++;
}
if (letter == '-')
{
charcount++;
}
}
//displays the total on screen.
printf_s("Words:", wordcount, "\n");
printf_s("Letters", charcount, "\n");
printf_s("Lines", linecount, "\n");
}
最佳答案
您的代码存在问题 -:
- 你把;就在 while ((letter = getc(file_in)) != EOF); 之后这就是为什么 while 循环读取文件中的所有字符并在不检查 if() 条件的情况下输出。
- 没有必要调用行计数的 if 条件,因为您还在 if() 条件之后放置了 ;(分号)。
- 你想从 if (isspace(letter) && !isspace(getchar())) 中的 getchar() 得到什么?由于这个 getchar() 你必须给出输入,否则 while 循环将不会终止。
- 有一个函数名称是 print_s()。
- 您收到的 getc() 结果是 char,这是错误的。 getc() 及其相关函数的返回值是 int,而不是 char。
最后一点你可以引用。
While (( c = getc(file)) != EOF) loop won't stop executing
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void)
{
//setting names of ints and chars.
FILE *file_in;
int wordcount, linecount, charcount;
char letter;
char filename1[50];
char filename2[50];
//setting all counts to 0.
wordcount = 0;
linecount = 0;
charcount = 0;
//Gets the user to enter name of file, then puts it in string.
printf("\n Enter first text document\n");
scanf("%s", filename1);
printf("\n Enter second text document\n");
scanf("%s", filename2);
//opens then reads the first file.
file_in = fopen(filename1, "r");
// counts the number of words, then lines, then letters in doc 1.
while ((letter = getc(file_in)) != EOF)
{
if (isspace(letter))
{
wordcount++;
}
if (letter == '\n')
{
linecount++;
}
if (letter == '-')
{
charcount++;
}
}
fclose(file_in);
//opens then reads the second file.
file_in = fopen(filename2, "r");
// counts the number of words, then lines, then letters in doc 2.
while ((letter = getc(file_in)) != EOF);
{
if (isspace(letter) && !isspace(getchar()))
{
wordcount++;
}
if (letter == '\n');
{
linecount++;
}
if (letter == '-')
{
charcount++;
}
}
//displays the total on screen.
printf("Words......%d:", wordcount);
printf("Letters....%d", charcount);
printf("Linesi....%d", linecount);
}
关于c - 字数、行数和字符数不适用于 c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40932792/