| Ver tema anterior :: Ver siguiente tema | | Autor | Mensaje |
|---|
Ninurta
Registrado: 04 Oct 2004 Mensajes: 13
| Publicado: 05/10/2004 4:10 am | | | Título: Imprimir lineas concretas de un fichero |
| Hola a todos: Estoy intentando escribir un programa que saque en pantalla solo las lineas de un archivo de texto que contienen una cadena determinada (en este caso "MGCP"). Hasta ahora he escrito esto que os muestro aqui pero no consigo que funcione como debe. ¿ Alguien puede decirme donde estan los errores ? ¿Puede hacerse aún mas simple? Nota: Utilizo GCC con MingWall bajo Windows2000
#include <stdio.h> #include <string.h>
main() {
FILE *fichero;
char buffer[180][80]; char *nextline, *where, *parametro[1000],*new_line; int total_lines; char type[]="MGCP"; char *type_1; type_1=type;
fichero= fopen("C:\\Trace1.txt","r");
total_lines=0;
while (nextline= fgets(buffer[total_lines],80,fichero)) { if (where = strchr(buffer[total_lines],'\n')) { *where='\0'; parametro[total_lines]= where+1;
if (new_line= strchr(parametro[total_lines],type)) { *new_line='\0'; total_lines+=1; } printf("%s",new_line);
} } fclose(fichero); } |
| | Volver arriba | |  | alp

Registrado: 12 Abr 2004 Mensajes: 413
| Publicado: 05/10/2004 9:18 am | | | Título: |
| Hola.
La función strchr() busca un carácter dentro de una cadena. Como lo que quieres es buscar una cadena, deberías utilizar la función strstr().
Un saludo. |
| | Volver arriba | |  | Ninurta
Registrado: 04 Oct 2004 Mensajes: 13
| Publicado: 06/10/2004 1:41 am | | | Título: |
| Hola de nuevo,
gracias por la solución, parece ser que ese era el problema y ahora funciona perfectamente. El programa final es así:
#include <stdio.h> #include <string.h>
main() {
FILE *fichero;
char buffer[180][80]; char *nextline, *where, *parametro[1000],*new_line; int total_lines; char type[]="MGCP";
fichero= fopen("C:\\Trace1.txt","r");
total_lines=0;
while (nextline= fgets(buffer[total_lines],80,fichero)) { if (new_line= strstr(buffer[total_lines],type)) { //*new_line='\0'; //total_lines+=1; printf("%s",new_line); }
if (where = strchr(buffer[total_lines],'\n')) { *where='\0'; parametro[total_lines]= where+1;
}
if (new_line= strchr(parametro[total_lines],'\n')) { *new_line='\0'; total_lines+=1; }
} fclose(fichero); }
Gracias por la ayuda y hasta otra |
| | Volver arriba | |  | | |
| No puede crear mensajes No puede responder temas No puede editar sus mensajes No puede borrar sus mensajes No puede votar en encuestas
|
|
| |