necesito ayuda con un programa
Publicado por ana (1 intervención) el 26/10/2006 03:32:17
En Dev c++ es sobre archivos no me sale la funcion eliminar solo elimina codigos cortos no se como hacer para que elimine codigos largos
//bibliotecas
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <conio.h>
#include <stdio.h>
#include <io.h>
using namespace std;
//clase
class Datospers
{
public:
//funciones y variables
int fd;//descriptor
Datospers();//declaracion de funciones
void agregar();
void mostrar();
void buscar();
void modificar();
void eliminar();
};
Datospers::Datospers()
{
fd=open("F:\\Prueba.txt",2);//abriendo el archivo
if(fd<0)
{
printf("\n Error al abrir archivo \t");
getch();
}
}
//funciones
void Datospers::agregar()
{
char cod[9],nom[20],edad[2],car[15];
int p,cont=0,opc;
// printf("hjjh");
//system ("PAUSE");
lseek(fd,0L,2);
do
{
fflush(stdin);
printf(" \n Introduce Codigo:");
gets(cod);
write(fd,cod,strlen(cod));
write(fd,"|",1);
printf("\n Introduce Nombre:");
gets(nom);
write(fd,nom,strlen(nom));
write(fd,"|",1);
printf("\n Introduce Edad: ");
gets(edad);
write(fd,edad,strlen(edad));
write(fd,"|",1);
printf("\n Introduce Sexo: ");
gets(car);
write(fd,car,strlen(car));
write(fd,"\n",1);
printf("\n ¿Deseas agregar otro?\n1.Si\n2.No ");
fflush(stdin);
scanf("%d",&opc);
}while(opc!=2);
}
void Datospers::mostrar()
{
char c,a;
lseek(fd,0L,0);
while(read(fd,&c,1)>0)
{
if(c=='&'){
while((read(fd,&c,1)>0)&&(c!='\n')) a='d';
}
if(c!='|')
write(1,&c,1);
else
write(1," ",1);
}
getch();
}
void Datospers::buscar()
{
char cad[20],c,cad2[20];
int pos=0,band=0,cod=0;
printf("¿Cual es el codigo a buscar:\n");
fflush(stdin);
gets(cad2);
cad[0]='\0';
lseek(fd,0L,0);
while((read(fd,&c,1)>0)&&(band<1))
{
if((c!='|')&&(cod<1))
{
cad[pos]=c;
pos++;
}
else
{
cad[pos]='\0';
cod++;
if(strcmpi(cad2,cad)==0)
{
while((read(fd,&c,1)>0)&&(c!='\n'))
{
if(c!='|')
write(1,&c,1);
else
write(1,"\t",1);
}
band++;
}
else
{
while((read(fd,&c,1)>0)&&(cod>0))
if(c=='\n')
{
cod=0;
pos=0;
cad[0]='\0';
break;
}
}
lseek(fd,0L,1);
}
}
getch();
}
void Datospers::eliminar()
{
char c, cod[10], codU[10];
int pos=0, band=0, cd=0, cR=0;
long val, posR[50];
printf("Codigo a eliminar:\n");
fflush(stdin);
gets(codU);
lseek(fd,0L,0);
while((read(fd,&c,1)>0) && (band<1))
{
val=lseek(fd,0L,1);
if(c=='\n')
{
posR[cR]=val;
cR++;
}
if((c!='|')&&(cd<1))
{
cod[pos]=c;
pos++;
}
else
{
cod[pos]='\0';
cd++;
if(strcmpi(codU,cod)==0)
{
val=0;
lseek(fd,val,0);
write(fd,"&",1);
band++;
}
else
{
while((read(fd,&c,1)>0)&&(cod>0))
if(c=='\n')
{
cd=0;
pos=0;
cod[0]='\0';
break;
}
}
lseek(fd,0L,1);
}
}
getch();
}
void Datospers::modificar(){
char c, cod[10], codU[10],nom[20],edad[2],car[15];
int pos=0, band=0, cd=0, cR=0;
long val, posR[50];
printf("Dame el codigo del registro a modificar: ");
fflush(stdin);
gets(codU);
lseek(fd,0L,0);
while((read(fd,&c,1)>0) && (band<1))
{
val=lseek(fd,0L,1);
if(c=='\n')
{
posR[cR]=val;
cR++;
}
if((c!='|')&&(cd<1))
{
cod[pos]=c;
pos++;
}
else
{
cod[pos]='\0';
cd++;
if(strcmpi(codU,cod)==0)
{
lseek(fd,val-2,0);
write(fd,"&",1);
band++;
}
else
{
while((read(fd,&c,1)>0)&&(cod>0))
if(c=='\n')
{
cd=0;
pos=0;
cod[0]='\0';
break;
}
}
lseek(fd,0L,1);
}
}
lseek(fd,0L,2);
fflush(stdin);
printf("\n Nuevos datos:");
write(fd,codU,strlen(codU));
write(fd,"|",1);
printf("\n Nombre: ");
gets(nom);
write(fd,nom,strlen(nom));
write(fd,"|",1);
printf("\n Edad: ");
gets(edad);
write(fd,edad,strlen(edad));
write(fd,"|",1);
printf("\n Sexo: ");
gets(car);
write(fd,car,strlen(car));
write(fd,"\n",1);
}
int main(int argc, char *argv[])
{
int opc;
Datospers p;
do
{
system("cls");
printf("Elige una opcion: \n\n1.Agregar\n2.Mostrar\n3.Buscar\n4.Modificar\n5.Eliminar\n6.Salir ");
scanf("%d",&opc);
switch(opc){
case 1: p.agregar();
break;
case 2: p.mostrar();
break;
case 3: p.buscar();
break;
case 4: p.modificar();
break;
case 5: p.eliminar();
break;
case 6: printf("Adios!!!");
break;
}
}while(opc!=6);
close(p.fd);
return 0;
system("PAUSE");
return EXIT_SUCCESS;
getch();
}
//bibliotecas
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <conio.h>
#include <stdio.h>
#include <io.h>
using namespace std;
//clase
class Datospers
{
public:
//funciones y variables
int fd;//descriptor
Datospers();//declaracion de funciones
void agregar();
void mostrar();
void buscar();
void modificar();
void eliminar();
};
Datospers::Datospers()
{
fd=open("F:\\Prueba.txt",2);//abriendo el archivo
if(fd<0)
{
printf("\n Error al abrir archivo \t");
getch();
}
}
//funciones
void Datospers::agregar()
{
char cod[9],nom[20],edad[2],car[15];
int p,cont=0,opc;
// printf("hjjh");
//system ("PAUSE");
lseek(fd,0L,2);
do
{
fflush(stdin);
printf(" \n Introduce Codigo:");
gets(cod);
write(fd,cod,strlen(cod));
write(fd,"|",1);
printf("\n Introduce Nombre:");
gets(nom);
write(fd,nom,strlen(nom));
write(fd,"|",1);
printf("\n Introduce Edad: ");
gets(edad);
write(fd,edad,strlen(edad));
write(fd,"|",1);
printf("\n Introduce Sexo: ");
gets(car);
write(fd,car,strlen(car));
write(fd,"\n",1);
printf("\n ¿Deseas agregar otro?\n1.Si\n2.No ");
fflush(stdin);
scanf("%d",&opc);
}while(opc!=2);
}
void Datospers::mostrar()
{
char c,a;
lseek(fd,0L,0);
while(read(fd,&c,1)>0)
{
if(c=='&'){
while((read(fd,&c,1)>0)&&(c!='\n')) a='d';
}
if(c!='|')
write(1,&c,1);
else
write(1," ",1);
}
getch();
}
void Datospers::buscar()
{
char cad[20],c,cad2[20];
int pos=0,band=0,cod=0;
printf("¿Cual es el codigo a buscar:\n");
fflush(stdin);
gets(cad2);
cad[0]='\0';
lseek(fd,0L,0);
while((read(fd,&c,1)>0)&&(band<1))
{
if((c!='|')&&(cod<1))
{
cad[pos]=c;
pos++;
}
else
{
cad[pos]='\0';
cod++;
if(strcmpi(cad2,cad)==0)
{
while((read(fd,&c,1)>0)&&(c!='\n'))
{
if(c!='|')
write(1,&c,1);
else
write(1,"\t",1);
}
band++;
}
else
{
while((read(fd,&c,1)>0)&&(cod>0))
if(c=='\n')
{
cod=0;
pos=0;
cad[0]='\0';
break;
}
}
lseek(fd,0L,1);
}
}
getch();
}
void Datospers::eliminar()
{
char c, cod[10], codU[10];
int pos=0, band=0, cd=0, cR=0;
long val, posR[50];
printf("Codigo a eliminar:\n");
fflush(stdin);
gets(codU);
lseek(fd,0L,0);
while((read(fd,&c,1)>0) && (band<1))
{
val=lseek(fd,0L,1);
if(c=='\n')
{
posR[cR]=val;
cR++;
}
if((c!='|')&&(cd<1))
{
cod[pos]=c;
pos++;
}
else
{
cod[pos]='\0';
cd++;
if(strcmpi(codU,cod)==0)
{
val=0;
lseek(fd,val,0);
write(fd,"&",1);
band++;
}
else
{
while((read(fd,&c,1)>0)&&(cod>0))
if(c=='\n')
{
cd=0;
pos=0;
cod[0]='\0';
break;
}
}
lseek(fd,0L,1);
}
}
getch();
}
void Datospers::modificar(){
char c, cod[10], codU[10],nom[20],edad[2],car[15];
int pos=0, band=0, cd=0, cR=0;
long val, posR[50];
printf("Dame el codigo del registro a modificar: ");
fflush(stdin);
gets(codU);
lseek(fd,0L,0);
while((read(fd,&c,1)>0) && (band<1))
{
val=lseek(fd,0L,1);
if(c=='\n')
{
posR[cR]=val;
cR++;
}
if((c!='|')&&(cd<1))
{
cod[pos]=c;
pos++;
}
else
{
cod[pos]='\0';
cd++;
if(strcmpi(codU,cod)==0)
{
lseek(fd,val-2,0);
write(fd,"&",1);
band++;
}
else
{
while((read(fd,&c,1)>0)&&(cod>0))
if(c=='\n')
{
cd=0;
pos=0;
cod[0]='\0';
break;
}
}
lseek(fd,0L,1);
}
}
lseek(fd,0L,2);
fflush(stdin);
printf("\n Nuevos datos:");
write(fd,codU,strlen(codU));
write(fd,"|",1);
printf("\n Nombre: ");
gets(nom);
write(fd,nom,strlen(nom));
write(fd,"|",1);
printf("\n Edad: ");
gets(edad);
write(fd,edad,strlen(edad));
write(fd,"|",1);
printf("\n Sexo: ");
gets(car);
write(fd,car,strlen(car));
write(fd,"\n",1);
}
int main(int argc, char *argv[])
{
int opc;
Datospers p;
do
{
system("cls");
printf("Elige una opcion: \n\n1.Agregar\n2.Mostrar\n3.Buscar\n4.Modificar\n5.Eliminar\n6.Salir ");
scanf("%d",&opc);
switch(opc){
case 1: p.agregar();
break;
case 2: p.mostrar();
break;
case 3: p.buscar();
break;
case 4: p.modificar();
break;
case 5: p.eliminar();
break;
case 6: printf("Adios!!!");
break;
}
}while(opc!=6);
close(p.fd);
return 0;
system("PAUSE");
return EXIT_SUCCESS;
getch();
}
Valora esta pregunta


0