Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- char* getword(FILE* f);
- int checkword (char * word);
- int main (int argc, char* argv[])
- {
- FILE *infile;
- FILE *outfile;
- char *word = NULL;
- int c;
- if(argc!=3)
- {
- printf("Manual: type ./filter infile outfile\n");
- exit (1);
- }
- if(!(infile = fopen(argv[1], "r")))
- {
- perror("Error. Unable to open file 1. \n");
- exit(2);
- }
- if(!(outfile = fopen(argv[2], "w")))
- {
- perror("Error. Unable to open file 2. \n");
- if (!fclose (infile))
- {
- perror("Error. Unable to close file 1. \n");
- exit(2);
- }
- else exit(2);
- }
- while(!feof(infile))
- {
- word = getword (infile);
- if(checkword(word))
- {
- fprintf(outfile, "%s", word);
- }
- if ((c = getc(infile)) != EOF)
- {
- putc (c, outfile);
- }
- free(word);
- }
- if (!fclose (infile))
- {
- perror("Error. Unable to close file 1. \n");
- exit(2);
- }
- if (!fclose (outfile))
- {
- perror("Error. Unable to close file 2. \n");
- exit(2);
- }
- free(word);
- return 0;
- }
- int checkword (char * word)
- {
- int i, len;
- len = strlen (word);
- for (i=0; i<len; i++)
- {
- if(word[i]=='e')
- {
- return 0;
- }
- }
- return 1;
- }
- char *getword(FILE *infile)
- {
- char *word = NULL;
- int length = 1, c, check = 0;
- word = malloc(sizeof(int));
- while ((c = getc(infile)) == '-' || isalpha(c))
- {
- if ((word = realloc (word, length * sizeof(int))) == NULL)
- {
- perror ("Error in malloc!\n");
- free(word);
- return NULL;
- }
- word[length - 1] = c;
- length++;
- }
- ungetc (c, infile);
- if (!check)
- {
- if ((word = realloc (word, length * sizeof(int))) == NULL)
- {
- perror ("Error in malloc!\n");
- free(word);
- return NULL;
- }
- }
- word [length-1] = '\0';
- return word;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement