[C con Clase] Problema al leer lineas de fichero texto

Ariel Romero aromero en cenatav.co.cu
Vie Oct 19 18:45:39 CEST 2007


Ya encontré el error:

    char* getStringCadFromFile(char* fileName){
        FILE *fp1;
        char line[BUFSIZ];
        char *c, *stringCad;
       
        fp1 = fopen(fileName, "r");
        if (fp1 == NULL){
            perror("Error al abrir el archivo \n");
            exit (EXIT_FAILURE);
            }
       
        stringCad = malloc(sizeof(char*));
        stringCad[0]=0;
        do{           
            c = fgets(line, BUFSIZ, fp1); /* Obtiene una linea del 
archivo */
            if (c != NULL){
                stringCad = realloc(stringCad, 
((sizeof(char*))*(strlen(line)+strlen(stringCad)+1)));
                strcat(stringCad, line);               
                }
            } while (c != NULL); /* Se repite hasta encontrar NULL */
        fclose(fp1);   
        return stringCad;
        }

Así es como está bien. Tenía que inicializar la variable porque si no 
toma lo que había antes en ese lugar
Gracias a todos




Más información sobre la lista de distribución Cconclase