Stratos: Punto de Encuentro de Desarrolladores

¡Bienvenido a Stratos!

Acceder

Foros





1 ejercicio de ficheros en c. Ayuda fseek

Iniciado por Dmume, 12 de Abril de 2003, 04:57:51 PM

« anterior - próximo »

Dmume

                                hoLA

Haber si alguien me ayuda con este programa, me funciona bien excepto la funcion de modificar (el resto os lo he puesto x. si os ayuda) , tengo un cacao mental con el fseek haber si alguien me ayuda con esa funcion.
Gracias :D




#include
#include
#include
#include


typedef struct una{
char nom[20];
char tel[20];
}datos;


FILE *f;
datos reg;


void insertar(void);
void listar(void);
void borrar(void);
void main(void)
{

int op;


    do   {
       clrscr();
       printf("n1.Insertar:");
       printf("n2.listar:");
       printf("n3.borrar:");
       printf("n4.modificar:");
       printf("n4.exit:");
       scanf("%d",&op);

       switch(op){
             case 1: insertar();break;
             case 2: listar()  ;break;
             case 3: borrar()  ;break;
            }


     }
     while(op!=4);

getch();
}
/////////////////////////////////////
void insertar(void)
{
if(!(f=fopen("agenda.doc","a+b"))) { printf("Error al abrir el fichero:");
                getch();
                exit(1); }
   fflush(stdin);
   printf("nIntroduce nombre:");
   gets(reg.nom);
   printf("nIntroduce tel‚fono:");
   gets(reg.tel);
   fwrite(®,sizeof(reg),1,f);


 fclose(f);
 getch();
}
/////////////////////////////////////
void listar (void)
{


clrscr();

if(!(f=fopen("agenda.doc","r+b")))
   {
   puts("n Error al abrir el fichero");
   exit(1);
   }
fread(®,sizeof(reg),1,f);


while(!(feof(f)))
   {

   printf("nn%s", reg.nom);
   printf("n %s", reg.tel);
   fread(®,sizeof(reg),1,f);
   }
fclose(f) ;
getch();
}

/////////////////////////////////////
void modificar(void)  :enfadado: AQUI TENGO EL CACAO MENTAL :X9:
{

int op2,c=0;
char buscax[20],newtel[20];

printf("nA quien quieres modificar:?");
gets(buscax);


if (!(f=fopen("agenda.doc","r+b")))  {

                 printf("Error al abrir el fichero");
                 getch();
                 exit(1);
                 }

fread(®,sizeof(reg),1,f);
while ((strcmp(buscax,reg.nom)!=0)&&(!(feof(f))))

     {
   c++;
   fread(®,sizeof(reg),1,f);
     }


      if (strcmp(buscax,reg.nom)==0) {   printf("n%s",®.nom);
               printf("n%s",®.tel); }

   printf("nIntroduce el nuevo n£mero de tel‚fono:");
   gets(newtel);
   fseek(f,sizeof(reg)*(c-1),1);
   fwrite(®,sizeof(reg),1,f);






fclose(f);
getch();
}
///////////////////////////////////
void borrar(void)
{
remove("agenda.doc");
printf("AGENDA BORRADA");
f=fopen("agenda.doc","a+b");
getch();

}                                

nostromo

                                Holas,

me parece que tienes que poner un SEEK_SET    :

fseek(f,sizeof(reg)*(c),SEEK_SET);

lo digo porque tu le pasas en ese parametro un 1 y debe ser un 0.
Un 1 seria SEEK_CUR que significaria suma al OFFSET la posicion ACTUAL del fichero. Un 0(SEEK_SET) es sumar a partir de la posicion de inicio del fichero.

Tambien te he cambiado el (c-1) por un c

Espero que te sirva.

Un saludo.
Nostromo