473,651 Members | 2,582 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with files in C - Delete data in C file

2 New Member
Hi you guys! How are? I'm new at the forum and know you could help me!!

I Have a C program that works with file...it's so simple, but i need help with deletion of some data in a file(.dat). This data i refer, is something previous recorded in file.
Besides, i need to implement a bubble sort algorithm to sort the product code.
Most of the code or some words are in portuguese, but i think you may understand that!!!

See the following code and try to help me...


#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include<string .h>

//cria estrutura do arquivo
struct material {

int codigo, estoque;
char nome[20], genero[20];
float preco;

};

typedef struct material Material;

//definicao das funcoes
void cadastrar();
void listar();
void pesquisar();
//void alterar(); pensar em como implementar!!?
//void ordenar();


int main ()
{
int opcao=1;
while(opcao !=0) {
clrscr();
cout<<"\n\n\t\t =============== =============";
cout<<"\n\t\t ---- Spaces Bookstore ----";
cout<<"\n\n\t\t =============== =============";
cout<<"\t\t\n\n \nNome: Gustavo Costa de Freitas"; cout<<"\nRA: 004200600084";
cout<<"\n\n1 - Cadastrar livro ou revista";
cout<<"\n2 - Listar livro ou revista";
cout<<"\n3 - Pesquisar nome do livro ou revista";
//cout<<"\n4 -Ordenacao de codigos";
cout<<"\n0 - Sair";
cout<<"\n\nDigi te a opcao desejada: ";
cin>>opcao;


switch(opcao) {
case 1: cadastrar(); break;
case 2: listar(); break;
case 3: pesquisar(); break;
//case 4: ordenar(); break;
default: cout <<"Opcao invalida"; break;
}
}
return 0;
}

//funcao para cadastrar livros e revistas
void cadastrar() {

Material pr;
FILE * parq;
int cod;

parq = fopen("material .dat", "ab");
if (parq==NULL)
{
cout<<"\nERRO na abertura do Arquivo para gravacao";
getch();
}
else
{
clrscr();
cout<<"\nCADAST RO DE LIVROS E REVISTAS";
cout<<"\n-----------------------------";
cout<<"\nDigite o codigo do livro ou revista <0 para sair>: ";
cin>> pr.codigo;
//if(pr.codigo == pr.codigo)
//cout<<"\nCodigo ja existente!";
while (pr.codigo !=0) {
cout<<"\nNome do Produto: ";
gets (pr.nome);
cout<<"\nQuanti dade do Produto em estoque: ";
cin>> pr.estoque;
cout<<"\nDigite o genero do Produto: ";
gets (pr.genero);
cout<<"\nDigite o preco do Produto: ";
cin>> pr.preco;
fwrite (&pr,sizeof (Material),1,pa rq);
cout << "\n\nCadast ro efetuado com sucesso!";
getch();
clrscr();
cout<<"\n\nDigi te o codigo do Produto <0 para sair>: ";
cin>> pr.codigo;
}
fclose(parq);
}
}

//funcao para listar os livros e revistas
void listar() {

Material pr;
FILE * parq;

parq = fopen("material .dat", "rb");
if (parq==NULL)
{
cout<<"\nERRO na abertura do Arquivo para leitura";
getch();
}
else
{
clrscr();
cout<<"\nLISTAG EM DE LIVROS E REVISTAS ";
cout<<"\n-------------------------------";
cout<<"\n\n0 - Voltar";
cout<<"\n-----------";
fread(&pr,sizeo f(Material),1,p arq);
while (!feof(parq))
{
cout<<"\n";
cout<<"\nCodigo : " <<pr.codigo;
cout<<"\nNome: " <<pr.nome;
cout<<"\nEstoqu e: " <<pr.estoque;
cout<<"\nGenero : "<<pr.gener o;
cout<<"\nPreco: " <<pr.preco;
fread(&pr,sizeo f(Material),1,p arq);
}
getch();
}
fclose(parq);
}


//funcao para pesquisar os livros e revistas
void pesquisar() {

Material pr;
FILE * parq;
int cod, flag, opt;
char nomprod[20], resp[3], genero[20];

parq = fopen("material .dat", "rb");
if (parq==NULL)
{
cout<<"\nERRO na abertura do Arquivo para leitura";
getch();
}
else
{
clrscr();
cout<<"\nPESQUI SA DE LIVROS E REVISTAS";
cout<<"\n-----------------------";
cout<<"\nEscolh a o tipo de pesquisa";
cout<<"\n\n0 - Voltar";
cout<<"\n1 - Nome";
cout<<"\n2 - Codigo";
cout<<"\n3 - Genero";
cout<<"\n";
cin>>opt;

if (opt == 2) {
cout<<"\n\nDigi te o codigo a ser pesquisado: ";
cin >> cod;
flag = 0;
fread(&pr,sizeo f(Material),1,p arq);
}
while (!feof(parq))
{
if (cod == pr.codigo)
{
cout<<"\nCodigo : " <<pr.codigo;
cout<<"\nNome: " <<pr.nome;
cout<<"\nEstoqu e: " <<pr.estoque;
cout<<"\nGenero : " <<pr.genero;
cout<<"\nPreco: " <<pr.preco;
flag = 1;
//cout<<"\nDeseja pesquisar outro produto?";
}
fread(&pr,sizeo f(Material),1,p arq);
}

if (opt == 1) {
cout<<"\n\nDigi te o nome a ser pesquisado: ";
gets(nomprod);
flag = 0;
fread(&pr,sizeo f(Material),1,p arq);
while (!feof(parq))
{
if (strcmp(nomprod , pr.nome))
{
cout<<"\nCodigo : " <<pr.codigo;
cout<<"\nNome: " <<pr.nome;
cout<<"\nEstoqu e: " <<pr.estoque;
cout<<"\nGenero : "<<pr.gener o;
cout<<"\n\nPrec o: " <<pr.preco;
flag = 1;
//cout<<"\nDeseja pesquisar outro produto?";
}
fread(&pr,sizeo f(Material),1,p arq);
}
}

if (opt == 3) {
cout<<"\n\nDigi te o genero do produto: ";
gets(genero);
flag = 0;
fread(&pr,sizeo f(Material),1,p arq);
}
while (!feof(parq))
{
if (!strcmp(genero , pr.genero))
{
cout<<"\nCodigo : " <<pr.codigo;
cout<<"\nNome: " <<pr.nome;
cout<<"\nEstoqu e: " <<pr.estoque;
cout<<"\nGenero : "<<pr.gener o;
cout<<"\n\nPrec o: " <<pr.preco;
flag = 1;
//cout<<"\nDeseja pesquisar outro produto?";
}
fread(&pr,sizeo f(Material),1,p arq);
}

if (flag == 0)
cout<<"\nProdut o nao cadastrado";
getch();
fclose(parq);

}
}


See you guys!! Tks a lot !!
May 28 '07 #1
2 2466
gustavoprogrammer
2 New Member
One more question...

Does anybody know how to implement an alter to this data?


See u !! Tka a lot one more time...
May 28 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
If the file has no particular structure, then the easiese thing is to read the entire file into the program and then rewrite the data without the deleted parts.

Otherwise, you need a flat file: a) records all same length,b) all records in identical format. If you have this,, you can fseek() to the particular record to chnage and then fwrite(). Note that if all the reocrds have the same length and the same format, you can fseek() to a particualr field and just change that.

Unless you completely read and rewrite the file, there is no way to complete;y delete data. Deleting in place would require you do define a "deleted character" andf to fill deleted records with these characters. All functions reading and wrirting the file would need to be sensitive to this.
May 28 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3921
by: matt | last post by:
I have compiled some code, some written by me, some compiled from various sources online, and basically i've got a very simple flat file photo gallery. An upload form, to upload the photos and give them a caption, storing the caption and filename in a text file. It's a bit buggy when removing the photos and captions from the file, and also in displaying them on the delete page. you can see it in action at www.4am.com.au/gallery/upload.php...
8
3011
by: MattP | last post by:
Ok, with the help of some examples found on the web and some minor modifications on our own, we have a simple and working encrypt and decrypt solution. It runs as a service, watches for files with a specific extension in a specific directory. The files are uploaded by FTP to this directory. The service then does the following steps: 1) Verify it can open the file (so we know it's fully uploaded). 2) Try to decrypt the file with known...
6
2871
by: Jamal | last post by:
I am working on binary files of struct ACTIONS I have a recursive qsort/mergesort hybrid that 1) i'm not a 100% sure works correctly 2) would like to convert to iteration Any comments or suggestion for improvements or conversion to iteration would be much appreciated
3
1907
by: Joe | last post by:
Hi, I have written a webpage that allows a user to delete files in asp.net with I am having a small problem. To access this page a user has to login via login.aspx page. After successful login, user is directed to a page called view.aspx which shows the user the files in a directory and allows them to delete the files. This page has a data grid having 4 columns - delete button, File Name, Last Write Time and File Size. When a user...
6
4984
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing for long running reports. When the processing is complete it uses crystal reports to load a template file, populate it, and then export it to a PDF. It works fine so far....
1
3704
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am attach this script files and inq files. I cant understand this error. Please suggest me. You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am online. Plz....Plz..Plz...
46
2510
by: Bruce W. Darby | last post by:
This will be my very first VB.Net application and it's pretty simple. But I've got a snag in my syntax somewhere. Was hoping that someone could point me in the right direction. The history: My work involves creating custom packages of our software product for golf courses that purchase our software. The course data is kept as a back up in the event the course needs us to replace their custom files. Each course has a folder of it's own...
0
4011
by: wb5plj | last post by:
Hi I am having a problem passing some sql to the db2cmd via ant. This is very confusing as I am doing this exact thing elseware with no problem (just differant sql, and I have verified the sql is valid) but in this one I am having problems. Any time I run this I get the folowing message from the db2 clp window that opens up. SQL0104N An unexpected token "SET" was found following "<identifier>". Expected tokens may include: "NEW". ...
2
4174
by: =?Utf-8?B?SnJ4dHVzZXIx?= | last post by:
I just started using Windows Live OneCare, I had been using Norton, but was unable to fix the problems I was having. I have yet been unsuccessful with OneCare as well. I keep getting the same warning from OneCare, one is for Adware, the other is for a trojan, I clean both, but almost immediatly, I get the same warning? My Windows Defender is also shut down, not by me as I have no idea how to do this(or to turn it back on), but am still...
2
2569
by: muppetjones | last post by:
I have been receiving a "Floating point exception" from Perl after my program has run several iterations. I have not been able to pin point the location of the error, and I am clueless. Especially as I can't seem to find exactly what this exception means. 1) What is a floating point exception? 2) What causes it? 3) Is there any way to fix it? I have included my code below, but as there is quite a bit any suggestion of how to handle...
0
8275
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
8792
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
7294
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...
1
6157
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4143
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
4280
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1905
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
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.