Good day! I have this simple program to count characters in a file and output the length, but whenever I open the file "File.txt" I always get that its "NULL", does anyone have any ideas?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// declare function before it's called
void Count (char * FILENAME);
int main ()
{
//int c;
char f1[100]; // array big enough to hold expected input
printf ("Please enter file for character counting\n");
scanf ("%99s", f1); // set max characters to read to prevent buffer overflow
Count(f1);
int c = getchar ();
while (c>0)
;
return 0;
}
void Count (char * FILENAME)
{
printf("Counting started\n");
FILE *inFile;
int Length;
int Byte; // fgetc returns int, so use an int for "Byte" and "newByte"
inFile = fopen(FILENAME,"r");
if(inFile == NULL)
{
printf("Error: Can't Open file!\n");
}
else
{
Length = 0;
while((Byte = fgetc(inFile)) != EOF) // read a byte, check if EOF, "EOF" is outside the range of "char most times"
{
++Length;
}
}
if (inFile != NULL) fclose(inFile); // close your files when you're done
printf ("The file contains %d characters, blanks, tabs, spaces are included!", Length);
}