473,385 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Binary file I/O, weird problem

35
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 1872
Banfa
9,065 Expert Mod 8TB
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 512MB
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
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...
2
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...
9
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...
3
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...
7
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...
7
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...
29
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...
3
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 ...
5
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.