Advertisement
Guest User

grep

a guest
Nov 26th, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. #define _CRT_SECURE_NO_DEPRECATE
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. //FILE *input;
  7. //FILE *output;
  8. int help()
  9. {
  10. printf(" Grep.txt -i Input.txt -o Output.txt ");
  11. exit(1);
  12. }
  13. int notsymbols(char c, FILE *ab)
  14. {
  15. int a=0;
  16. if (c == '\n' || c == '\t' || c == ' ' || feof(ab))
  17. {
  18. a++;
  19. }
  20. return a;
  21. }
  22. int main ( int argc, char**argv)
  23. {
  24. FILE *input;
  25. FILE *output;
  26. input=NULL;
  27. output=NULL;
  28. char tab[2],tab2[2];
  29. int SymbolsInLine=0,line=0,TotalSymbols=0,NotSymbols=0,k=0,WordsInLine=0,TotalWords=0;
  30. if (argc == 2 && argv[1][1] == 'h')
  31. {
  32.  
  33. }
  34. else if (argc != 5)
  35. {
  36. printf("Error\nFor helt type Grep.exe -h\n");
  37. exit (1);
  38. }
  39. for (int i = 1; i < argc;i++)
  40. {
  41. if (argv[i][0] == '-')
  42. {
  43. switch (argv[i][1])
  44. {
  45. case 'h':
  46. help();
  47. break;
  48. case 'i':
  49. input = fopen(argv[i+1], "r");
  50. break;
  51. case 'o':
  52. output = fopen(argv[i+1], "w");
  53. break;
  54.  
  55. default:
  56. printf("unknown command '%c'\n", argv[i][1]);
  57. break;
  58.  
  59. }
  60. }
  61. else
  62. continue;
  63. }
  64. if(output==NULL)
  65. {
  66. printf("Error with output file\n");
  67. }
  68. else
  69. {
  70. if (input == NULL)
  71. {
  72. printf("Error with input file\n");
  73. }
  74. else
  75. {
  76. while (!feof(input))
  77. {
  78. SymbolsInLine++;
  79. char c = fgetc(input);
  80. k = SymbolsInLine % 2;
  81. int pre = (k + 1) % 2;
  82. tab2[k] = c;
  83.  
  84. if ((tab2[k] == ',' || tab2[k] == '-' || tab2[k] == '.') && tab2[pre] == ' ')
  85. {
  86. tab[k] = ' ';
  87. }
  88. else
  89. {
  90. tab[k] = c;
  91. }
  92. if ((tab[k] == ' ' || tab[k] == '\n' || tab[k] == '\t' || feof(input)) && SymbolsInLine != 1)
  93. {
  94. if (tab[pre] == ' ' || (tab[pre]>47 && tab[pre]<58))
  95. {
  96. }
  97. else
  98. {
  99. WordsInLine++;
  100. }
  101. }
  102.  
  103. NotSymbols= NotSymbols + notsymbols(c,input);
  104.  
  105. if (c == '\n' || feof(input))
  106. {
  107. line++;
  108. TotalSymbols = TotalSymbols + SymbolsInLine - NotSymbols;
  109. TotalWords = TotalWords + WordsInLine;
  110. // puting the result in output file
  111. fprintf(output, "number of symbols in line%2d=%3d\n", line, SymbolsInLine - NotSymbols);
  112. fprintf(output, "number of words in line%2d=%3d\n", line, WordsInLine);
  113.  
  114. SymbolsInLine = 0;
  115. NotSymbols = 0;
  116. WordsInLine = 0;
  117. }
  118. if (feof(input))
  119. {
  120. // putting the result in output file
  121. fprintf(output, "\nnumber of all symbols=%3d \n", TotalSymbols);
  122. fprintf(output, "number of all lines =%3d \n", line);
  123. fprintf(output, "number of all words =%3d \n", TotalWords);
  124. }
  125. }
  126. fclose(input);
  127. }
  128. fclose(output);
  129. }
  130. return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement