473,763 Members | 8,980 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Binary files /DELETE/

/*
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(F ILE *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_pupi ls),
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_pup ils);
come_back();
}
Apr 25 '06 #1
7 2782
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(F ILE *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.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Apr 25 '06 #2
Richard Heathfield <in*****@invali d.invalid> writes:
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(F ILE *file_pupils, int del)
No definition for FILE.

<SNIP>

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


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

--

John Devereux
Apr 25 '06 #3

John Devereux wrote:
Richard Heathfield <in*****@invali d.invalid> writes:
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(F ILE *file_pupils, int del)


No definition for FILE.

<SNIP>

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


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


Only negative, IMO. Let's see:
Olaf "El Blanco" said:
/*
It look for a last name... That's work OK.
Erm, no. As Richard pointed out, it's shot with errors. BTW, what last
name?
If 'del' is 1, after search it will delete
After search for what? Delete what?
I want to put the field valid at NOT_VALID
but even fwrite return 1, it never copy anything


This one's completely lost on me...

Apr 25 '06 #4
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.



Apr 25 '06 #5
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Apr 25 '06 #6
"Olaf \"El Blanco\"" <ve******@yahoo .no> writes:
Sorry...
Do you need the whole code?
What whole code?

Don't assume anyone can see the article you're responding to. Read
<http://cfaj.freeshell. org/google/>.
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.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 25 '06 #7
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.tx t"

#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(vo id);
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_AR CHIVO, "rb")) == NULL) {
printf ("Creando el fichero <%s>...", NOMBRE_ARCHIVO) ;
fdat = fopen(NOMBRE_AR CHIVO, "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(fda t);
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(vo id)
/* Devuelve un Registro Alumno */
{
TAlumno aux;
fflush(stdin);
printf ("\n1/5 --> Ingrese Nombre: "); gets(aux.nombre );
printf ("2/5 --> Ingrese Apellidos: "); gets(aux.apelli do);
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_AR CHIVO, "ab");
fwrite(&aux, sizeof(TAlumno) , 1, archivo);
fclose(archivo) ;
}

/* See the file */
Ver_archivo(FIL E *archivo)
/* Muestra el archivo completo */
{
TAlumno persona;
system ("cls");
encabezado();
fopen(NOMBRE_AR CHIVO, "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.apellid o, persona.edad,
persona.nota_me dia, persona.DNI);
}
fclose(archivo) ;
regresar();
}

/* Show 'pos' position in the file */
void Muestra_posicio n(long int pos, FILE *archivo)
{
TAlumno persona;
/* Possible to open? */
if ((archivo = fopen(NOMBRE_AR CHIVO, "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.apellid o,
persona.nombre, persona.edad, persona.nota_me dia, persona.DNI);
fclose(archivo) ;
}
}
}

void Borra_Posicion( long int pos, FILE *archivo)
{
TAlumno aux;
fclose(archivo) ;
archivo = fopen(NOMBRE_AR CHIVO, "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_AR CHIVO, "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_med ia, 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_med ia, 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_posicio n(posicion*size of(TAlumno), archivo);
if (borrar == ELIMINA) {
archivo = fopen(NOMBRE_AR CHIVO, "rb");
eliminado.valid o = INVALIDO;
fseek(archivo, posicion - sizeof(TAlumno) , SEEK_SET);
fwrite(&elimina do, 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.apellid o,
persona.nombre, persona.edad, persona.nota_me dia, persona.DNI);
fclose(archivo) ; */


void imprime_menu(vo id)
{
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\tES C 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\tES C 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\tES C 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();
}


Apr 25 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
555
by: Simon | last post by:
Hi all, I have a process, where I take a dataset from an SQL call, and need to write an XML file from that dataset. The data set can contain 10's of tables, each with 100's of rows, and I have no way of knowing what the tables are, or what they contain. I am currently using the Dataset.WriteXML method passing an XMLWriter.
20
3072
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code: --cut here-- typedef struct pkg_ { short int info; char* data;
6
2728
by: | last post by:
Hi all, is there a better way to stream binary data stored in a table in sql 2005 to a browser in .net 2.0? Or is the code same as in .net 1.1? We noticed that in certain heavy load scenarios, every now and then the client would timeout and have to re-initiate the request... TIA!
4
6724
by: Pedro Leite | last post by:
Good Afternoon. the code below is properly retreiving binary data from a database and saving it. but instead of saving at client machine is saving at the server machine. what is wrong with my code ?? thank you Pedro Leite From Portugal ------------------------------------
2
7235
by: tech101 | last post by:
Can I get the master (or slaves) to automatically remove the binary logs once they are processed by all slaves? It says in the mysql manual : If you are using replication, you should not delete old binary log files until you are sure that no slave still needs to use them. For example, if your slaves never run more than three days behind, once a day you can execute mysqladmin flush-logs on the master and then remove any logs that are...
0
4100
by: phoenix7 | last post by:
Dear all, I want to store some data in form of a zip file into an access database. I created a table with with a column of type OLE Object, then I designed a form to insert data to the table. I inserted some files to it, but when I tried to read them using my java client it retrieves an OLE object but I just need the file content (what the program gets form the db was the original file added with some extra binary information at the...
3
2144
by: showson1 | last post by:
Hi all. We have some files that are basically TIF images with a text header. The header portion is blocked by {startdb} and {enddb}.. it is the same format for all the files. What I need to do is read the file in, delete that block (including the startdb and enddb) and write out the result as a TIF. I can manually do this by loading them in a HEX editor, deleting the block and saving it, opens fine... I'm just not sure how to do it...
5
2586
by: Canned | last post by:
Hi, I'm trying to write a class that can convert ascii to binary and vice versa. I write my class based on this function I've found on internet That works perfectly, but when I try to implement it in my own class it gives me alot of headache, also because I'm totally new to the language. It work only with one character at a time, and if I give a string it just give some weird result.
3
3790
by: reddevils2020 | last post by:
Hi everyone! How we can change the end of binary files?for example we have a file contains 6 recorde and each recorde is 10 byte(size of file is 60byte).I want to delete the last recorde,I want to set the file seek on 50th byte and make here end of my file that means I want to delete the last 10 byte and change size of file to 50 byte.how can I do this?(I now other ways to delete one recorde,please dont commend other ways!!!) thank you bye...
0
9564
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9387
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10148
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10002
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.