473,770 Members | 6,348 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Binary file I/O, weird problem

35 New Member
Hello,
I've got the following code:
Expand|Select|Wrap|Line Numbers
  1.  wxString path = filepath;
  2.  wxString newpath = filepath;
  3.  fstream f(path.Append("/tests/tests.bin"), ios::in | ios::binary);
  4.  fstream fnew(newpath.Append("/tests/temp.bin"), ios::out | ios::binary);
  5.  Test* transferTest = new Test();
  6.  int thisSize = sizeof(Test);
  7.  bool success = false;
  8.  bool toBeDeleted = false;
  9.  int testNo = 1;
  10.  if (f)
  11.  {
  12.   f.seekg(0, ios::beg);
  13.   fnew.seekp(0, ios::beg);
  14.   while (testNo <= noOfTests)
  15.   {
  16.    toBeDeleted = false;
  17.    for (int i = 0; i < noOfSelectedRows; i++)
  18.    {
  19.     int row = selectedRows[i];
  20.     if (testNo == row)
  21.     {
  22.      toBeDeleted = true;
  23.     }
  24.    }
  25.    if (!toBeDeleted)
  26.    {
  27.     f.read(reinterpret_cast<char *>(transferTest), thisSize);
  28.     fnew.write(reinterpret_cast<char *>(transferTest), thisSize);
  29.     fnew.seekp(testNo*thisSize, ios::beg);
  30.    }
  31.    f.seekg(testNo*thisSize, ios::beg);
  32.    testNo++;
  33.   }
  34.   success = true;
  35.   f.close();
  36.   fnew.close();
  37.   remove(path.c_str());
  38.   rename(newpath.c_str(), path.c_str());
  39.  }
  40.  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!
Feb 2 '09 #1
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

Expand|Select|Wrap|Line Numbers
  1.     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.
Feb 2 '09 #2
newb16
687 Contributor
Expand|Select|Wrap|Line Numbers
  1.  f.seekg(testNo*thisSize, ios::beg); 
  2.  testNo++; 
  3.  
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.
Feb 2 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3494
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);
2
3302
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.
9
6520
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.
3
2128
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...
7
6063
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...
7
2346
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;
29
5087
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!
3
3198
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:
5
2586
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.
0
9591
marktang
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...
0
9425
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,...
0
10225
Oralloy
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...
0
10053
jinu1996
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...
1
7415
isladogs
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...
0
5312
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...
1
3969
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
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.