Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 2nd, 2006, 12:07 PM   #1
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 215
Rep Power: 3 Seif is on a distinguished road
writing to file wrong?

hey can anyone tell me whats wrong with the following code? its meant to find the selected struct and overwrite the file. problem is, it seems to keep writing to the txt file, last time i tried it i ended up with a txt file 819MB in size lol!

 void removeRecords(int record)
{

	 char empty[10] = "empty";

	ofstream database("vehicles.txt",ios::in);
	if(!database)
		{
			 cerr << "File could not be opened\n";
		}
	else
		{
			database.seekp((record - 1)*sizeof(vehicle),ios::beg);
	
				strcpy(newvehicle.regNumber,empty);
 				strcpy(newvehicle.make,empty);
				strcpy(newvehicle.model,empty);
				strcpy(newvehicle.datePurchased,empty);
				strcpy(newvehicle.dateServiced,empty);
				newvehicle.mileage = 0;
				strcpy(newvehicle.location,empty);
					
				database.write(reinterpret_cast<const char*> (&newvehicle), sizeof(vehicle));
				database.close();
		}
}

am looking at fstream commands now, but cant see whats wrong.
Seif is offline   Reply With Quote
Old Feb 2nd, 2006, 5:02 PM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
IIRC, ostream::write() accepts two arguments, not one as you have it. The first is the pointer to the buffer to output. The second is the number of characters to be output.

Also check that your "vehicle" type contains actual arrays of char, not pointers.
grumpy is offline   Reply With Quote
Old Feb 2nd, 2006, 5:16 PM   #3
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
A couple of other possibilities:
- Maybe the record parameter value is incorrect or corrupt, or possibly even just zero. This would make the program try to write a very large file.
- Maybe the problem isn't in this function - is it being called in a loop?
The Dark is offline   Reply With Quote
Old Feb 2nd, 2006, 5:22 PM   #4
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 215
Rep Power: 3 Seif is on a distinguished road
hi thanks for the help. i managed to get it working in the end i did the following

firstly i changed ofstream ("vehicles.txt",ios::in) to ios::in|ios::out and seekg instead of seekp. seems to be working ok now. thanks guys.
Seif is offline   Reply With Quote
Old Feb 2nd, 2006, 8:31 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Be aware that using text mode in Windows can lead to problems with correct performance of seeks and tells. This is not a problem in *nix systems because they only use binary mode. If you plan to use seek/tell and append in Windows, I suggest you use binary mode (binary mode works fine with text, as text is just binary codes). You can accomplish this with a ios:binary combined with your ios:in and/or ios:out. You may also set a global variable to specify that binary is the default mode (text mode is normally the default).

There are a number of things associated with text mode that are explained in the documentation if you look for it.

Incidentally, if you disable smilies in posts you won't get the wierd little faces in conjunction with your colons and adjacent symbols. You may turn them back on when you don't have such sequences of characters.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:48 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC