I was wondering how 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.
#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) Loops exist for a reason! Generally when you have to do the same thing over and over again, loops are what you really need. Just use a for
loop which runs for 2 times.
Note: void main()
is not a valid signature for main()
in c, instead use int main(void)
as you are sending no arguments into main()
Using for
loop, your main()
would look like:
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
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)You can use both loop and function!
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();
}