1
私はこれをどのように絞り込むことができ、多くのコードを繰り返す必要がないのでしょうか?私は、関数を作成し、それらをメインプログラムで呼び出す方法についてはあまりよく分かりません。私のCプログラムで関数を作成しますか? - 2つのテキストファイルの統計情報を取得する
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main() {
FILE *openFile;
char fileName1[1500] = "", fileName2[1500] = "";
char character;
int lineCount, wordCount, charCount;
printf("Enter the first filename: ");
gets(fileName1);
openFile = fopen(fileName1, "r");
lineCount = 0;
wordCount = 0;
charCount = 0;
if (openFile) {
while ((character = getc(openFile)) != EOF)
{
if (character != ' ' && character != '\n')
{
++charCount;
}
if (character == ' ' || character == '\n')
{
++wordCount;
}
if (character == '\n')
{
++lineCount;
}
}
if (charCount > 0)
{
++lineCount;
++wordCount;
}
}
else
{
printf("Failed to open the file\n");
}
printf("The number of lines : %d \n", lineCount);
printf("The number of words: %d \n", wordCount);
printf("The number of characters : %d \n", charCount);
fclose(openFile);
printf("Enter the second filename: ");
gets(fileName2);
openFile = fopen(fileName2, "r");
lineCount = 0;
wordCount = 0;
charCount = 0;
if (openFile) {
while ((character = getc(openFile)) != EOF)
{
if (character != ' ' && character != '\n')
{
++charCount;
}
if (character == ' ' || character == '\n')
{
++wordCount;
}
if (character == '\n')
{
++lineCount;
}
}
if (charCount > 0)
{
++lineCount;
++wordCount;
}
}
else
{
printf("Failed to open the file\n");
}
printf("The number of lines : %d \n", lineCount);
printf("The number of words: %d \n", wordCount);
printf("The number of characters : %d \n", charCount);
fclose(openFile);
getchar();
}
「タブスペース」について代わりに 'isspace'を使用してください。なぜ、ファイル名のような大きな配列サイズですか? – Raindrop7