Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. char* getword(FILE* f);
  7. int checkword (char * word);
  8.  
  9. int main (int argc, char* argv[])
  10. {
  11.     FILE *infile;
  12.     FILE *outfile;
  13.     char *word = NULL;
  14.     int c;
  15.     if(argc!=3)
  16.     {
  17.         printf("Manual: type ./filter infile outfile\n");
  18.         exit (1);
  19.     }
  20.     if(!(infile = fopen(argv[1], "r")))
  21.     {
  22.         perror("Error. Unable to open file 1. \n");
  23.         exit(2);
  24.     }
  25.     if(!(outfile = fopen(argv[2], "w")))
  26.     {
  27.         perror("Error. Unable to open file 2. \n");
  28.         if (!fclose (infile))
  29.         {
  30.             perror("Error. Unable to close file 1. \n");
  31.             exit(2);
  32.         }
  33.         else exit(2);      
  34.     }
  35.     while(!feof(infile))
  36.     {
  37.         word = getword (infile);       
  38.         if(checkword(word))
  39.         {
  40.             fprintf(outfile, "%s", word);
  41.         }  
  42.         if ((c = getc(infile)) != EOF)
  43.         {
  44.                 putc (c, outfile);
  45.         }
  46.         free(word);    
  47.     }  
  48.     if (!fclose (infile))
  49.     {
  50.         perror("Error. Unable to close file 1. \n");
  51.         exit(2);
  52.     }
  53.     if (!fclose (outfile))
  54.     {
  55.         perror("Error. Unable to close file 2. \n");
  56.         exit(2);
  57.     }
  58.     free(word);
  59.     return 0;
  60. }
  61.  
  62.  
  63.  
  64. int checkword (char * word)
  65. {
  66.     int i, len;
  67.     len = strlen (word);
  68.     for (i=0; i<len; i++)
  69.     {
  70.         if(word[i]=='e')
  71.         {
  72.             return 0;
  73.         }
  74.     }
  75.     return 1;
  76. }
  77.  
  78.  
  79.  
  80. char *getword(FILE *infile)
  81. {
  82.   char *word = NULL;
  83.   int length = 1, c, check = 0;
  84.   word = malloc(sizeof(int));
  85.   while ((c = getc(infile)) == '-' || isalpha(c))
  86.   {
  87.     if ((word = realloc (word, length * sizeof(int))) == NULL)
  88.     {
  89.       perror ("Error in malloc!\n");
  90.       free(word);
  91.       return NULL;
  92.     }
  93.     word[length - 1] = c;
  94.     length++;
  95.   }
  96.   ungetc (c, infile);
  97.   if (!check)
  98.   {
  99.     if ((word = realloc (word, length * sizeof(int))) == NULL)
  100.     {
  101.       perror ("Error in malloc!\n");
  102.       free(word);
  103.       return NULL;
  104.     }
  105.   }
  106.   word [length-1] = '\0';
  107.   return word;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement