fgets () returns NULL when reading text file

4

When I try to read my FILE fpTextFixed ( fgets(line,sizeof(line),fpTextFixed) ), it returned me NULL , but it was corroborating and my file was opened and written correctly. What I'm trying to do is write a binary file from reading a fixed-length text file.

File main.c

/**main.c**/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "protFileTxt.h"


int main()
{
FILE *fpData,
     *fpDataFixed,
     *fpDataVariable,
     *fpTextFixed,
     *fpTextVariable;
student reg;
char line[LINE],
     *aux;

printf("\t\t\t\t Test File txt!\n");

if(!openFile(&fpData,FileStudent,"r+b",!CON_SIN_MSJ))
{
    createFileEstudent();
    if(!openFile(&fpData,FileStudent,"r+b",CON_SIN_MSJ))
        return 0;
}
if(!openFile(&fpTextFixed,FILE_TEXT_FIXED,"r+t",!CON_SIN_MSJ))
{
    if(!openFile(&fpTextFixed,FILE_TEXT_FIXED,"w+t",CON_SIN_MSJ))
    {
        fclose(fpData);
        return 0;
    }
}
if(!openFile(&fpTextVariable,FILE_TEXT_VARIABLE,"r+t",!CON_SIN_MSJ))
{
    if(!openFile(&fpTextVariable,FILE_TEXT_VARIABLE,"w+t",CON_SIN_MSJ))
    {
        fclose(fpData);
        fclose(fpTextFixed);
        return 0;
    }
}

fread(&reg,1,sizeof(reg),fpData);
while(!feof(fpData))
{
    //Preguntar por %-*s
    fprintf(fpTextFixed,"%08ld%-51s%-51s%-7.2f\n",reg.dni,reg.name,reg.surName,reg.average);

    fprintf(fpTextVariable,"%ld|%-s|%-s|%.2f\n",reg.dni,reg.name,reg.surName,reg.average);

    fread(&reg,1,sizeof(reg),fpData);
}
if(!openFile(&fpDataFixed,FileStudentFixed,"r+b",!CON_SIN_MSJ))
{
    if(!openFile(&fpDataFixed,FileStudentFixed,"w+b",CON_SIN_MSJ))
    {
        fclose(fpData);
        fclose(fpTextFixed);
        fclose(fpTextVariable);
        return 0;
    }
}
if(!openFile(&fpDataVariable,FileStudentVariable,"r+b",!CON_SIN_MSJ))
{
    if(!openFile(&fpDataVariable,FileStudentVariable,"w+b",CON_SIN_MSJ))
    {
        fclose(fpData);
        fclose(fpTextFixed);
        fclose(fpTextVariable);
        fclose(fpDataFixed);
        return 0;
    }
}
while(fgets(line,sizeof(line),fpTextFixed))
{
    if(!(aux = strchr(line,'\n')))
    {
        fprintf(stderr,"Error - No se pudo procesar archivo %s", FILE_TEXT_FIXED);
        fclose(fpData);
        fclose(fpTextFixed);
        fclose(fpTextVariable);
        fclose(fpDataFixed);
        fclose(fpDataVariable);
        return 0;
    }
    *aux = '
/**funFileTxt.c**/
#include <stdio.h>
#include "protFileTxt.h"

void createFileEstudent()
{
FILE *fp;
student reg[] = {{87143658L, "Allende","Miguel",7.50},
             {30589425L,"Brito","Claudio",8.00},
             {46558892L,"Cena","Marcelo",8.90},
             {79815612L,"Dominguez","Diego",6.89},
             {46868278L,"Franzoi","Maximiliano",4.36},
             {32589614L,"Herrera","Cristobal",7.50},
             {47896425L,"Merlo","Micaela",8.55},
             {98713469L,"Oviedo","Cesar",6.00},
             {16887451L,"Pagnutti","Jose",8.98},
             {91649487L,"Perez","Leandro",7.39},
             {81659417L,"Recalde","Jose",7.28},
             {87952123L,"Recalde","Camila",5.56},
             {32139745L,"Rodriguez","Fabiana",7.00},
             {97962146L,"Romero","Johana",8.78},
             {34562501L,"Solla","Tamara",7.29},
             {16549654L,"Sosa","Federico",4.89},
             {66547893L,"Spinelli","Ezequiel",6.78},
             {21656154L,"Tapia","Jorge",8.56},
             {54514964L,"Torres","Mario",7.89},
             {22366985L,"Vizzoni","Daniela",6.56}};

fp = fopen(FileStudent,"wb");
if(fp)
{
    fwrite(reg,1,sizeof(reg),fp);
}
fclose(fp);
}


int openFile(FILE **file,const char *nameFile,const char *mode,int CON_SIN)
{
*file = fopen(nameFile,mode);

if(!*file)
{
    if(CON_SIN)
        fprintf(stderr,"Error - No se pudo abrir el archivo %s en modo %s",nameFile,mode);
    return 0;
}
return 1;
}
'; //TODO: funcion para verificar los campos de los registros leidos por fgets /** promedio alumno **/ aux -= AVERAGE; sscanf(aux,"%f",&reg.average); *aux = '
/**protFileTxt.h**/
#define FileStudent "Data"
#define FileStudentFixed "DataFixed"
#define FileStudentVariable "DataVariable"
#define FILE_TEXT_FIXED "txtFixed"
#define FILE_TEXT_VARIABLE "txtVariable"
#define CON_SIN_MSJ 1
#define AVERAGE 9
#define SURNAME 51
#define NAME 51
#define DNI 8
#define LINE AVERAGE + SURNAME + NAME + DNI

typedef struct
{
long dni;
char name[51],
     surName[51];
float average;
}student;

int openFile(FILE **file,const char *nameFile,const char *mode,int CON_SIN);
void createFileEstudent();
'; /** apellido alumno **/ aux -= SURNAME; sscanf(aux,"%s",&reg.surName); *aux = '
/**main.c**/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "protFileTxt.h"


int main()
{
FILE *fpData,
     *fpDataFixed,
     *fpDataVariable,
     *fpTextFixed,
     *fpTextVariable;
student reg;
char line[LINE],
     *aux;

printf("\t\t\t\t Test File txt!\n");

if(!openFile(&fpData,FileStudent,"r+b",!CON_SIN_MSJ))
{
    createFileEstudent();
    if(!openFile(&fpData,FileStudent,"r+b",CON_SIN_MSJ))
        return 0;
}
if(!openFile(&fpTextFixed,FILE_TEXT_FIXED,"r+t",!CON_SIN_MSJ))
{
    if(!openFile(&fpTextFixed,FILE_TEXT_FIXED,"w+t",CON_SIN_MSJ))
    {
        fclose(fpData);
        return 0;
    }
}
if(!openFile(&fpTextVariable,FILE_TEXT_VARIABLE,"r+t",!CON_SIN_MSJ))
{
    if(!openFile(&fpTextVariable,FILE_TEXT_VARIABLE,"w+t",CON_SIN_MSJ))
    {
        fclose(fpData);
        fclose(fpTextFixed);
        return 0;
    }
}

fread(&reg,1,sizeof(reg),fpData);
while(!feof(fpData))
{
    //Preguntar por %-*s
    fprintf(fpTextFixed,"%08ld%-51s%-51s%-7.2f\n",reg.dni,reg.name,reg.surName,reg.average);

    fprintf(fpTextVariable,"%ld|%-s|%-s|%.2f\n",reg.dni,reg.name,reg.surName,reg.average);

    fread(&reg,1,sizeof(reg),fpData);
}
if(!openFile(&fpDataFixed,FileStudentFixed,"r+b",!CON_SIN_MSJ))
{
    if(!openFile(&fpDataFixed,FileStudentFixed,"w+b",CON_SIN_MSJ))
    {
        fclose(fpData);
        fclose(fpTextFixed);
        fclose(fpTextVariable);
        return 0;
    }
}
if(!openFile(&fpDataVariable,FileStudentVariable,"r+b",!CON_SIN_MSJ))
{
    if(!openFile(&fpDataVariable,FileStudentVariable,"w+b",CON_SIN_MSJ))
    {
        fclose(fpData);
        fclose(fpTextFixed);
        fclose(fpTextVariable);
        fclose(fpDataFixed);
        return 0;
    }
}
while(fgets(line,sizeof(line),fpTextFixed))
{
    if(!(aux = strchr(line,'\n')))
    {
        fprintf(stderr,"Error - No se pudo procesar archivo %s", FILE_TEXT_FIXED);
        fclose(fpData);
        fclose(fpTextFixed);
        fclose(fpTextVariable);
        fclose(fpDataFixed);
        fclose(fpDataVariable);
        return 0;
    }
    *aux = '
/**funFileTxt.c**/
#include <stdio.h>
#include "protFileTxt.h"

void createFileEstudent()
{
FILE *fp;
student reg[] = {{87143658L, "Allende","Miguel",7.50},
             {30589425L,"Brito","Claudio",8.00},
             {46558892L,"Cena","Marcelo",8.90},
             {79815612L,"Dominguez","Diego",6.89},
             {46868278L,"Franzoi","Maximiliano",4.36},
             {32589614L,"Herrera","Cristobal",7.50},
             {47896425L,"Merlo","Micaela",8.55},
             {98713469L,"Oviedo","Cesar",6.00},
             {16887451L,"Pagnutti","Jose",8.98},
             {91649487L,"Perez","Leandro",7.39},
             {81659417L,"Recalde","Jose",7.28},
             {87952123L,"Recalde","Camila",5.56},
             {32139745L,"Rodriguez","Fabiana",7.00},
             {97962146L,"Romero","Johana",8.78},
             {34562501L,"Solla","Tamara",7.29},
             {16549654L,"Sosa","Federico",4.89},
             {66547893L,"Spinelli","Ezequiel",6.78},
             {21656154L,"Tapia","Jorge",8.56},
             {54514964L,"Torres","Mario",7.89},
             {22366985L,"Vizzoni","Daniela",6.56}};

fp = fopen(FileStudent,"wb");
if(fp)
{
    fwrite(reg,1,sizeof(reg),fp);
}
fclose(fp);
}


int openFile(FILE **file,const char *nameFile,const char *mode,int CON_SIN)
{
*file = fopen(nameFile,mode);

if(!*file)
{
    if(CON_SIN)
        fprintf(stderr,"Error - No se pudo abrir el archivo %s en modo %s",nameFile,mode);
    return 0;
}
return 1;
}
'; //TODO: funcion para verificar los campos de los registros leidos por fgets /** promedio alumno **/ aux -= AVERAGE; sscanf(aux,"%f",&reg.average); *aux = '
/**protFileTxt.h**/
#define FileStudent "Data"
#define FileStudentFixed "DataFixed"
#define FileStudentVariable "DataVariable"
#define FILE_TEXT_FIXED "txtFixed"
#define FILE_TEXT_VARIABLE "txtVariable"
#define CON_SIN_MSJ 1
#define AVERAGE 9
#define SURNAME 51
#define NAME 51
#define DNI 8
#define LINE AVERAGE + SURNAME + NAME + DNI

typedef struct
{
long dni;
char name[51],
     surName[51];
float average;
}student;

int openFile(FILE **file,const char *nameFile,const char *mode,int CON_SIN);
void createFileEstudent();
'; /** apellido alumno **/ aux -= SURNAME; sscanf(aux,"%s",&reg.surName); *aux = '%pre%'; /** nombre alumno **/ aux -= NAME; sscanf(aux,"%s",&reg.name); *aux = '%pre%'; /** dni alumno **/ aux -= DNI; sscanf(aux,"%ld",&reg.dni); *aux = '%pre%'; fwrite(line,1,sizeof(line),fpDataFixed); } fclose(fpData); fclose(fpTextFixed); fclose(fpTextVariable); fclose(fpDataFixed); fclose(fpDataVariable); return 0; }
'; /** nombre alumno **/ aux -= NAME; sscanf(aux,"%s",&reg.name); *aux = '%pre%'; /** dni alumno **/ aux -= DNI; sscanf(aux,"%ld",&reg.dni); *aux = '%pre%'; fwrite(line,1,sizeof(line),fpDataFixed); } fclose(fpData); fclose(fpTextFixed); fclose(fpTextVariable); fclose(fpDataFixed); fclose(fpDataVariable); return 0; }

File funFileTxt.c

%pre%

File protFileTxt.h

%pre%     
asked by Jorge Gonzalez 23.09.2016 в 03:00
source

2 answers

3

When you make fgets the position indicator of the file is at the end. To return it to the beginning, use rewind

In this line the file is opened using "r+t" .

if(!openFile(&fpTextFixed,FILE_TEXT_FIXED,"r+t",!CON_SIN_MSJ))

In this line you write at the end of the file:

//Preguntar por %-*s
fprintf(fpTextFixed,"%08ld%-51s%-51s%-7.2f\n",reg.dni,reg.name,reg.surName,reg.average);

And when you get to fgets the position of the file is at the end, so it returns NULL. If you use rewind you will start reading from the beginning, which will read what you just wrote.

rewind( fpTextFixed );
while(fgets(line,sizeof(line),fpTextFixed))

An alternative solution would be to close the file after writing and open it before reading.

    
answered by 23.09.2016 / 08:38
source
3

The fpTextFixed file was recorded with:

while(!feof(fpData))
{
    //Preguntar por %-*s
    fprintf(fpTextFixed,"%08ld%-51s%-51s%-7.2f\n",reg.dni,reg.name,reg.surName,reg.average);

    fprintf(fpTextVariable,"%ld|%-s|%-s|%.2f\n",reg.dni,reg.name,reg.surName,reg.average);

    fread(&reg,1,sizeof(reg),fpData);
}

Therefore, the cursor is at the end of the file, and if you try to read with fgets() , you will return NULL . To prevent this from happening, you must rewind before reading:

rewind(fpTextFixed);

With the function rewind() we tell c to send the cursor to the beginning of the file, to be able to read the data:

while(fgets(line,sizeof(line),fpTextFixed))
{
    // ...
}
    
answered by 23.09.2016 в 08:38