Join 300,490 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,820 people online right now. Registration is fast and FREE... Join Now!
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!!
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
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.
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.
//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 */
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?
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 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.
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: