Segmentation fault FILE

1

When I try to make a ftell from my pointer to FILE 'em' (employees) in the function 'createTablekeyEmployee (fpEmployee, tableKey);' a segmentation error is created, but the code has been revised and it appears in the functions of Creation and opening of the FILE are creating well.

#include <stdio.h>
#include <stdlib.h>
#include "updateFilePrototipe.h"

int main()
{
FILE *fpEmployee,
     *fpStudent;
employee regEm;
student regStud;
long tableKey[20];

printf("Test Update File\n");

if(!openFile(fpStudent,FileStudent,"r+b",!CON_SIM_MSJ))
{
    createFileEstudent();
    if(!openFile(fpStudent,FileStudent,"r+b",CON_SIM_MSJ))
        return 1;
}
if(!openFile(fpEmployee,FileEmployee,"r+b",!CON_SIM_MSJ))
{
    createFileEmployee();
    if(!openFile(fpEmployee,FileEmployee,"r+b",CON_SIM_MSJ))
    {
       fclose(fpStudent);
       return 1;
    }
}

createTablekeyEmployee(fpEmployee,tableKey);
showFileStudent(fpStudent);
showFileEmployee(fpEmployee);

fread(&regStud,1,sizeof(regStud),fpStudent);
while(!feof(fpStudent))
{
    if(regStud.average >= 7)
        updateEmployee(searchKey(regStud.dni,tableKey),fpEmployee);

    fread(&regStud,1,sizeof(regStud),fpStudent);
}

showFileStudent(fpStudent);
showFileEmployee(fpEmployee);

fclose(fpStudent);
fclose(fpEmployee);
return 0;
}

#include <stdio.h>
#include "updateFilePrototipe.h"

void createFileEmployee()
{
FILE *fp;
employee reg[] = {{87143658L, "Allende","Miguel",7.5},
             {30589425L,"Brito","Claudio",8},
             {46558892L,"Cena","Marcelo",8.9},
             {79815612L,"Dominguez","Diego",6.89},
             {46868278L,"Franzoi","Maximiliano",4.36},
             {32589614L,"Herrera","Cristobal",7.5},
             {47896425L,"Merlo","Micaela",8.55},
             {98713469L,"Oviedo","Cesar",6},
             {16887451L,"Pagnutti","Jose",8.98},
             {91649487L,"Perez","Leandro",7.39},
             {81659417L,"Recalde","Jose",7.28},
             {87952123L,"Recalde","Camila",5.56},
             {32139745L,"Rodriguez","Fabiana",7},
             {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(FileEmployee,"wb");
if(fp)
{
    fwrite(reg,1,sizeof(reg),fp);
}
fclose(fp);
}

void createFileEstudent()
{
FILE *fp;
student reg[] = {{87143658L, "Allende","Miguel",7000},
             {30589425L,"Brito","Claudio",8000},
             {46558892L,"Cena","Marcelo",9005},
             {79815612L,"Dominguez","Diego",6000},
             {46868278L,"Franzoi","Maximiliano",4500},
             {32589614L,"Herrera","Cristobal",7500},
             {47896425L,"Merlo","Micaela",8055},
             {98713469L,"Oviedo","Cesar",6750},
             {16887451L,"Pagnutti","Jose",8980},
             {91649487L,"Perez","Leandro",7039},
             {81659417L,"Recalde","Jose",7828},
             {87952123L,"Recalde","Camila",5556},
             {32139745L,"Rodriguez","Fabiana",7236},
             {97962146L,"Romero","Johana",8356},
             {34562501L,"Solla","Tamara",7429},
             {16549654L,"Sosa","Federico",4089},
             {66547893L,"Spinelli","Ezequiel",6678},
             {21656154L,"Tapia","Jorge",8756},
             {54514964L,"Torres","Mario",7489},
             {22366985L,"Vizzoni","Daniela",6456}};

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

int openFile(FILE *fp,const char *name,const char *mode,int CON_SIN)
{
fp = fopen(name,mode);

if(!fp)
{
    if(CON_SIN)
        fprintf(stderr,"No se pudo abrir %s, en modo %s",name,mode);
    return 0;
}
return 1;
}

void createTablekeyEmployee(FILE *em,long *vec)
{
employee reg;
long actualPos = ftell(em);

fread(&reg,1,sizeof(reg),em);
while(!feof(em) && vec)
{
    *vec = reg.dni;
    vec ++;
    fread(&reg,1,sizeof(reg),em);
}
fseek(em,actualPos,SEEK_SET);
}

int searchKey(const long key, long vec[])
{
int i = 0;
while(vec[i] != key && i < TAM_TABLE)
{
    i ++;
}
return vec ? i : -1;
}

void updateEmployee(const int key,FILE *em)
{
employee reg;

fseek(em,key * (long)sizeof(employee),SEEK_SET);
fread(&reg,1,sizeof(reg),em);
reg.salary *= 1.728;
fseek(em,key * (long)sizeof(employee),SEEK_SET);
fwrite(&reg,1,sizeof(reg),em);
fseek(em,0L,SEEK_SET);
}

void showFileEmployee(FILE *em)
{
employee reg;
long actualPos = ftell(em);

fread(&reg,1,sizeof(reg),em);
showEmployee(NULL);
while(!feof(em))
{
   showEmployee(&reg);
   fread(&reg,1,sizeof(reg),em);
}

fseek(em,actualPos,SEEK_SET);
}

void showEmployee(employee *d)
{
if(!d)
    printf("DNI        NOMBRE          APELLIDO       SUELDO");
else
    printf("%ld        %s               %s            %lf",d->dni,d->name,d-        >surName,d->salary);
}

void showFileStudent(FILE *st)
{
 student reg;
 long actualPos = ftell(st);

fread(&reg,1,sizeof(reg),st);
showStudent(NULL);
while(!feof(st))
{
   showStudent(&reg);
   fread(&reg,1,sizeof(reg),st);
}

fseek(st,actualPos,SEEK_SET);
}

void showStudent(student *d)
{
if(!d)
    printf("DNI        NOMBRE          APELLIDO       PROMEDIO");
else
    printf("%ld        %s               %s            %f",d->dni,d->name,d->surName,d->average);
}




#define FileEmployee "employee"
#define FileStudent  "student"
#define CON_SIM_MSJ 1
#define TAM_TABLE 20
typedef struct
{
long dni;
char name[51],
     surName[51];
double salary;
}employee;

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

void createFileEmployee();
void createFileEstudent();
int openFile(FILE *fp,const char *name,const char *mode,int CON_SIN);
void createTablekeyEmployee(FILE *em,long *vec);
int searchKey(const long key, long vec[]);
void updateEmployee(const int key,FILE *em);
void showFileEmployee(FILE *em);
void showEmployee(employee *d);
void showFileStudent(FILE *st);
void showStudent(student *d);
    
asked by Jorge Gonzalez 18.09.2016 в 15:26
source

1 answer

4

We will see this problem without using pointers. What value does the following example print?

void func(int valor)
{ valor = 3; }

int main()
{
  int var = 0;
  func(var);

  printf("%d",var);
}

The correct answer is 0, since the changes made within func are purely local. If we now introduce pointers ...

void func1(int* valor)
{ valor = 3; }

void func2(int* valor)
{ *valor = 3; }

int main()
{
  int var = 0;
  func1(&var);
  printf("%d",var);

  func2(&var);
  printf("%d",var);
}

The result will be 03. This is because func makes changes on the pointer itself and that does not stop being local changes, while func2 makes changes on the targeted memory, changes that are going to be reflected outside of the function. I do not know if the difference is appreciated.

In your case the problem is the following:

int openFile(FILE *fp,const char *name,const char *mode,int CON_SIN)
{
  fp = fopen(name,mode);

  if(!fp)
  {
    if(CON_SIN)
        fprintf(stderr,"No se pudo abrir %s, en modo %s",name,mode);
    return 0;
  }
  return 1;
}

fp is a local variable, then all the changes you make on that variable are implicitly local. And unfortunately changing the memory address pointed to by the pointer is a local change that will have no effect outside of the function.

You have two possible solutions to the problem:

  • Return fp with the return, so that if you return 0 it is assumed that the file could not be opened.

  • Pass fp as an argument in the form of a double pointer. In this way the direction changes of the simple pointer will be reflected outside the function.

Return fp

FILE* openFile(const char *name,const char *mode,int CON_SIN)
{
  FILE* fp = fopen(name,mode);

  if(!fp)
  {
    if(CON_SIN)
        fprintf(stderr,"No se pudo abrir %s, en modo %s",name,mode);
  }

  return fp;
}

double pointer

int openFile(FILE **fp,const char *name,const char *mode,int CON_SIN)
{
  *fp = fopen(name,mode);

  if(!*fp)
  {
    if(CON_SIN)
        fprintf(stderr,"No se pudo abrir %s, en modo %s",name,mode);
    return 0;
  }
  return 1;
}

Greetings.

    
answered by 19.09.2016 / 09:00
source