completar programa
Estoy viendo archivos en c++, actualizaciones
Necesito un programa que de alta, de baja, que pueda hacer cambios y que haga consultas.
Encontre un programa que realiza cambios, da de alta(permite introducir datos), y permite realizar consultas (leer el archivos), pero no tiene la opcion de dar de baja (eliminar). Ya intente pero no he podido, quisiera que me ayudaran completando el programa para que pueda eliminar. Es muy importante. Si alguien puede por favor comuniquense.
este el programa
//CompraVenta.cpp Permite crear una base de datos en un archivo .txt para un concesionario
//de compra-venta de coches. Permite introducir, consultar y modificar datos.
#include <stdlib.h>
#include <process.h>
#include <ctype.h>
#include <string.h>
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <iomanip.h>
#include <ctype.h>
#include <fstream.h>
#include <fcntl.h>
typedef struct
{
char marca[30];
char modelo[30];
char cc[30]; //cilindrada en centimetros cúbicos
char cv[30]; //Potencia en caballos
char matricula[30];
char precio[30];
}garaje;
garaje coches; //Declaración de tipos (ver arriba typedef)
int visualizar(void);
void grabar(void);
int buscar(void);
void main()
{
int opcion;
for (;;){
do
{
clrscr();
printf("nt*****************************************************");
printf("nt* COCHES DE OCASION *");
printf("nt*****************************************************");
printf("nt* 1. INTRODUCIR DATOS DEL VEHICULO *");
printf("nt* 2. LEER LOS DATOS DEL VEHICULO *");
printf("nt* 3. BUSCAR Y MODIFICAR LOS DATOS DE UN VEHICULO *");
printf("nt* 4. SALIR *");
printf("nt*****************************************************");
printf("nnnnt ELIJE UNA OPCION Y PULSA ENTER: ");
scanf("%d", &opcion);
}while(opcion<1||opcion>5);
switch(opcion)
{
case 1:
grabar();
break;
case 2:
visualizar();
break;
case 3:
buscar();
break;
case 4:
exit(0);
break;
default:
exit(0);
}
}
}
void grabar(void)
{
FILE *punfich;
char respuesta='s';
if ((punfich = fopen("a:/garaje.txt", "ab")) == NULL)
{
fprintf(stderr, "IMPOSIBLE ABRIR EL ARCHIVO ELEGIDOn");
exit(0);
}
while(respuesta=='s'||respuesta=='S')
{
clrscr();
fflush(stdin);
printf("nnt MARCA DEL COCHE: ");
gets(coches.marca);
printf("nt MODELO DEL COCHE: ");
gets(coches.modelo);
printf("nt CENTIMETROS CUBICOS: ");
gets(coches.cc);
printf("nt POTENCIA EN CABALLOS: ");
gets(coches.cv);
printf("nt MATRICULA DEL COCHE: ");
gets(coches.matricula);
printf("nt PRECIO DEL COCHE: ");
gets(coches.precio);
fflush(stdin);
//Escribimo en archivo el bloque de datos
fwrite(&coches, sizeof(coches), 1, punfich);
printf ("nnt QUIERES INTRODUCIR MAS DATOS s/n:");
scanf ("%c",&respuesta);
}
fclose(punfich);
}
int visualizar(void)
{
FILE *punfich;
int totalreg=0;
int i=0;
int registro=0;
clrscr();
if((punfich = fopen("a:/garaje.txt", "rb"))==NULL)
{
fprintf(stderr, "IMPOSIBLE ABRIR EL ARCHIVO ELEGIDO");
getch();
exit(0);
}
do
{
fseek(punfich, 0L, SEEK_END);
totalreg = (int)ftell(punfich) /sizeof(coches);
registro=registro+1;
printf ("nt El numero de registros es: %d",totalreg);
printf ("nt El registro actual es: %d",registro);
fseek(punfich, sizeof(coches)*i, SEEK_SET);
fread(&coches, sizeof(coches), 1, punfich);
fflush(stdin);
printf("nnt MARCA DEL COCHE: ");
puts(coches.marca);
printf("nt MODELO DEL COCHE: ");
puts(coches.modelo);
printf("nt CENTIMETROS CUBICOS: ");
puts(coches.cc);
printf("nt POTENCIA EN CABALLOS: ");
puts(coches.cv);
printf("nt MATRICULA DEL COCHE: ");
puts(coches.matricula);
printf("nt PRECIO DEL COCHE: ");
puts(coches.precio);
printf("nnt PARA VER EL SIGUIENTE REGISTRO PULSE ENTER ");
getch();
i++;
clrscr();
if(i==totalreg)
{
fclose(punfich);
return(1);
}
}while (!ferror(punfich) && !feof(punfich));
fclose(punfich);
getch();
return(0);
}
int buscar(void)
{
int bytesreg = sizeof(coches);
FILE *punfich;
int totalreg;
int nreg;
long desp;
int c, respuesta;
if ((punfich = fopen("a:/garaje.txt", "r+b")) == NULL)
{
fprintf(stderr, "IMPOSIBLE ABRIR EL ARCHIVO ELEGIDO");
exit(0);
}
fseek(punfich, 0L, SEEK_END);
totalreg = (int)ftell(punfich)/bytesreg;
do
{
clrscr();
printf("nn N§ de registro a buscar entre 1 y %d (0 para salir): ", totalreg);
c = scanf("%d",&nreg);
fflush(stdin);
if(c && (nreg>=1) && (nreg<=totalreg))
{
desp = (long)(nreg-1)*bytesreg;
fseek(punfich, desp, SEEK_SET);
fread(&coches, bytesreg, 1, punfich);
if (ferror(punfich))
{
printf("n Error al leer un registro del fichero.n");
exit(1);
}
printf("nnt MARCA DEL COCHE: ");
puts(coches.marca);
printf("nt MODELO DEL COCHE: ");
puts(coches.modelo);
printf("nt CENTIMETROS CUBICOS: ");
puts(coches.cc);
printf("nt POTENCIA EN CABALLOS: ");
puts(coches.cv);
printf("nt MATRICULA DEL COCHE: ");
puts(coches.matricula);
printf("nt PRECIO DEL COCHE: ");
puts(coches.precio);
do
{
printf("nn ¨Desea modificar el registro? (s/n) ");
respuesta = getchar();
fflush(stdin);
clrscr();
}while(tolower(respuesta != 's') && tolower(respuesta != 'n'));
if (respuesta=='s')
{
printf("nnt MARCA DEL COCHE: ");
gets(coches.marca);
printf("nt MODELO DEL COCHE: ");
gets(coches.modelo);
printf("nt CENTIMETROS CUBICOS: ");
gets(coches.cc);
printf("nt POTENCIA EN CABALLOS: ");
gets(coches.cv);
printf("nt MATRICULA DEL COCHE: ");
gets(coches.matricula);
printf("nt PRECIO DEL COCHE: ");
gets(coches.precio);
fseek(punfich, -bytesreg, SEEK_CUR); //Retrocedemos en el archivo
fwrite(&coches, bytesreg, 1, punfich);
if(ferror(punfich))
{
printf("n Error al escribir el registro.n");
getch();
exit(2);
}
}
}
}while(nreg);
fclose(punfich);
return(1);
}
Necesito un programa que de alta, de baja, que pueda hacer cambios y que haga consultas.
Encontre un programa que realiza cambios, da de alta(permite introducir datos), y permite realizar consultas (leer el archivos), pero no tiene la opcion de dar de baja (eliminar). Ya intente pero no he podido, quisiera que me ayudaran completando el programa para que pueda eliminar. Es muy importante. Si alguien puede por favor comuniquense.
este el programa
//CompraVenta.cpp Permite crear una base de datos en un archivo .txt para un concesionario
//de compra-venta de coches. Permite introducir, consultar y modificar datos.
#include <stdlib.h>
#include <process.h>
#include <ctype.h>
#include <string.h>
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <iomanip.h>
#include <ctype.h>
#include <fstream.h>
#include <fcntl.h>
typedef struct
{
char marca[30];
char modelo[30];
char cc[30]; //cilindrada en centimetros cúbicos
char cv[30]; //Potencia en caballos
char matricula[30];
char precio[30];
}garaje;
garaje coches; //Declaración de tipos (ver arriba typedef)
int visualizar(void);
void grabar(void);
int buscar(void);
void main()
{
int opcion;
for (;;){
do
{
clrscr();
printf("nt*****************************************************");
printf("nt* COCHES DE OCASION *");
printf("nt*****************************************************");
printf("nt* 1. INTRODUCIR DATOS DEL VEHICULO *");
printf("nt* 2. LEER LOS DATOS DEL VEHICULO *");
printf("nt* 3. BUSCAR Y MODIFICAR LOS DATOS DE UN VEHICULO *");
printf("nt* 4. SALIR *");
printf("nt*****************************************************");
printf("nnnnt ELIJE UNA OPCION Y PULSA ENTER: ");
scanf("%d", &opcion);
}while(opcion<1||opcion>5);
switch(opcion)
{
case 1:
grabar();
break;
case 2:
visualizar();
break;
case 3:
buscar();
break;
case 4:
exit(0);
break;
default:
exit(0);
}
}
}
void grabar(void)
{
FILE *punfich;
char respuesta='s';
if ((punfich = fopen("a:/garaje.txt", "ab")) == NULL)
{
fprintf(stderr, "IMPOSIBLE ABRIR EL ARCHIVO ELEGIDOn");
exit(0);
}
while(respuesta=='s'||respuesta=='S')
{
clrscr();
fflush(stdin);
printf("nnt MARCA DEL COCHE: ");
gets(coches.marca);
printf("nt MODELO DEL COCHE: ");
gets(coches.modelo);
printf("nt CENTIMETROS CUBICOS: ");
gets(coches.cc);
printf("nt POTENCIA EN CABALLOS: ");
gets(coches.cv);
printf("nt MATRICULA DEL COCHE: ");
gets(coches.matricula);
printf("nt PRECIO DEL COCHE: ");
gets(coches.precio);
fflush(stdin);
//Escribimo en archivo el bloque de datos
fwrite(&coches, sizeof(coches), 1, punfich);
printf ("nnt QUIERES INTRODUCIR MAS DATOS s/n:");
scanf ("%c",&respuesta);
}
fclose(punfich);
}
int visualizar(void)
{
FILE *punfich;
int totalreg=0;
int i=0;
int registro=0;
clrscr();
if((punfich = fopen("a:/garaje.txt", "rb"))==NULL)
{
fprintf(stderr, "IMPOSIBLE ABRIR EL ARCHIVO ELEGIDO");
getch();
exit(0);
}
do
{
fseek(punfich, 0L, SEEK_END);
totalreg = (int)ftell(punfich) /sizeof(coches);
registro=registro+1;
printf ("nt El numero de registros es: %d",totalreg);
printf ("nt El registro actual es: %d",registro);
fseek(punfich, sizeof(coches)*i, SEEK_SET);
fread(&coches, sizeof(coches), 1, punfich);
fflush(stdin);
printf("nnt MARCA DEL COCHE: ");
puts(coches.marca);
printf("nt MODELO DEL COCHE: ");
puts(coches.modelo);
printf("nt CENTIMETROS CUBICOS: ");
puts(coches.cc);
printf("nt POTENCIA EN CABALLOS: ");
puts(coches.cv);
printf("nt MATRICULA DEL COCHE: ");
puts(coches.matricula);
printf("nt PRECIO DEL COCHE: ");
puts(coches.precio);
printf("nnt PARA VER EL SIGUIENTE REGISTRO PULSE ENTER ");
getch();
i++;
clrscr();
if(i==totalreg)
{
fclose(punfich);
return(1);
}
}while (!ferror(punfich) && !feof(punfich));
fclose(punfich);
getch();
return(0);
}
int buscar(void)
{
int bytesreg = sizeof(coches);
FILE *punfich;
int totalreg;
int nreg;
long desp;
int c, respuesta;
if ((punfich = fopen("a:/garaje.txt", "r+b")) == NULL)
{
fprintf(stderr, "IMPOSIBLE ABRIR EL ARCHIVO ELEGIDO");
exit(0);
}
fseek(punfich, 0L, SEEK_END);
totalreg = (int)ftell(punfich)/bytesreg;
do
{
clrscr();
printf("nn N§ de registro a buscar entre 1 y %d (0 para salir): ", totalreg);
c = scanf("%d",&nreg);
fflush(stdin);
if(c && (nreg>=1) && (nreg<=totalreg))
{
desp = (long)(nreg-1)*bytesreg;
fseek(punfich, desp, SEEK_SET);
fread(&coches, bytesreg, 1, punfich);
if (ferror(punfich))
{
printf("n Error al leer un registro del fichero.n");
exit(1);
}
printf("nnt MARCA DEL COCHE: ");
puts(coches.marca);
printf("nt MODELO DEL COCHE: ");
puts(coches.modelo);
printf("nt CENTIMETROS CUBICOS: ");
puts(coches.cc);
printf("nt POTENCIA EN CABALLOS: ");
puts(coches.cv);
printf("nt MATRICULA DEL COCHE: ");
puts(coches.matricula);
printf("nt PRECIO DEL COCHE: ");
puts(coches.precio);
do
{
printf("nn ¨Desea modificar el registro? (s/n) ");
respuesta = getchar();
fflush(stdin);
clrscr();
}while(tolower(respuesta != 's') && tolower(respuesta != 'n'));
if (respuesta=='s')
{
printf("nnt MARCA DEL COCHE: ");
gets(coches.marca);
printf("nt MODELO DEL COCHE: ");
gets(coches.modelo);
printf("nt CENTIMETROS CUBICOS: ");
gets(coches.cc);
printf("nt POTENCIA EN CABALLOS: ");
gets(coches.cv);
printf("nt MATRICULA DEL COCHE: ");
gets(coches.matricula);
printf("nt PRECIO DEL COCHE: ");
gets(coches.precio);
fseek(punfich, -bytesreg, SEEK_CUR); //Retrocedemos en el archivo
fwrite(&coches, bytesreg, 1, punfich);
if(ferror(punfich))
{
printf("n Error al escribir el registro.n");
getch();
exit(2);
}
}
}
}while(nreg);
fclose(punfich);
return(1);
}
Me imagino que lo que desea al decir dar de baja, es borrar un conjunto determinado de datos del archivo, lo cual que yo sepa no se puede hacer, una forma de simularlo es colocando un campo especial en la estructura donde van los datos, este campo será entero y valdrá 1, si esos datos estan activos y 0 si fueron dados de baja osea "borrados", asi cuando consulte el archivo primero mira que devuelve la estructura en ese campo, si es 1 lo muestra, si es 0 da un mensaje de error o algo.
Otra sln es cuando se de baja un dato, hacer que el archivo se copie en otro completamente menos el dato que no necesita, esto puede tomar algo de tiempo dependiendo del archivo, pero puede ser colocado como una opción para el usuario, algo asi como preguntar: ¿Desea actualizar el archivo?.
Espero que esto le sirva de algo. SUERTE.
Otra sln es cuando se de baja un dato, hacer que el archivo se copie en otro completamente menos el dato que no necesita, esto puede tomar algo de tiempo dependiendo del archivo, pero puede ser colocado como una opción para el usuario, algo asi como preguntar: ¿Desea actualizar el archivo?.
Espero que esto le sirva de algo. SUERTE.
