2016-11-27 6 views
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(); 
} 
+0

「タブスペース」について代わりに 'isspace'を使用してください。なぜ、ファイル名のような大きな配列サイズですか? – Raindrop7

答えて

1

1)ループが理由のために存在します!一般的に何度も同じことをやり直さなければならないときは、ループが本当に必要なものです。ループを2回実行するforループを使用してください。

char fileName[1500] = ""; //just a single variable is enough to store file name 
//other declarations 

for(int i = 1; i <= 2; i++) //loop to make it happen 2 times 
{ 
    printf("Enter the %d filename: ", i); 
    gets(fileName); 

    openFile = fopen(fileName, "r"); 

    //handle file opening error 
    if(OpenFile == NULL){ 
     printf("file opening error"); 
     exit(1); 
    } 

    //code that gets repeated 

    fclose(openFile); 
} 

getch(); 
void main()はあなたが forループを使用して main()

に引数を送信されていないとして、代わりにint main(void)を使用し、main()のための有効な署名ではありません、あなたのmain()は次のようになります。


2)代わりにを書くことができます機能

void my_function(char* file_name) 
{ 
    char character; 
    int lineCount, wordCount, charCount; 

    lineCount = 0; 
    wordCount = 0; 
    charCount = 0; 

    openFile = fopen(file_name, "r"); 

    //handle file opening error 
    if(OpenFile == NULL){ 
     printf("file opening error"); 
     exit(1); 
    } 

    //code that gets repeated 

    fclose(openFile); 
} 

//your main 

int main (void) { 
    char fileName1[1500] = "", fileName2[1500] = ""; 

    printf("Enter the first filename: "); 
    gets(fileName1); 
    my_function(fileName1); 

    printf("Enter the second filename: "); 
    gets(fileName2); 
    my_function(fileName2); 

    getch(); 
} 

3(ここで私は、引数としてファイル名を送信しています)を2回それを呼び出す)あなたはループの両方と機能を使用することができます!

void my_function(char* file_name) 
{ 
    //body of the function same as in before example 
} 

//your main 

int main (void) { 
    char fileName[1500] = ""; 

    for(int i = 1; i <= 2; i++){ 
     printf("Enter the %d filename: ", i); 
     gets(fileName); 
     my_function(fileName); 
    } 

    getch(); 
} 
関連する問題