-1
私はプログラミングの初心者です。このコードは動作したくないので、アイデアが足りなくなりました。それはファイルをうまく読み込みますが、何も数えません。私はwhileステートメントとは何かを知っています。これは2つの別々のファイル用ですが、どちらも最後に表示する必要があります。あなたのコードで単語、行、文字の数がcで機能していない
#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");
}
あなたは[ 'getc'](http://en.cppreference.com/w/c/io/fgetc)について重要なことを見逃し、それは' int'を返します。これは 'EOF'と比較するときに重要です。また、スペースを検出すると、別の文字、つまりカウントされない文字が読み込まれます。エラーチェックはありません。ファイルを開けないとどうなりますか?最後に、デバッガの使い方を学んでください。デバッガを使用すると、変数とその値を監視しながら、コードを1行ずつ進めることができます。このようにすれば、問題を簡単に検出できるはずです。 –
'getchar()'はstdinから読み込みます。私はあなたが連続したスペースを無視しようとしていると思います。そうであれば、読み込まれた前のcharを追跡するほうが簡単かもしれません。 –
また、あなたの 'printf'文が間違っています。これを読んでください:http://en.cppreference.com/w/cpp/io/c/fprintf –