473,473 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Binary File: Writing and Reading

1 New Member
Hi!
^.^
I am making a program but I can't continue because there's something wrong...

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<string>
  3. #include<fstream>
  4.  
  5. using namespace std;
  6.  
  7. struct record{
  8.   int IdentificationNumber;
  9.   string Name;
  10.   int Quantity;
  11.   double Cost;
  12. };
  13.  
  14. main(){
  15.   record TheTool;
  16.   fstream file("hardware.dat", ios::in|ios::out|ios::binary);
  17.   if(!file){
  18.     cerr<<"File could not be opened or created"<<endl;
  19.     exit(1);    
  20.   }
  21.   cout<<"I Will Add Something To The File"<<endl;
  22.   cout<<"Enter tool ID Number: ";
  23.   cin>>TheTool.IdentificationNumber;
  24.   cout<<"Enter tool name: ";
  25.   cin.ignore();
  26.   getline(cin, TheTool.Name);
  27.   cout<<"Enter Quantity: ";
  28.   cin>>TheTool.Quantity;
  29.   cout<<"Enter Cost: ";
  30.   cin>>TheTool.Cost;
  31.   file.seekp((TheTool.IdentificationNumber-1)*sizeof(record));
  32.   file.write(reinterpret_cast<const char *> (&TheTool), sizeof(record));
  33.   file.close();
  34.   fstream file1("hardware.dat", ios::in|ios::out|ios::binary);
  35.   file1.seekg(0);
  36.   file1.read(reinterpret_cast<char*>(&TheTool),sizeof(record));
  37.   while(file1.good()){
  38.     cout<<TheTool.IdentificationNumber<<TheTool.Name<<TheTool.Quantity<<TheTool.Cost<<endl;
  39.     file1.read(reinterpret_cast<char*>(&TheTool),sizeof(record));
  40.   }
  41. }
I want to save it to a binary file and if I exit the program and I open it again, the previous input will still be there. But when I run the program the second time, and read its contents, the name of the tool is replaced with my new input and I don't know why or how to stop this from happening. Also, when the tool identification number is 1,2,3,... (input in proper order), it reads ok(except the name) and runs until it reaches its end. But when the number is in 1,2,3,5 (there's no 4 so not in proper order of input), it will encountered an error. But I want to be able to do that. Please help... Thanks!

(Sorry for the wrong grammar and not nice explanation, i'm not good in english hehe.. thank you!)
Jan 29 '08 #1
1 4614
weaknessforcats
9,208 Recognized Expert Moderator Expert
You are reading and writing sizeof(record).

Are you saying that arecord object with an name of:

ADADADADADADADADADADADADDADADADADADADDADADADADADA

is the same size as a record object with a name of

A

???

Your string member probably as the string data on the heap so writing the sizeof(record) just writes the part of the string that's on the stack.

You need to prepare a data buffer that contains all of the member data in a format where the number of bytes is constant. Then read/write the sizeof that buffer.

On the read, you will need to parse the buffer back into a record object.
Jan 29 '08 #2

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

Similar topics

6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
103
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it...
5
by: Neo | last post by:
Hello: I am receiving a Binary File in a Request from a application. The stream which comes to me has the boundary (Something like "---------------------------39<WBR>­0C0F3E0099" without the...
4
by: knapak | last post by:
Hello I'm a self instructed amateur attempting to read a huge file from disk... so bear with me please... I just learned that reading a file in binary is faster than text. So I wrote the...
26
by: Patient Guy | last post by:
Has anyone written code that successfully manipulates binary file data using Javascript? It might---and in the case of doing I/O, will---make use of browser- specific functions (ActiveX/COM with...
7
by: John Salerno | last post by:
In C#, writing to a binary file wrote the actual data types into the file (integers, etc.). Is this not how Python binary files work? I tried to write integers into a file, but the write method...
7
by: smith4894 | last post by:
Hello all, I'm working on writing my own streambuf classes (to use in my custom ostream/isteam classes that will handle reading/writing data to a mmap'd file). When reading from the mmap...
15
by: JoeC | last post by:
I am writing a program that I am trying to learn and save binary files. This is the page I found as a source: http://www.angelfire.com/country/aldev0/cpphowto/cpp_BinaryFileIO.html I have...
3
by: masood.iqbal | last post by:
Hi, Kindly excuse my novice question. In all the literature on ifstream that I have seen, nowhere have I read what happens if you try to read a binary file using the ">>" operator. I ran into...
6
by: zl2k | last post by:
hi, there I have a appendable binary file of complex data structure named data.bin created by myself. It is written in the following format: number of Data, Data array Suppose I have...
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...
0
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...
0
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.