Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,308 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 824 people online right now. Registration is fast and FREE... Join Now!




deletion of data from binary file

 
Reply to this topicStart new topic

deletion of data from binary file

dan_ram
15 Nov, 2008 - 07:04 AM
Post #1

D.I.C Head
Group Icon

Joined: 15 Aug, 2007
Posts: 141



Thanked: 4 times
Dream Kudos: 50
My Contributions
there is some problem with this code..i just cant figure it out...
i'm deleting employees from the file emp.dat. but after running this function, when i view details again, the employee is not getting deleted..
in short, deletion is not working!!
cpp
int view_details(){
clrscr();
gotoxy(27,10);
cout<<"enter employee number:";
cin>>emp_id;
EMPLOYEE s; //struct containing employee details
ifstream f("emp.dat",ios::binary);
f.seekg(0,ios::end);
int t=f.tellg();
t=t/sizeof(s);
f.seekg(sizeof(s)*(emp_id-1));
f.read((char *)&s, sizeof(s));
if((strcmpi(s.name,"n/a")!=0)&&(strcmpi(s.fname,"n/a")!=0)
&&(strcmpi(s.dep,"n/a")!=0)&&emp_id<=t){
cout<<"name:"<<s.name<<endl;
cout<<"father's name:"<<s.fname<<endl;
cout<<endl<<"address:"<<s.addr<<endl;
cout<<endl<<"phone number:"<<s.phno<<endl;
cout<<endl<<"sex m/f:"<<s.s<<endl;
cout<<endl<<"date of birth:"<<s.dob[0]<<"/"<<s.dob[1]<<"/"<<s.dob[2]<<endl;
cout<<endl<<"department:"<<s.dep<<endl;
cout<<endl<<"salary:"<<s.sal<<endl;
cout<<"PRESS ANY KEY TO EXIT"<<endl;
}
else{
cout<<"invalid employee number"<<endl;
return 1;
}
f.close();
return 0;
}
void del_emp(){
clrscr();
char ch='y';
EMPLOYEE s; //struct containing employee details
char chk;
fstream f("emp.dat",ios::binary);
while(ch=='y'||ch=='Y'){
if(view_details()!=1){
strcpy(s.name,"n/a");
strcpy(s.fname,"n/a");
strcpy(s.addr,"n/a");
strcpy(s.dep,"n/a");
s.s='i';
s.phno=0;
s.sal=0;
s.dob[0]=s.dob[1]=s.dob[2]=0;
cout<<"confirm delete?y/n:";
cin>>chk;
if(chk=='y'||chk=='Y'){
f.seekp(sizeof(s)*(emp_id-1));
f.write((char*)&s,sizeof(s));
cout<<"DELETED"<<endl;
f.close();
}
}
cout<<"wish to delete another employee? y/n:";
cin>>ch;
}
cout<<"PRESS ANY KEY TO EXIT";
getch();
main_menu();
}


view_details() is working properly normally..
PLS HELP...DO SUGGEST ANY ALTERATIONS

User is offlineProfile CardPM
+Quote Post


polymath
RE: Deletion Of Data From Binary File
15 Nov, 2008 - 07:47 AM
Post #2

D.I.C Addict
Group Icon

Joined: 4 Apr, 2008
Posts: 614



Thanked: 17 times
Dream Kudos: 500
My Contributions
Where do you get the employee's ID? I see no i/o (beyond "Delete another employee") and the Employee is uninitialized. Both functions have no args, so i can see what you're trying to get but i can't see where you are looking through the file to find the employee with the correct ID. If you could post your FULL code that'd be helpful.

EDIT: I see now, it's in the other function... tricky. give me a sec

EDIT2: emp_id is a global var i'm assuming.

EDIT3: Because of the limitations of fstream you can not overwrite data using the write function, it simply moves back the older data. Correct me if i'm wrong.

This post has been edited by polymath: 15 Nov, 2008 - 07:52 AM
User is offlineProfile CardPM
+Quote Post

dan_ram
RE: Deletion Of Data From Binary File
15 Nov, 2008 - 07:53 AM
Post #3

D.I.C Head
Group Icon

Joined: 15 Aug, 2007
Posts: 141



Thanked: 4 times
Dream Kudos: 50
My Contributions
this is the employee input function..the employee id is automatically allotted.
(variable emp_id is in global scope)
cpp
void new_emp(){
EMPLOYEE s;
char chk='y';
while(chk=='y'||chk=='Y'){
fstream f("emp.dat",ios::binary);
f.seekg(0,ios::end);
int m=f.tellg()/sizeof(s)+1; //(why?)
s.accept();
f.seekp(sizeof(s)*(m-1));
f.write((char *)&s, sizeof(s));
cout<<endl<<"the employee number is:"<<m<<endl;
cout<<"wish to add another employee? y/n";
cin>>chk;
f.close();
}
main_menu();
}


oh....so what should i do to change the value in file?
User is offlineProfile CardPM
+Quote Post

polymath
RE: Deletion Of Data From Binary File
15 Nov, 2008 - 07:55 AM
Post #4

D.I.C Addict
Group Icon

Joined: 4 Apr, 2008
Posts: 614



Thanked: 17 times
Dream Kudos: 500
My Contributions
In the file, do you have the N/A entries in the correct position but have the old entry immediatly following?
User is offlineProfile CardPM
+Quote Post

dan_ram
RE: Deletion Of Data From Binary File
15 Nov, 2008 - 07:58 AM
Post #5

D.I.C Head
Group Icon

Joined: 15 Aug, 2007
Posts: 141



Thanked: 4 times
Dream Kudos: 50
My Contributions
it is a binary file..so cant see...
but when i view the details of all employees(tats another function),i cant see the "n/a" entry at all..only the employee details whom i entered are seen.
User is offlineProfile CardPM
+Quote Post

polymath
RE: Deletion Of Data From Binary File
15 Nov, 2008 - 08:07 AM
Post #6

D.I.C Addict
Group Icon

Joined: 4 Apr, 2008
Posts: 614



Thanked: 17 times
Dream Kudos: 500
My Contributions
that would be your problem. You need to read the entire file into memory and then overwrite the entire file. To do this open the file in an ifstream, read it into memory, manipulate, and then output.
cpp

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;


//your code would look sommat like
ifstream fin;
ofstream fout;
vector<string> filecontents;
fin.open("filename", fstream::binary);
/*
here you read the whole file into memory
i would suggest using the string library with
#include<string> and the STL vectors lib
with #include <vector> and then using the
getline function and a loop to grab a temp
string, check if it's at the EOF, and if not,
push_back the data into filecontents
*/
fin.close();
//modify the vector
fout.open("filename", fstream::trunc | fstream::binary);
/*
here you would simply need to fout all of the stuff, i'm sure
you can figure that out based on the code you have
*/

User is offlineProfile CardPM
+Quote Post

dan_ram
RE: Deletion Of Data From Binary File
15 Nov, 2008 - 08:13 AM
Post #7

D.I.C Head
Group Icon

Joined: 15 Aug, 2007
Posts: 141



Thanked: 4 times
Dream Kudos: 50
My Contributions
i use turbo C++ version 3 which does not support vectors.. sad.gif
User is offlineProfile CardPM
+Quote Post

AntiBNI
RE: Deletion Of Data From Binary File
15 Nov, 2008 - 08:38 AM
Post #8

D.I.C Head
**

Joined: 21 Jul, 2006
Posts: 62


My Contributions
QUOTE(polymath @ 15 Nov, 2008 - 08:07 AM) *

that would be your problem. You need to read the entire file into memory and then overwrite the entire file. To do this open the file in an ifstream, read it into memory, manipulate, and then output.

Are you serious? blink.gif

What if the file is for really BIG database and it's like 100MBs big?
That app will be a memory hog,instead of reading directly to memory.

You need to only create a temp file and name it what ever you want,read a string from the old file (one at a time) and compare then write it to the temp file.

Every line you get from old file will get compared to see if the name of the employee ur looking for has been readed,if it is then skip reading all the info and stuff about him.
and continue on with the next employee.

That way is easier,faster,better performance (since you won't use a lot of memory),you can use it to not only delete but edit and add info too. and best of all "easier to code".

I have done it that way and it works flawless.

User is offlineProfile CardPM
+Quote Post

polymath
RE: Deletion Of Data From Binary File
15 Nov, 2008 - 08:42 AM
Post #9

D.I.C Addict
Group Icon

Joined: 4 Apr, 2008
Posts: 614



Thanked: 17 times
Dream Kudos: 500
My Contributions
@AntiBNI

I would agree with you on everything except the ease of code factor. You would still need to read each chunk into memory, but instead of having everything in the same position you would have only one portion of the data in memory at a time, which works. There is a performance benefit, though there is a downside in that to copy the temp file you would need to use system-dependant calls (though that does not seem to be an issue here)

In short, you're right, just that was the first thing i thought of at the time, so that's what i wrote.

It's the same principal, just you're redirecting the output to another file and only reading chunks at a time. The main error in the code is still the write function.
User is offlineProfile CardPM
+Quote Post

KYA
RE: Deletion Of Data From Binary File
15 Nov, 2008 - 11:48 AM
Post #10

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 9,508



Thanked: 363 times
Dream Kudos: 2550
Expert In: C, C++, Java

My Contributions
QUOTE(dan_ram @ 15 Nov, 2008 - 09:13 AM) *

i use turbo C++ version 3 which does not support vectors.. sad.gif


Get a compiler/IDE that supports STL use. Otherwise you'll just end up reinventing the wheel.
User is offlineProfile CardPM
+Quote Post

dan_ram
RE: Deletion Of Data From Binary File
16 Nov, 2008 - 07:09 AM
Post #11

D.I.C Head
Group Icon

Joined: 15 Aug, 2007
Posts: 141



Thanked: 4 times
Dream Kudos: 50
My Contributions
thanks a lot for your efforts to look into my program, polymath,antibni and kya..
unfortunately i have 2 use only this compiler...
but then i figured out the problem myself..and im putting up the code:
CODE
void del_emp(){
   clrscr();
   cout<<"enter employee number:";
   cin>>emp_id;
   long recpos;
   char ch='y';
   EMPLOYEE s;
   char chk;
   fstream f("emp.dat",ios::binary|ios::in|ios::out);
   f.seekg((emp_id-1)*sizeof(s),ios::beg);
   recpos=f.tellg();
   while(ch=='y'||ch=='Y'){
     if(view_details()!=1){
       strcpy(s.name,"n/a");
       strcpy(s.fname,"n/a");
       strcpy(s.addr,"n/a");
       strcpy(s.dep,"n/a");
       s.s='i';
       s.phno=0;
       s.sal=0;
       s.dob[0]=s.dob[1]=s.dob[2]=0;
       cout<<"confirm delete?y/n:";
       cin>>chk;
       if(chk=='y'||chk=='Y'){
     f.seekp(recpos,ios::beg);
     f.write((char*)&s,sizeof(s));
     cout<<"DELETED"<<endl;
     f.close();
       }
     }
     cout<<"wish to delete another employee? y/n:";
     cin>>ch;
   }
   cout<<"PRESS ANY KEY TO EXIT";
   getch();
   main_menu();
}
void main(){
  main_menu();
}


This post has been edited by dan_ram: 16 Nov, 2008 - 07:10 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 7/4/09 07:01PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month