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

标签 c function file program-entry-point

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

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

关于在我的 C 程序中创建函数? - 获取 2 个文本文件的统​​计信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40832791/

相关文章:

c - 尝试从服务器向客户端发送消息的段错误

c - 嵌入式系统(c 代码)中使用的 tdf 文件是什么?

c - 长按键 GTK

c - 接收错误 - 请求成员 ****** 不是结构或 union

arrays - 返回一个映射并将结果存储在不同的变量中

c - 使用 read() 将文件内容写入屏幕和 C 中的其他文件的问题

c - C 函数调用的返回值?

javascript - 当我传递引用字符串的错误语句时,while 循环不会停止

html - 如何将文件从 html 选择器发送到 golang apis?

c# - ASP.Net MVC : Display file in browser, 在浏览器选项卡中显示文件名