Advertisement
Guest User

grep

a guest
Nov 16th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.13 KB | None | 0 0
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include "cola.h"
  7.  
  8. void imprimir_cola(cola_t* cola){
  9.     char* linea;
  10.     while(!cola_esta_vacia(cola)){
  11.         linea = cola_desencolar(cola);
  12.         printf("%s",linea );
  13.     }
  14. }
  15.  
  16. int main(int argc,char* argv[]){
  17.  
  18.     if (argc<3){
  19.         fprintf(stderr, "Cantidad de parametros erronea\n");
  20.         return 0;
  21.     }
  22.     FILE* archivo;
  23.  
  24.     if(argc == 4)
  25.         archivo = fopen(argv[3],"r");
  26.     else
  27.         archivo = stdin;
  28.  
  29.  
  30.     size_t bufsize = 48;
  31.     char* aux;
  32.     char* buffer = malloc(bufsize*sizeof(char*));
  33.    
  34.     size_t cantidad_lineas;
  35.     sscanf(argv[2],"%zu",&cantidad_lineas);
  36.     char* cadena = argv[1];
  37.    
  38.     cola_t* cola = cola_crear();
  39.     size_t cont = 0;
  40.  
  41.     while(!feof(archivo)){                  
  42.         if (cont > cantidad_lineas)
  43.             cola_desencolar(cola);
  44.  
  45.         getline(&buffer,&bufsize,archivo);
  46.    
  47.         aux = strdup(buffer);
  48.         cola_encolar(cola,aux);
  49.  
  50.         if(strstr(aux,cadena)){
  51.         imprimir_cola(cola);
  52.             cont = 0;
  53.         }      
  54.         else
  55.             cont++;
  56.  
  57.      }
  58.  
  59.     if (argv[3])
  60.         fclose(archivo);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement