Number and number of characters in the statistics file for C Language exercises
Count the number of characters in the file (using command line parameters)
# Include <stdio. h>
# Include <stdlib. h>
Int main (int argc, char * argv [])
{
Char ch;
FILE * fp;
Long count = 0;
If (argc! = 2)
{
Printf ("File Name: % s \ n", argv [0]);
Exit (EXIT_FAILURE );
}
If (fp = fopen (argv [1], "r +") = NULL)
{
Fprintf (stderr, "cannot open file \" % s \ "\ n", argv [1]);
Exit (EXIT_FAILURE );
}
While (ch = getc (fp ))! = EOF)
{
Putc (ch, stdout );
++ Count;
}
Fclose (fp );
Printf ("File % s has % ld characters \ n", argv [1], count );
Return 0;
}
Count the number of characters in the file (command line parameters are not used)
# Include <stdio. h>
# Include <stdlib. h>
# Define MAX 80
Int main (void)
{
FILE * fp;
Char ch;
Char name [MAX];
Long count = 0;
Printf ("Enter the file name :");
Gets (name );
If (fp = fopen (name, "r +") = NULL)
{
Fprintf (stderr, "cannot open file % s \ n", name );
Exit (EXIT_FAILURE );
}
While (ch = getc (fp ))! = EOF)
{
Putc (ch, stdout );
Count ++;
}
Fclose (fp );
Printf ("File % s has % ld characters \ n", name, count );
Return 0;
}