home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / alde_c / misc / lib / r_la4_02 / charcnt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-12-31  |  871 b   |  34 lines

  1. /* CHARCNT.C - From page 434 of "Microsoft C Programming for    */
  2. /* the IBM" by Robert Lafore. This file counts characters in    */
  3. /* a file that is opened as a text file as opposed to a file    */
  4. /* opened as a binary mode file. These 2 modes differ in the    */
  5. /* way they handle the EOF and the CR/LF-newline. See CHARCNT2.C*/
  6. /****************************************************************/
  7.  
  8. #include <stdio.h>
  9.  
  10. main(argc, argv)
  11. int argc;
  12. char *argv[];
  13. {
  14. FILE *fptr;
  15. char string[81];
  16. int count = 0;
  17.  
  18.    if(argc != 2) {
  19.       printf("\nFormat: C>charcnt2 filename");
  20.       exit();
  21.    }
  22.    if((fptr = fopen(argv[1], "r")) == NULL) {
  23.       printf("\nCan't open file %s.", argv[1]);
  24.       exit();
  25.    }
  26.    while(getc(fptr) != EOF)
  27.       count++;
  28.    fclose(fptr);
  29.    printf("\nFile %s contains %d characters.", argv[1], count);
  30. }
  31.  
  32.  
  33.  
  34.