Hello,
I've got the following code: - wxString path = filepath;
-
wxString newpath = filepath;
-
fstream f(path.Append("/tests/tests.bin"), ios::in | ios::binary);
-
fstream fnew(newpath.Append("/tests/temp.bin"), ios::out | ios::binary);
-
Test* transferTest = new Test();
-
int thisSize = sizeof(Test);
-
bool success = false;
-
bool toBeDeleted = false;
-
int testNo = 1;
-
if (f)
-
{
-
f.seekg(0, ios::beg);
-
fnew.seekp(0, ios::beg);
-
while (testNo <= noOfTests)
-
{
-
toBeDeleted = false;
-
for (int i = 0; i < noOfSelectedRows; i++)
-
{
-
int row = selectedRows[i];
-
if (testNo == row)
-
{
-
toBeDeleted = true;
-
}
-
}
-
if (!toBeDeleted)
-
{
-
f.read(reinterpret_cast<char *>(transferTest), thisSize);
-
fnew.write(reinterpret_cast<char *>(transferTest), thisSize);
-
fnew.seekp(testNo*thisSize, ios::beg);
-
}
-
f.seekg(testNo*thisSize, ios::beg);
-
testNo++;
-
}
-
success = true;
-
f.close();
-
fnew.close();
-
remove(path.c_str());
-
rename(newpath.c_str(), path.c_str());
-
}
-
delete transferTest;
Essentially, what I have is a binary file with objects of the class Test. Trough a list in my GUI I make it possible to delete objects from this bunch. And it works fine as long as I delete one or more objects at the end of the file. However if I try to delete an object in the middle of the bunch, there becomes a "hole" in the "temp.bin" file and it keeps on writing the rest of the objects perfectly fine. So the file gets the exact same size as before.
I've been staring at this for a while now and I really can't see why it doesn't work the way I want it to, so hopefully someone can help :)
Thanks in advance!
2 1890 Banfa 9,065
Recognized Expert Moderator Expert
I am going to take a stab in the dark and guess that the whole in your new file appears at the location after the entry to be deleted.
The problem is this line of code - fnew.seekp(testNo*thisSize, ios::beg);
It sets the file pointer for the new file based on the current test not the number of tests that have been written to the file.
In theory this seekp (and the seekg at line 31) are just not required, a file is read or written sequencially you only need to seek if you want to move the file pointer to an out of sequence position.
-
f.seekg(testNo*thisSize, ios::beg);
-
testNo++;
-
You apply this with incrementing testNo each iteration, whether item was written or not. Write function should handle seek after write automatically, try to comment out all seek()'s.
Btw, writing classes ( unless they are POD objects ) as memory snapshot is no a good idea.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Jon Hyland |
last post by:
Hi all,
I'm looking for the fastest way to write (and/or read) binary to a
file in VC++. I've been using the fstream object in this way:
//unsigned char *pDataOut and long iLength initilized somewhere..
fstream file_out((const char *)pszOutFile,
ios::in|ios::trunc|ios::binary);
|
by: anirudhvr |
last post by:
Hi,
I needed a basic binary to ascii encoder, so I wrote this piece of
code:
/*
Encoding algo: suppose 11111010 is the byte to be encoded.
It is first broken up into two 4-bit parts (1111 and 1010)
This 4-bit quantity is then padded between a 01 and a 10, ie,
01xxxx10 will be written out to the output file.
|
by: Ching-Lung |
last post by:
Hi all,
I try to create a tool to check the delta (diff) of 2
binaries and create the delta binary. I use binary
formatter (serialization) to create the delta binary. It
works fine but the delta binary is pretty huge in size. I
have 1 byte file and 2 bytes file, the delta should be 1
byte but somehow it turns out to be 249 bytes using binary
formatter. I guess serialization has some other things
added to the delta file.
|
by: John R. Delaney |
last post by:
I am running in debugging mode after a clean C++ compilation under .NET 2003. In a BIG loop (controlled many levels up in the call stack), I open a file with fopen using the "a" option. Then I write 23 doubles to it with fwrite, one call for each double. Then I close the file using fclose. After three times around the loop in the debugger, I stop the program (using "Stop debugging"). That is writing 552 bytes.
The resulting file's properties...
|
by: John Dann |
last post by:
I'm trying to read some binary data from a file created by another
program. I know the binary file format but can't change or control the
format. The binary data is organised such that it should populate a
series of structures of specified variable composition.
I have the structures created OK, but actually reading the files is
giving me an error. Can I ask a simple question to start with:
I'm trying to read the file using the...
| |
by: UnknownBlue |
last post by:
Hi.
I am trying to read .dat files which is in binary using visual C++ 6.0. I want to display the first 100 char (in hex). But instead some weird char are being display. I opened the file through hex editor and this is what i am supposed to get: 2EFACCF69FFC84...
Can anyone help me in this? Thanks in advance!
char *memblock;
ifstream::pos_type size;
ifstream file;
|
by: Harlin Seritt |
last post by:
Hi...
I would like to take a string like 'supercalifragilisticexpialidocius'
and write it to a file in binary forms -- this way a user cannot read
the string in case they were try to open in something like ascii text
editor. I'd also like to be able to read the binary formed data back
into string format so that it shows the original value. Is there any
way to do this in Python?
Thanks!
|
by: nguser3552 |
last post by:
Hello Everyone,
I have a problem I can't surmount, anything is gravy at this point.
I need to be able to read any type of file .ext (mov,mpeg,mp3,etc) in binary
format. I can do this in C, but have to use:
int main(int argc,char** argv)
which is archaic.
My compiler is Visual Studio 5.0 Pro.
To make my dilemma as clear as possible:
|
by: Canned |
last post by:
Hi,
I'm trying to write a class that can convert ascii to binary and vice
versa. I write my class based on this function I've found on internet
That works perfectly, but when I try to implement it in my own class it
gives me alot of headache, also because I'm totally new to the language.
It work only with one character at a time, and if I give a string it
just give some weird result.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |