#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char nombre[100];
int telefono;
}datos;
void main()
{
int posicion;
char op;
FILE *pf;
datos ficha;
pf=fopen("c:\\archivo.txt","r+");//abrir el fichero para leer y escribir
if(pf==NULL)
{
printf("\n\aError abriendo el archivo.");
getchar();
return;
}
fseek(pf,0L,SEEK_END);//lleva el apuntador del fichero al final
printf("\nTotal de registros: %d",(int)ftell(pf)/sizeof(ficha));//Muestra el nº total de registros en el fichero
printf("\nRegistro a visualizar (1-%d): ",(int)ftell(pf)/sizeof(ficha));
scanf("%d",&posicion);
rewind(pf);//corre el puntero del fichero al inicio no sería necesario...Para que se vean todas las fuciones
fseek(pf,sizeof(ficha)*(posicion-1),SEEK_SET);//se mueve el puntero hasta la posición que se ha pedido
fread(&ficha,sizeof(ficha),1,pf);
printf("\n\n\tNombre: %s\n\tTelefono: %d",ficha.nombre,ficha.telefono);
fflush(stdin);
do
{
printf("\n\n\tDesea cambiar el registro? (s-n): ");
scanf("%c",&op);
fflush(stdin);
op=tolower(op);
}while(op!='s' && op!='n');
if(op=='s')
{
printf("\n\n\tIntroduce nuevo nombre: ");
gets(ficha.nombre);
printf("\n\tTelefono: ");
scanf("%d",&ficha.telefono);
fseek(pf,-(signed)sizeof(ficha),SEEK_CUR);//retroceder un registro a partir del que estamos pq al hacer fwrite nos hemos movido una posición hacia delante.
fwrite(&ficha,sizeof(ficha),1,pf);
}
fclose(pf);
}