#include #include #include #define MAXLINE 1024 typedef struct { char ** linea; int cantlineas; int numlinea0; } Ventana; char * GetLine( FILE * pf ); Ventana * creaVentana ( int cantlineas ); void destruyeVentana ( Ventana * v ); void pushVentana( Ventana * v, char * s ); void imprimeVentana( Ventana * v ); char * avanzaVentana( Ventana * v, FILE *pf ); int buscaVentana( FILE *pf, char *busco, Ventana *v ); int main() { Ventana * pv; int n=1, nlinea, count=0; char * archivo, *busco; FILE * pf; printf("Ingrese el numero de lineas extra (antes y despues)\n"); scanf("%d",&n); getchar(); /* para sacar el "@#!&*" \n del buffer de teclado */ if ( n < 0 || n > 10 ) n = 1; /* default de 3 líneas, evitando casos ridículos */ if( (pv = creaVentana(2*n+1)) == NULL ) { perror("Error: creando ventana\n"); return 1; } printf("Ingrese el nombre del archivo\n"); if( (archivo = GetLine(stdin)) == NULL || strlen(archivo)==0 ) { perror("Error: ingresando nombre de archivo"); destruyeVentana(pv); return 1; } if( (pf = fopen(archivo,"r")) == NULL ) { perror("Error: abriendo archivo para lectura"); free(archivo); destruyeVentana(pv); return 1; } printf("Ingrese el string a buscar\n"); if( (busco = GetLine(stdin)) == NULL || strlen(busco)==0 ) { perror("Error: ingresando nombre de archivo"); free(archivo); destruyeVentana(pv); return 1; } while((nlinea=buscaVentana(pf, busco, pv))>0) { printf("String \"%s\" encontrado en linea numero: %d\n",busco,nlinea); imprimeVentana(pv); count++; } if (count > 0) { printf("String \"%s\" fue encontrado %d veces\n",busco,count); } else { printf("String \"%s\" NO fue encontrado\n",busco); } free(busco); free(archivo); destruyeVentana(pv); return 0; } Ventana * creaVentana (int cantlineas) { Ventana * v; int i; if ((v = (Ventana *) malloc(sizeof(Ventana)))==NULL) return NULL; v->cantlineas = cantlineas; v->numlinea0 = - v->cantlineas +1; if ((v->linea = (char **) malloc((cantlineas)*sizeof(char*)))==NULL) { free(v); return NULL; } for (i=0; i< v->cantlineas; i++) v->linea[i] = NULL; return v; }