C/Visual C - ARCHIVOS SECUENCIALES INDEXADOS

 
Vista:

ARCHIVOS SECUENCIALES INDEXADOS

Publicado por BULE BU (46 intervenciones) el 19/03/2006 03:33:33
tengo esta estructura:

estoy creando un archivo secuencial indexado, me podrian decir si voy bien..?

//ARCHIVOS SECUENCIALES INDEXADOS

#include<stdio.h>
#include<ctype.h>
#include<io.h>
#include<dos.h>
#include<string.h>
#include<conio.h>

struct reg
{
int pos,edo;
char nom[15],email[15],cel[15];
}
agenda;

void crear();
void altas();
void mostrar();
int file_exists(char *filename);
int abre_archivo(char *nombre,char *tipo);
void centra(int fila,char *texto);

void main()
{
char tecla;
do
{
clrscr();
centra(14,"Agenda Telefonica");
gotoxy(20,17);printf("Crear.................................1");
gotoxy(20,19);printf("Altas.................................2");
gotoxy(20,21);printf("Bajas.................................3");
gotoxy(20,23);printf("Modificar.............................4");
gotoxy(20,25);printf("Consultas.............................5");
gotoxy(20,27);printf("Listado...............................6");
gotoxy(20,29);printf("Salir.................................7");
gotoxy(20,32);printf("Opcion...............................[ ]\b\b");
tecla=getche();
switch(tecla)
{
case '1': crear();break;
case '2': altas();break;
case '6': mostrar();break;
}
}
while(tecla!='7');
}

//CENTRA

void centra(int fila,char *texto)
{
gotoxy((80-strlen(texto))/2,fila);printf("%s",texto);
}

//CREA ARCHIVO

void crear()
{
char op;
FILE *archivo;
clrscr();
if(file_exists("C:\\AGENDA_T.txt")==1)//si existe
{
centra(21,"EL ARCHIVO YA EXISTE");
gotoxy(18,24);printf("Abrir y escribir (r+t).........................S");
gotoxy(18,26);printf("Borrar Contenido y volver a escribir (w+t).....N");
gotoxy(18,28);printf("Exit...........................................E");
gotoxy(18,31);printf("Elige Opcion..................................[ ]\b\b");
op=getche();
if(toupper(op)=='S') //abre para escribir
{
if(abre_archivo("C:\\AGENDA_T.txt","r+t")==1)
{
centra(40,"EL ARCHIVO SE ABRIO PARA ESCRIBIR");
getch();
return;
}
}
if(toupper(op)=='N')
{ //borra contenido y escribe
if(abre_archivo("C:\\AGENDA_T.txt","w+t")==1)
{
centra(40,"SE DESTRUYO EL CONTENIDO PARA ESCRIBIR");
getch();
return;
}
}
if(toupper(op)=='E')
{
centra(40,"ACABAS DE CANCELAR");
getch();
return;
}
} //fin if
else
{
if(abre_archivo("C:\\AGENDA_T.txt","w+t")==1)
{
centra(35,"SE CREO DE MODO w+t");
getch();
return;
}
else
{
centra(24,"SE ACABA DE CREAR EL ARCHIVO");
getch();
return;
}
}
fclose(archivo);
delay(500);
}
//ALTAS
void altas()
{
int band=0,p;
clrscr();
FILE *archivo;
archivo=fopen("C:\\AGENDA_T.txt","r+t");
if(archivo==NULL)
{
centra(24,"NO SE PUDO LEER PARA ESCRIBIR (r+t)");
}
else
{
//SOLICITA ID DE REGISTRO
do
{
clrscr();
centra(4,"AGENDA TELEFONICA");
gotoxy(3,8);printf("Introduce numero de registro: ");
scanf("%d",&p);
}
while(p<=0 || p>100);
do
{
fread(&agenda,sizeof(agenda),1,archivo);
if(agenda.edo==1 && !feof(archivo))
{
if(agenda.pos==p)
{
band=1;
centra(24,"Ya existe ese registro");
break;
}
}
}
while(!feof(archivo));
if(!band)
{
agenda.pos=p;
gotoxy(3,10); printf("Nombre: ");
scanf("%s",&agenda.nom);
gotoxy(3,12); printf("Email: ");
scanf("%s",&agenda.email);
gotoxy(3,14);printf("Celular: ");
scanf("%s",&agenda.cel);
agenda.edo=1;
fseek(archivo,(p-1)*sizeof(agenda),SEEK_SET);
fwrite(&agenda,sizeof(agenda),1,archivo);
centra(24,"Acabas de ingresar nuevo numero");
}
}
fclose(archivo);
getch();
}

//MOSTRAR DATOS

void mostrar()
{
int band=0,y=10,x=0;
clrscr();
FILE *archivo;
archivo=fopen("C:\\AGENDA_T.txt","rt");
if(archivo==NULL)
{
centra(24,"NO SE ENCONTRO EL ARCHIVO DE LECTURA");
}
else
{
centra(4,"LISTADO");
do
{
fread(&agenda,sizeof(agenda),1,archivo);
if(agenda.edo==1 && !feof(archivo))
{
band=1;
gotoxy(3,7);printf("Registro");
gotoxy(20,7);printf("Nombre");
gotoxy(32,7);printf("Email");
gotoxy(60,7);printf("Celular");
gotoxy(5,y);printf("%d",x);
gotoxy(20,y);printf("%s",agenda.nom);
gotoxy(32,y);printf("%s",agenda.email);
gotoxy(60,y);printf("%s",agenda.cel);
y+=2;
// x++;
}
x++;
}
while(!feof(archivo));
if(!band)
{
centra(24,"NO SE ENCONTRO NIONGUN DATO");
}
}
fclose(archivo);
getch();
}

//EXISTE

int file_exists(char *filename)
{
int existe;
if(access(filename,0)==0)
{
existe=1;
}
return(existe);
}
//ABRE ARCHIVO

int abre_archivo(char *nombre,char *tipo)
{
int valor=0; //si es cero no hay error
FILE *archivo;
archivo=fopen(nombre,tipo);
if(archivo==NULL)
{
valor=1;
centra(24,"ERROR DE ESCRITURA");
}
return valor;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder