在我的C程序中创建一个函数? - 获取2个文本文件的统​​计信息

问题描述:

我想知道如何才能缩小这个范围,以便我不必重复这么多代码段?我不太确定如何创建函数并在主程序中调用它们。在我的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

'tab space'呢?使用'isspace'代替,为什么这么大的数组大小只是一个文件名? – Raindrop7

1)循环存在是有原因的!一般来说,当你不得不一遍又一遍地做同样的事情时,循环就是你真正需要的东西。只需使用运行2次的for循环。

注意void main()是不适合的main()有效的签名,而使用int main(void)为你发送任何参数为main()

使用for循环,你main()看起来像:

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(); 

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)您可以使用两个循环和功能

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(); 
}