Advertisement
theelitenoob

File Stat Counter

Jul 12th, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main(int argc, char *argv[]){
  6.  
  7.     int charCount, spaceCount, wordCount;
  8.     charCount = spaceCount = wordCount = 0;
  9.  
  10.     int ch, och = 0; // EOF Flag From Input or Character
  11.     FILE *inputFile = fopen(argv[1], "r");
  12.  
  13.     if(inputFile == 0){
  14.         printf("\nCannot open a file which doesn't exist\n");
  15.         exit(8); // not really sure why it's 8, it just worked... :)
  16.     }
  17.  
  18.     int word = 0; // in or out of word
  19.  
  20.     while(1){
  21.         ch = fgetc(inputFile);
  22.         if(ch == EOF)
  23.             break;
  24.         if(ch == ' '){
  25.             ++spaceCount;
  26.             word = 0;
  27.         }else{
  28.             if(word == 0){
  29.                 ++wordCount;
  30.                 word = 1;
  31.             }
  32.         }
  33.         ++charCount;
  34.     }
  35.    
  36.     printf("This file has %d spaces; %d words; and %d chars.\n", spaceCount, wordCount, charCount);
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement