单词,行和字符计数不适用于c

时间:2016-12-02 12:57:50

标签: c

我是编程新手,这段代码不想工作,我的想法已经用完了。它读入文件很好但不计算任何东西。我知道它与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");

}

1 个答案:

答案 0 :(得分:1)

代码问题是 - :

  • 你把;就在while((letter = getc(file_in))!= EOF);这就是为什么while循环读取文件中的所有字符并在没有检查if()条件的情况下出现。
  • 你是否因为你在if()条件之后放了;(分号)而没有调用linecount的条件。
  • 你想从getchar()这里找到if(isspace(letter)&amp;&amp;!isspace(getchar()))?因为这个getchar()你必须提供输入,否则循环不会终止。
  • 功能名称为print_s()。
  • 您收到的char中的getc()结果是错误的。 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);

}