Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- character_analysis_in_file_v1.c by Dragan Milicev
- Character analysis in a file.
- Ascii table:
- characters_0 0 - 31 special characters
- characters_1 32 - 47
- 0 - 9 48 - 57 digits
- characters_2 58 - 64
- A - Z 65 - 90 capital letters
- characters_3 91 - 96
- a - z 97 - 122 small letters
- characters_4 123 - 126
- Character occurrences statistics in a file.
- Total number of lines.
- Total number of words.
- The number of occurrences of each character.
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define BUFFER_SIZE 100
- #define NUM_OF_ASCII_CHARACTERS 256
- int main(int argc, char *argv[]) {
- FILE* pointer_to_the_input_file;
- FILE* pointer_to_the_output_file;
- char input_file_name[BUFFER_SIZE];
- char output_file_name[BUFFER_SIZE];
- char ch;
- int num_of_characters = 0; // total number of characters in the input file
- int num_of_lines = 0; // total number of lines (rows) in the input file
- int num_of_small_letters = 0; // total number of small letters in the input file
- int num_of_capital_letters = 0; // total number of capital letters in the input file
- int num_of_digits = 0; // total number of digits in the input file
- int num_of_spaces = 0; // total number of spaces (blanko) in the input file
- int num_of_tabs = 0; // total number of tabs in the input file
- int num_of_spaces_and_tabs = 0; // total number of spaces and tabs in the input file
- int arr_for_ch_occurrences[NUM_OF_ASCII_CHARACTERS]; // array for each character's occurrence numbers
- int i;
- for(i=0;i<argc;i++)
- printf("\n %3d. argument of program is \n %s \n", i, argv[i] );
- // get input file name
- if( argc == 1 ) { // if no arguments are given to the program
- printf("\n No arguments were provided when calling the program.\n");
- printf("\n You can specify the name of the input file as the program argument.");
- //exit(EXIT_FAILURE);
- // Enter the name of the input file in the program
- printf("\n\n Enter name of the input file: ");
- //scanf("%[^\n]",input_file_name); // or, better, to awoid buffer overflow:
- fgets(input_file_name,BUFFER_SIZE,stdin);
- // We have to finish the string. Replace the newline with null char.
- input_file_name[strlen(input_file_name)-1] = '\0';
- //printf("\n|%s|\n",input_file_name);
- }
- else { // arguments are given to the program
- strcpy(input_file_name,argv[1]);
- }
- //strcpy(input_file_name,"character_analysis_in_file_v1.c");
- // make output file name
- strcpy(output_file_name,"statistic_for_");
- strcat(output_file_name,input_file_name);
- strcat(output_file_name,".txt");
- printf("\n Input file name: %s \n",input_file_name);
- printf("\n Output file name: %s \n",output_file_name);
- // open the input file
- if( (pointer_to_the_input_file = fopen(input_file_name, "r")) == NULL ) {
- fprintf(stderr, "\n\n Error opening input file %s ! \n\n", input_file_name );
- exit(EXIT_FAILURE);
- }
- // open the output file
- if( (pointer_to_the_output_file = fopen(output_file_name, "w")) == NULL ) {
- fprintf(stderr, "\n\n Error opening output file %s ! \n\n", output_file_name );
- exit(EXIT_FAILURE);
- }
- // Zeroing counter array
- for(i=0;i<NUM_OF_ASCII_CHARACTERS;i++)
- arr_for_ch_occurrences[i] = 0; // array for each character's occurrence numbers
- // analysis of the input file,
- // loading one character at a time from the input file to the end (EOF) of the input file
- while( (ch = fgetc(pointer_to_the_input_file)) != EOF ) {
- num_of_characters++; // Total number of characters
- arr_for_ch_occurrences[ch]++; // array for each character's occurrence numbers
- } // while( (ch = fgetc(pointer_to_the_input_file)) != EOF )
- // the number of lines (rows) is the same as the number of /n characters whose dec code is 10
- num_of_lines = arr_for_ch_occurrences[10];
- for(i='0';i<='9';i++) // total number of digits in the input file
- num_of_digits = num_of_digits + arr_for_ch_occurrences[i];
- for(i='a';i<='z';i++) // total number of small letters in the input file
- num_of_small_letters = num_of_small_letters + arr_for_ch_occurrences[i];
- for(i='A';i<='Z';i++) // total number of capital letters in the input file
- num_of_capital_letters = num_of_capital_letters + arr_for_ch_occurrences[i];
- // the spaces (blanks) characters number is the same as the number of character whose dec code is 32
- num_of_spaces = arr_for_ch_occurrences[32];
- // the number of tabs is the same as the number of character whose dec code is 9
- num_of_tabs = arr_for_ch_occurrences[9];
- // the number of tabs and spaces (blanks)
- num_of_spaces_and_tabs = num_of_spaces + num_of_tabs;
- // display the analysis results on screen and in output file
- // (only shows characters that appear in the file)
- printf("\n\n In input file \"%s\" there are: \n\n", input_file_name );
- fprintf(pointer_to_the_output_file,"\n\n In input file \"%s\" there are: \n\n", input_file_name );
- printf(" %7d characters \n\n", num_of_characters ); // total number of characters in the input file
- fprintf(pointer_to_the_output_file," %7d characters \n\n", num_of_characters );
- printf(" %7d lines (rows) \n\n", num_of_lines ); // total number of lines (rows) in the input file
- fprintf(pointer_to_the_output_file," %7d redova \n\n", num_of_lines );
- printf(" %7d small letters \n\n", num_of_small_letters );
- fprintf(pointer_to_the_output_file," %7d small letters \n\n", num_of_small_letters );
- printf(" %7d capital letters \n\n", num_of_capital_letters );
- fprintf(pointer_to_the_output_file," %7d capital letters \n\n", num_of_capital_letters );
- printf(" %7d digits \n\n", num_of_digits );
- fprintf(pointer_to_the_output_file," %7d digits \n\n", num_of_digits );
- printf(" %7d spaces \n\n", num_of_spaces );
- fprintf(pointer_to_the_output_file," %7d spaces \n\n", num_of_spaces );
- printf(" %7d tabs \n\n", num_of_tabs );
- fprintf(pointer_to_the_output_file," %7d tabs \n\n", num_of_tabs );
- printf(" %7d spaces and tabs \n\n", num_of_spaces_and_tabs );
- fprintf(pointer_to_the_output_file," %7d spaces and tabs \n\n", num_of_spaces_and_tabs );
- printf("\n\n hex dec ch br_ch \n\n\n"); // table header
- fprintf(pointer_to_the_output_file,"\n\n hex dec ch br_ch \n\n\n");
- // display of visible characters
- for(i=32;i<NUM_OF_ASCII_CHARACTERS;i++) {
- if( arr_for_ch_occurrences[i] ) { // if the number of occurrences of the i-th character is greater than zero
- printf(" %x %3d %c %7d \n\n", i, i, i, arr_for_ch_occurrences[i] );
- fprintf(pointer_to_the_output_file," %x %3d %c %7d \n\n", i, i, i, arr_for_ch_occurrences[i] );
- }
- }
- // closing the input file
- if( ( fclose(pointer_to_the_input_file) ) == EOF ) { // close the input file
- fprintf(stderr, "\n\n Error closing input file %s ! \n\n", input_file_name );
- exit(EXIT_FAILURE);
- }
- else
- printf("\n Input file \n %s \n successfully closed. \n", input_file_name );
- // closing the output file
- if( ( fclose(pointer_to_the_output_file) ) == EOF ) { // close the output file
- fprintf(stderr, "\n\n Error closing output file %s ! \n\n", output_file_name );
- exit(EXIT_FAILURE);
- }
- else
- printf("\n Output file \n %s \n successfully closed. \n\n", output_file_name );
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement