Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_DEPRECATE
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- //FILE *input;
- //FILE *output;
- int help()
- {
- printf(" Grep.txt -i Input.txt -o Output.txt ");
- exit(1);
- }
- int notsymbols(char c, FILE *ab)
- {
- int a=0;
- if (c == '\n' || c == '\t' || c == ' ' || feof(ab))
- {
- a++;
- }
- return a;
- }
- int main ( int argc, char**argv)
- {
- FILE *input;
- FILE *output;
- input=NULL;
- output=NULL;
- char tab[2],tab2[2];
- int SymbolsInLine=0,line=0,TotalSymbols=0,NotSymbols=0,k=0,WordsInLine=0,TotalWords=0;
- if (argc == 2 && argv[1][1] == 'h')
- {
- }
- else if (argc != 5)
- {
- printf("Error\nFor helt type Grep.exe -h\n");
- exit (1);
- }
- for (int i = 1; i < argc;i++)
- {
- if (argv[i][0] == '-')
- {
- switch (argv[i][1])
- {
- case 'h':
- help();
- break;
- case 'i':
- input = fopen(argv[i+1], "r");
- break;
- case 'o':
- output = fopen(argv[i+1], "w");
- break;
- default:
- printf("unknown command '%c'\n", argv[i][1]);
- break;
- }
- }
- else
- continue;
- }
- if(output==NULL)
- {
- printf("Error with output file\n");
- }
- else
- {
- if (input == NULL)
- {
- printf("Error with input file\n");
- }
- else
- {
- while (!feof(input))
- {
- SymbolsInLine++;
- char c = fgetc(input);
- k = SymbolsInLine % 2;
- int pre = (k + 1) % 2;
- tab2[k] = c;
- if ((tab2[k] == ',' || tab2[k] == '-' || tab2[k] == '.') && tab2[pre] == ' ')
- {
- tab[k] = ' ';
- }
- else
- {
- tab[k] = c;
- }
- if ((tab[k] == ' ' || tab[k] == '\n' || tab[k] == '\t' || feof(input)) && SymbolsInLine != 1)
- {
- if (tab[pre] == ' ' || (tab[pre]>47 && tab[pre]<58))
- {
- }
- else
- {
- WordsInLine++;
- }
- }
- NotSymbols= NotSymbols + notsymbols(c,input);
- if (c == '\n' || feof(input))
- {
- line++;
- TotalSymbols = TotalSymbols + SymbolsInLine - NotSymbols;
- TotalWords = TotalWords + WordsInLine;
- // puting the result in output file
- fprintf(output, "number of symbols in line%2d=%3d\n", line, SymbolsInLine - NotSymbols);
- fprintf(output, "number of words in line%2d=%3d\n", line, WordsInLine);
- SymbolsInLine = 0;
- NotSymbols = 0;
- WordsInLine = 0;
- }
- if (feof(input))
- {
- // putting the result in output file
- fprintf(output, "\nnumber of all symbols=%3d \n", TotalSymbols);
- fprintf(output, "number of all lines =%3d \n", line);
- fprintf(output, "number of all words =%3d \n", TotalWords);
- }
- }
- fclose(input);
- }
- fclose(output);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement