Cuanto intento hacer un ftell de mi puntero a FILE ´em´ (empleados) en la funcion ´createTablekeyEmployee(fpEmployee,tableKey);´se crea un error de segmentacion, pero eh revisado el codigo y al parecerse en las funciones de creacion y apertura de los FILE se estan creando bien.
#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(®Stud,1,sizeof(regStud),fpStudent);
while(!feof(fpStudent))
{
if(regStud.average >= 7)
updateEmployee(searchKey(regStud.dni,tableKey),fpEmployee);
fread(®Stud,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(®,1,sizeof(reg),em);
while(!feof(em) && vec)
{
*vec = reg.dni;
vec ++;
fread(®,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(®,1,sizeof(reg),em);
reg.salary *= 1.728;
fseek(em,key * (long)sizeof(employee),SEEK_SET);
fwrite(®,1,sizeof(reg),em);
fseek(em,0L,SEEK_SET);
}
void showFileEmployee(FILE *em)
{
employee reg;
long actualPos = ftell(em);
fread(®,1,sizeof(reg),em);
showEmployee(NULL);
while(!feof(em))
{
showEmployee(®);
fread(®,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(®,1,sizeof(reg),st);
showStudent(NULL);
while(!feof(st))
{
showStudent(®);
fread(®,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);