Advertisement
Guest User

quantity words

a guest
May 2nd, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <locale.h>
  4.  
  5. int main (int argc, char **argv)
  6. {
  7.     FILE *fin, *fout;
  8.     int ch, state = 0, words = 0;
  9.     setlocale(0, "");    
  10.    
  11.     if (argc == 1)
  12.     {
  13.         printf("Программа для подсчета слов в файле\n"
  14.                "wordqty [входной файл] [выходной файл]\n"
  15.                "если нет выходного файла результат выводится на дисплей\n");
  16.         system("pause");
  17.         exit(1);
  18.     }
  19.    
  20.     if (argc > 1)
  21.     {
  22.         if ((fin = fopen(argv[1], "r")) == NULL)
  23.         {
  24.             puts("Не могу открыть файл\n");
  25.             system("pause");
  26.             exit(1);
  27.         }
  28.         while ((ch = getc(fin)) != EOF)
  29.         {
  30.             if (ch == ' ' || ch == '\t' || ch == '\n')
  31.                 state = 0;
  32.             else if (state == 0)
  33.             {
  34.                 state = 1;
  35.                 words++;
  36.             }
  37.         }
  38.         if ((fout = fopen(argv[2], "w")) != NULL)
  39.             fprintf(fout, "Количество слов: %d\n", words);
  40.         else
  41.         {
  42.             printf("Количество слов: %d\n", words);
  43.             system("pause");
  44.         }
  45.     }
  46.    
  47.     fclose(fin);
  48.     fclose(fout);
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement