Binary files /DELETE/

O

Olaf \El Blanco\

/*
It look for a last name... That's work OK.
If 'del' is 1, after search it will delete
I want to put the field valid at NOT_VALID
but even fwrite return 1, it never copy anything
*/

void Search_Delete(FILE *file_pupils, int del)
{
char option;
char Last_name[MAX_LAST_NAME];
Tpupil pupil;
printf ("Last name to search? ");
scanf ("%s", &Last_name);
while (fread(&pupil, sizeof(pupil), 1, file_pupils) == 1) {
if ((strcmp(pupil.last_name, Last_name) == 0) && (alumno.valid ==
VALID)) {
printf ("[%d] %10s, %10s %16d %15.2f%15s\n", ftell(file_pupils),
pupil.last_name, pupil.name, pupil.age, pupil.note, pupil.DNI);
if (del == 1) {
printf ("Are you sure? [y/N]\n");
option = getche();
if (toupper(option) == 'Y') {
pupil.valid = NOT_VALID;
fwrite(&pupil, sizeof(Tpupil), 1, file_pupil);
}
}
}
}
fclose(file_pupils);
come_back();
}
 
R

Richard Heathfield

Olaf "El Blanco" said:
/*
It look for a last name... That's work OK.
If 'del' is 1, after search it will delete
I want to put the field valid at NOT_VALID
but even fwrite return 1, it never copy anything
*/

void Search_Delete(FILE *file_pupils, int del)

No definition for FILE.
{
char option;
char Last_name[MAX_LAST_NAME];

No definition for MAX_LAST_NAME;
Tpupil pupil;

No definition for Tpupil.
printf ("Last name to search? ");

No prototype for printf. Behaviour is undefined.

No flushing or newline after the prompt but before blocking for input -
thus, no guarantee that the prompt will be displayed:
scanf ("%s", &Last_name);

No prototype for scanf. Behaviour is undefined.

Insecure use of scanf ("%s" invites buffer overruns, either accidental or
malicious). %s matches char *, not char (*)[MAX_LAST_NAME].
while (fread(&pupil, sizeof(pupil), 1, file_pupils) == 1) {
if ((strcmp(pupil.last_name, Last_name) == 0) && (alumno.valid ==
VALID)) {

No prototype for fread. No definition for alumno. No prototype for strcpy.
No definition of VALID.

And so on and so on.

This thing is so shot with errors it's painful.
 
V

Vladimir Oka

John said:
Yes but he embedded his question as a C comment! Surely he gets
points for that here?

Only negative, IMO. Let's see:

Erm, no. As Richard pointed out, it's shot with errors. BTW, what last
name?

After search for what? Delete what?

This one's completely lost on me...
 
O

Olaf \El Blanco\

Sorry...
Do you need the whole code?
And Is a problem if the names of my variables are in another languaje?
I need an example of how is the best way to delete a register in a binary
file. The user give me a information of any field and I search and delete.
(Directly in the file, I don't need tu loaded the file in memory)

Maybe you know a good page full of good examples.

Thanks.
 
R

Richard Heathfield

Olaf "El Blanco" said:
Sorry...
Do you need the whole code?

Normally, yes, since we can't debug in a vacuum. But if it's all written
like that, the experience might be too painful.
I need an example of how is the best way to delete a register in a binary
file. The user give me a information of any field and I search and delete.
(Directly in the file, I don't need tu loaded the file in memory)

The best way to do this is to include, in each record, a field for recording
whether the record is "live". If it is 0, it is not live, and can be
considered deleted. If it is 1, it is live, and must not be deleted.

Then all you have to do is update the value of that field.

From time to time, perhaps once a day or once a week or once a year or
whatever, you can run a consolidation program through the file. This would
read each record in the file, in turn, and test its "live" field. It would
write all the live records to a new file, ignoring all the deleted records.


The only alternative I can see right now is to rewrite the whole file every
time, which I presume you don't want to do.
 
K

Keith Thompson

Olaf \"El Blanco\" said:
Sorry...
Do you need the whole code?

What whole code?

Don't assume anyone can see the article you're responding to. Read
And Is a problem if the names of my variables are in another languaje?
I need an example of how is the best way to delete a register in a binary
file. The user give me a information of any field and I search and delete.
(Directly in the file, I don't need tu loaded the file in memory)

What do you mean by "register"? Files, binary or otherwise, don't
contain registers.
 
O

Olaf \El Blanco\

I know that you need to do a big mind effort to understand this...
I understand if i have no reply.
Thanks.
/*************************************************************/
#include <stdio.h>
#include <stdlib.h>

#define ESC '\x1b'

#define NOMBRE_ARCHIVO "datosp1.txt"

#define ERROR_01_ES "No se puede abrir el fichero"
#define ERROR_01_EN "Can't open file"

#define ELIMINA 1
#define NO_ELIMINA 0


#define VALIDO 1 /* Valid */
#define INVALIDO 0 /* Not valid */

#define MAX_NOMBRE 15 /* Max chars for name */
#define MAX_APELLIDO 20 /* Max chars for last name */
#define MAX_DNI 10 /* Max chars for DNI */


typedef struct {
int valido; /* valid ? */
char nombre[MAX_NOMBRE]; /* Name */
char apellido[MAX_APELLIDO]; /* Last Name */
int edad; /* Age */
float nota_media; /* Note */
char DNI[MAX_DNI]; /* Dni */
} TAlumno; /* Type Pupil */


void Busca_Apellido(FILE *archivo, int borrar);
void Busca_Posicion(FILE *archivo, int borrar);
void sub_menu_buscar(void);
void encabezado(void);
void sub_menu_borrar(void);
void imprime_menu(void);
void regresar(void);
void salida(void);


main()
{
char opcion; /* Optcion */
FILE *fdat;
/* If it doesn't exist, I'll crate it */
if ((fdat = fopen(NOMBRE_ARCHIVO, "rb")) == NULL) {
printf ("Creando el fichero <%s>...", NOMBRE_ARCHIVO);
fdat = fopen(NOMBRE_ARCHIVO, "wb");
printf ("OK!\n");
}
fclose(fdat);
do {
/* Show menu */
imprime_menu();
opcion = getch();
switch(opcion) {
case '1' :
/* Add Pupil */
Agregar_alumno(fdat);
break;
case '2' :
/* See the while file */
Ver_archivo(fdat);
break;
case '3' :
do {
/* Sub menu for search */
sub_menu_buscar();
opcion = getche();
switch(opcion) {
case '1' :
Busca_Apellido(fdat,
NO_ELIMINA);
break;
case '2' : Busca_Posicion(fdat,
NO_ELIMINA);
case '3' : break;
}
}
while (opcion != '3');
break;
case '4' :
do {
sub_menu_borrar();
opcion = getche();
switch(opcion) {
case '1' :
Busca_Apellido(fdat, ELIMINA);
break;
case '2' :
Busca_Posicion(fdat, ELIMINA);
break;
case '3' : break;
}
}
while (opcion != '3');
break;
}
}
while (opcion != ESC);
salida();

}

/* Return a pupil */
TAlumno Nuevo_Alumno(void)
/* Devuelve un Registro Alumno */
{
TAlumno aux;
fflush(stdin);
printf ("\n1/5 --> Ingrese Nombre: "); gets(aux.nombre);
printf ("2/5 --> Ingrese Apellidos: "); gets(aux.apellido);
printf ("3/5 --> Ingrese Edad: "); scanf("%d", &aux.edad);
printf ("4/5 --> Ingrese Nota Media: "); scanf("%f", &aux.nota_media);
printf ("5/5 --> Ingrese DNI: "); scanf("%s", &aux.DNI);
return aux;
}

/* Add a pupil in the file */
Agregar_alumno(FILE *archivo)
/* Agrega un alumno al final del archivo, el archivo se abre y se cierra
aqui */
{
TAlumno aux;
aux = Nuevo_Alumno(); /* En aux guarda los datos entrado por usuario */
aux.valido = VALIDO; /* Le digo que es valido */
archivo = fopen(NOMBRE_ARCHIVO, "ab");
fwrite(&aux, sizeof(TAlumno), 1, archivo);
fclose(archivo);
}

/* See the file */
Ver_archivo(FILE *archivo)
/* Muestra el archivo completo */
{
TAlumno persona;
system ("cls");
encabezado();
fopen(NOMBRE_ARCHIVO, "rb");
while (fread(&persona, sizeof(persona), 1, archivo) == 1) {
/* Las dos lineas siguientes luego las borrare, son para checkeo */
if (persona.valido == VALIDO) printf ("V");
else printf ("I");
/* strcat(persona.apellido, ", ");
strcat(persona.apellido, persona.nombre); */
printf ("%20s %16d %15.2f%15s\n", persona.apellido, persona.edad,
persona.nota_media, persona.DNI);
}
fclose(archivo);
regresar();
}

/* Show 'pos' position in the file */
void Muestra_posicion(long int pos, FILE *archivo)
{
TAlumno persona;
/* Possible to open? */
if ((archivo = fopen(NOMBRE_ARCHIVO, "rb")) == NULL)
printf (ERROR_01_ES);
else {
/* Go to the end */
fseek(archivo, 0L, SEEK_END);
/* Tell me if the current position (the last one) is < than the one
that you want to see */
if (pos > ftell(archivo))
printf ("Quiere ver un numero de registro, que aun no ha sido
copiado\n");
else {
fseek(archivo, pos - sizeof(persona), SEEK_SET);
fread(&persona, sizeof(persona), 1, archivo);
printf ("%10s, %10s %16d %15.2f%15s\n", persona.apellido,
persona.nombre, persona.edad, persona.nota_media, persona.DNI);
fclose(archivo);
}
}
}

void Borra_Posicion(long int pos, FILE *archivo)
{
TAlumno aux;
fclose(archivo);
archivo = fopen(NOMBRE_ARCHIVO, "r+");
fseek(archivo, pos, SEEK_SET);
aux.valido = INVALIDO;
fwrite(&aux, sizeof(TAlumno), 1, archivo);
}


/*** PROBLEMS ****/
/*** PROBLEMS ****/
/*** PROBLEMS ****/
/*** PROBLEMS ****/

void Busca_Apellido(FILE *archivo, int borrar)
{
char opcion;
char Apellido[MAX_APELLIDO];
int num_reg=0;
TAlumno alumno, aux;
printf ("Ingrese apellido a Buscar: ");
scanf ("%s", &Apellido);
if ((archivo = fopen(NOMBRE_ARCHIVO, "r+b")) == NULL)
printf (ERROR_01_ES);
encabezado();
while (fread(&alumno, sizeof(alumno), 1, archivo) == 1) {
if ((strcmp(alumno.apellido, Apellido) == 0) && (alumno.valido ==
VALIDO)) {
printf ("[%d] %10s, %10s %16d %15.2f%15s\n", ftell(archivo),
alumno.apellido, alumno.nombre, alumno.edad, alumno.nota_media, alumno.DNI);
if (borrar == ELIMINA) {
printf ("¿Esta seguro que desea borrarlo? [s/N]\n");
opcion = getche();
if (toupper(opcion) == 'S') {
alumno.valido = INVALIDO;
printf ("[%d] %d %10s, %10s %16d %15.2f%15s\n",
ftell(archivo), alumno.valido, alumno.apellido, alumno.nombre, alumno.edad,
alumno.nota_media, alumno.DNI);
if (fwrite(&alumno, sizeof(TAlumno), 2, archivo) == 1)
printf ("Algo se ha copiado");
}
}
}
num_reg++;
}
fclose(archivo);
regresar();
}



void Busca_Posicion(FILE *archivo, int borrar)
{
TAlumno eliminado;
int posicion;
printf ("Ingrese la posicion del registr que quiere ver: ");
scanf ("%d", &posicion);
Muestra_posicion(posicion*sizeof(TAlumno), archivo);
if (borrar == ELIMINA) {
archivo = fopen(NOMBRE_ARCHIVO, "rb");
eliminado.valido = INVALIDO;
fseek(archivo, posicion - sizeof(TAlumno), SEEK_SET);
fwrite(&eliminado, sizeof(TAlumno), 1, archivo);
}
regresar();
}
/* fseek(archivo, pos - sizeof(persona), SEEK_SET);
fread(&persona, sizeof(persona), 1, archivo);
printf ("%10s, %10s %16d %15.2f%15s\n", persona.apellido,
persona.nombre, persona.edad, persona.nota_media, persona.DNI);
fclose(archivo); */




void imprime_menu(void)
{
system ("cls");
printf ("\n\n\n\n\n\n");
printf ("\t\t\t1. Añadir elemento\n");
printf ("\t\t\t2. Mostrar archivo\n");
printf ("\t\t\t3. Buscar elemento -> \n");
printf ("\t\t\t4. Borrar elemento -> \n");
printf ("\t\t\t5. Subir archivo a memoria\n");
printf ("\n\t\t\tESC Salir\n");
}
void sub_menu_borrar(void)
{
system ("cls");
printf ("\n\n\n\n\n\n");
printf ("\t\t\t1. Añadir elemento\n");
printf ("\t\t\t2. Mostrar archivo\n");
printf ("\t\t\t3. Buscar elemento -> \n");
printf ("\t\t\t4. Borrar elemento -> \n");
printf ("\t\t\t |---------> 1. Buscandolo por Apellido\n");
printf ("\t\t\t |---------> 2. Buscandolo por posicion\n");
printf ("\t\t\t |---------> <ESC Menu anterior>\n");
printf ("\t\t\t5. Subir archivo a memoria\n");
printf ("\n\t\t\tESC Salir\n");
}

void encabezado(void)
{
char Nombre[] = "Nombre Completo";
char Edad[] = "Edad";
char Nota[] = "Nota Media";
char Dni[] = "DNI";
printf (" %s %20s %15s %15s\n", Nombre, Edad, Nota, Dni);
printf
("--------------------------------------------------------------------------------\n");
}

void sub_menu_buscar(void)
{
system ("cls");
printf ("\n\n\n\n\n\n");
printf ("\t\t\t1. Añadir elemento\n");
printf ("\t\t\t2. Mostrar archivo\n");
printf ("\t\t\t3. Buscar elemento -> \n");
printf ("\t\t\t |---------> 1. por Apellido\n");
printf ("\t\t\t |---------> 2. por posicion\n");
printf ("\t\t\t |---------> <ESC Menu anterior>\n");
printf ("\t\t\t4. Borrar elemento -> \n");
printf ("\t\t\t5. Subir archivo a memoria\n");
printf ("\n\t\t\tESC Salir\n");
}
void regresar(void)
{
printf ("Presione una tecla para regresar...");
getch();
}

void salida(void)
{
printf ("\b\a24 de Abril de 2006\n\n\n");
getch();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Command Line Arguments 0
URGENT 1
HOW to convert this coding to DEV c++ 4.9.9.2 1
Calling free() causes weird crash. 2
Lexical Analysis on C++ 1
Question about printf... 1
Binary Search in C 7
A Binary Tree in C 18

Members online

Forum statistics

Threads
474,183
Messages
2,570,964
Members
47,511
Latest member
svareza

Latest Threads

Top