/* ########## # # charCount.cpp # version 1.00 # MDJ 1999-05-14 # # Counts the number of each ASCII # character (0-255) in a specified # text file. # # Copyright (c) 1999 by # M. David Johnson # BDS Software # P.O. Box 485 # Glenview, IL 60025-0485 # Phone: 847-998-1656 # Fax: 847-657-8359 # Email: mdjohnson@worldnet.att.net # Web: http://www.bds-soft.com # All Rights Reserved # ########## */ // header files #include #include #include #include #include #include int main(int argc, char *argv[]) { // Check for command line entry of filename FILE *fp; if (argc != 2) { cout << "\n\nERROR: Filename required on command line!\n\n"; exit(1); } // Establish and initialize count array // count[0] through count[255] = individual character counts // count[256] = total character count unsigned long count[257]; for (long i = 0; i < 257; i++) { count[i] = 0; } // Open file for character counting fp = fopen (argv[1], "r"); if (fp == NULL) { cout << "\n\nERROR: Could not open " << argv[1] << "!\n\n"; exit(1); } // Prepare to begin counting cout << "\n\nProgress Counter\n\n"; char ch; unsigned long cct = 0; // Do the character count for (;;) { ch = fgetc(fp); if (feof(fp) != 0) { break; } cct++; if ((cct % 10000) == 0) { cout << cct << "\n"; // progress count; } count[(long) ch]++; // individual count count[256]++; // total count } // Close the file and open the report file fclose(fp); FILE *fpR; char *rFilename = "report.txt"; fpR = fopen(rFilename, "w"); if (fpR == NULL) { cout << "\n\nError: Could not open " << rFilename << "\n\n"; exit(1); } // Output the report to the report file for (long j = 0; j < 257; j++) { fprintf(fpR, "%i %u \n", j, count[j]); } // Close the report file and indicate completion fclose(fpR); cout << "\nReport Complete\n\n"; return(0); } /* ########## # # end of charCount.cpp # ########## */