home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Sams Teach Yourself C in 21 Days (6th Edition)
/
STYC216E.ISO
/
mac
/
Examples
/
TR
/
count_ch.c
< prev
next >
Wrap
C/C++ Source or Header
|
2002-05-04
|
2KB
|
65 lines
/* Counts the number of occurrences
of each character in a file. */
#include <stdio.h>
#include <stdlib.h>
int file_exists(char *filename);
int main( void )
{
char ch, source[80];
int index;
long count[127];
FILE *fp;
/* Get the source and destination filenames. */
fprintf(stderr, "\nEnter source file name: ");
gets(source);
/* See that the source file exists. */
if (!file_exists(source))
{
fprintf(stderr, "\n%s does not exist.\n", source);
exit(1);
}
/* Open the file. */
if ((fp = fopen(source, "rb")) == NULL)
{
fprintf(stderr, "\nError opening %s.\n", source);
exit(1);
}
/* Zero the array elements. */
for (index = 31; index < 127 ; index++)
count[index] = 0;
while ( 1 )
{
ch = fgetc(fp);
/* Done if end of file */
if (feof(fp))
break;
/* Count only characters between 32 and 126. */
if (ch > 31 && ch < 127)
count[ch]++;
}
/* Display the results. */
printf("\nChar\t\tCount\n");
for (index = 32; index < 127 ; index++)
printf("[%c]\t%d\n", index, count[index]);
/* Close the file and exit. */
fclose(fp);
return(0);
}
int file_exists(char *filename)
{
/* Returns TRUE if filename exists,
FALSE if not. */
FILE *fp;
if ((fp = fopen(filename, "r")) == NULL)
return 0;
else
{
fclose(fp);
return 1;
}
}