Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #define OFILE_NAME "histo.txt"
- #define SIGMA 'z' - 'a' + 1
- int freqLow[SIGMA], freqUp[SIGMA], total;
- FILE *safeOpenFile(const char *const path, const char *const mode) {
- FILE *file = NULL;
- file = fopen(path, mode);
- if (file == NULL) {
- perror("opening error!");
- exit(EXIT_FAILURE);
- }
- return file;
- }
- void safeCloseFile(FILE *file) {
- if (fclose(file) == EOF) {
- perror(NULL);
- }
- }
- void getFreq(const char *const path) {
- FILE *file = safeOpenFile(path, "r");
- for (char ch; (ch = fgetc(file)) != EOF;) {
- if (!isalpha(ch)) {
- continue;
- }
- if (islower(ch)) {
- freqLow[ch - 'a']++;
- } else {
- freqUp[ch - 'A']++;
- }
- total++;
- }
- safeCloseFile(file);
- }
- void writeHisto(const char *const path) {
- FILE *file = safeOpenFile(path, "w");
- for (char ch = 'a'; ch <= 'z'; ch++) {
- if (!freqLow[ch - 'a']) {
- continue;
- }
- fprintf(file, "%c - %g%%\n", ch, 100.0 * freqLow[ch - 'a'] / total);
- }
- for (char ch = 'A'; ch <= 'Z'; ch++) {
- if (!freqUp[ch - 'A']) {
- continue;
- }
- fprintf(file, "%c - %g%%\n", ch, 100.0 * freqUp[ch - 'A'] / total);
- }
- safeCloseFile(file);
- }
- int main(int argc, char **argv) {
- if (argc == 1) {
- fprintf(stderr, "ONE MORE ARG NEEDED!\n");
- exit(-1);
- }
- getFreq(argv[1]);
- writeHisto(OFILE_NAME);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement