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

Creating a Function in my C Program? - Getting Statistics for 2 Text Files

我想知道如何缩小范围,这样我就不必重复那么多代码了?我不太确定如何在主程序中创建函数和调用它们。

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

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