Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Code below









C++
void Staff::Delete() {
	char tgtIC[15];
	Staff objStaff;
	fstream iofile( "Employee.dat",ios::in || ios::out);
	if (!iofile) 				// overloaded ! operator 
	{ 														// or: if( outClientFile.fail() ) 
		cerr << "\t File could not be opened" << endl; 
		system( "PAUSE" ); 
		exit( 1 ); 					// prototype in cstdlib 
	} 
	fflush(stdin);				// To remove any previous content stored in memory
	cout<<endl;
	cout<< "\t ==================================================" 
	cout<< "\t < DELETE A RECORD >" <<endl;
	cout<< "\t Enter IC that you want to delete : " 
	cout<< "\t ==================================================" <<endl;

	cout<< "\t |      Enter Staff IC : " ;
	cin.getline(tgtIC, 15);
//==================================================" <<endl;

	iofile.read((char *) (&objStaff), sizeof(Staff));
	while (!iofile.eof())
	{	//cout<< "t :" <<tgtIC<< "obj : " <<objStaff.IC<<endl;
		if (!strcmp(tgtIC,objStaff.IC)==0)
			display(objStaff);
		else 
			cout<< "\t Delete record - by writing a blank record if using random file" <<endl;
		iofile.read((char *) (&objStaff), sizeof(Staff));
	} 
	iofile.close(); // explicit closing 
}
Posted
Updated 14-Jul-10 22:58pm
v5
Comments
Sauro Viti 14-Jul-10 13:51pm    
For a better look and to make it more readable, please wrap your code in a <PRE> block

Try using the debugger.
 
Share this answer
 
Comments
hmf1 14-Jul-10 12:32pm    
Error list display no error... so, i dunno where is error....
Sauro Viti 14-Jul-10 13:49pm    
The error list display compile and link errors! What John mean is: run your application under the debugger, place a breakpoint at the beginning of your Delete() method, then proceed step-by-step and watch variables, files, etc. to understand what's happening
iofile.read((char *) (&objStaff), sizeof(Staff));


Just doesn't sit right with me. You definitely should run through the debugger. You are casting a staff object as a character array I would wager that sizeOf(Staff) is returning a number greater than the array your cast represents. Causing your error.
 
Share this answer
 
Comments
Niklas L 14-Jul-10 17:05pm    
This is common practise under the right circumstances (no contained pointers / byte alignment policy / byte order policy / etc)
Are you mixing text mode and binary mode?

I suggest you use the ios::binary flag combined with in and out in your stream constructor.

Can't really tell for sure, since your code is a bit messed up and does not look complete due to mismatched quotes (and what else?)

Also, declare variables in the correct scope. The constructor of objStaff is run even if the stream opening fails. Declare it as close to the action as possible. Make that a habit. (There are exceptions however when dealing with loops)
 
Share this answer
 
A couple of things spring to mind straight away:

- The code's way too complicated. Try something like rendering your objects to something that's human readable (XML if you're a masochist, CSV or space delimited otherwise)

- If you do that you can implement insertion and extraction operators for your class. Once you've done that you'll be able to enter a staff member "raw" from the console, from a file or even from a string

- Then you can use things like std::string to simplify your comparison of the staff member's name

- iofile.close() is completely pointless. You're programming in C++, fstream has a destructor

- fflush(stdin) may or may not have any effect on std::cin - std::cin typically uses another level of buffering over stdio handles. Don't mix stdio with iostreams until you're sure you know what you're up to

So the message here is partition your code a bit better, abandon using all the C guff that's infecting your code (strcmp, declarations miles away from where they're used, exit()) and the binary file and then the problem might show up.

Okay, I lied, that was more than a couple of things...

Cheers,

Ash
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900