LumaLeeeeeee LumaLeeeeeee - 9 days ago 10
C Question

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

Sorry I am an extreme C newbie, but I was just wondering how on earth would I be able to narrow this down so that I don't have to have so many pieces of code repeated? I'm not so sure on how to create functions and call upon them in the main program so some help would be wonderful. :)

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

Answer

1) Loops are there for a purpose! Just use a for loop which runs for 2 times.

in your main() function:

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) You can instead write a function and call it twice (here I'm sending file name as argument)

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

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