Characters in a txt file.
Sorry for posting again.
I have updated the previous code but still no luck. I am trying to count the number of characters in the code, the number printed is always the number to which i initialised "ch" to, so am guessing the counter is not working. If anyone has any ideas it would be greatly appreciated. Thanks in advance.
#include <stdio.h>
#include <stdlib.h>
void content(void);
void characters(void);
FILE * input_file;
char input_filename[50];
char file[50];
int rval;
char temp;
int ch=0;
main(void)
{
printf("Please enter the file you wish to open: ");
scanf("%s", input_filename);
content();
characters();
fclose(input_file);
}
void content( )
{
input_file=fopen(input_filename, "r");
rval=fscanf(input_file, "%s", file);
printf("The file contains the following text:- %s", file);
if(input_file==NULL)
{
printf("Error opening file");
}
}
void characters( )
{
while(!feof(input_file))
{
temp=fgetc(input_file);
ch++;
if(ferror(input_file))
{
printf("Error counting characters");
}
}
printf("\nThe file is %d characters long.\n", ch);
}