Guest User

Untitled

a guest
Nov 20th, 2019
967
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.84 KB | None | 0 0
  1. /* Basic C implementation of "wc -w" */
  2.  
  3. #include <ctype.h>
  4. #include <stdio.h>
  5.  
  6. int exitstatus = 0;
  7.  
  8. void wc(char *filename) {
  9.     FILE *fp;
  10.     int ch;
  11.     int was_white = 1;
  12.     int words = 0;
  13.  
  14.     if (!(fp = fopen(filename, "r"))) {
  15.         fprintf(stderr, "%s: can't read\n", filename);
  16.         exitstatus = 1;
  17.         return;
  18.     }
  19.  
  20.     while ((ch = fgetc(fp)) != EOF) {
  21.         if (isspace(ch)) {
  22.             was_white = 1;
  23.         } else {
  24.             if (was_white)
  25.                 words++;
  26.             was_white = 0;
  27.         }
  28.     }
  29.  
  30.     fclose(fp);
  31.  
  32.     printf("%d\t%s\n", words, filename);
  33. }
  34.  
  35. int main(int argc, char **argv) {
  36.     int i;
  37.  
  38.     if (argc < 2) {
  39.         fprintf(stderr, "usage: wc FILE...\n");
  40.         return 1;
  41.     }
  42.  
  43.     for (i = 1; i < argc; i++)
  44.         wc(argv[i]);
  45.  
  46.     return exitstatus;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment