![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 215
Rep Power: 3
![]() |
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. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
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? |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 215
Rep Power: 3
![]() |
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. ![]() |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|