473,385 Members | 1,893 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.

saving Objects on binary file

4
Can any one help me to fix this code? the result is Garpish>>

Expand|Select|Wrap|Line Numbers
  1. <iostream>
  2. #include<fstream>
  3. using namespace std;
  4. class Student
  5. {
  6. public:
  7.     char name[10];
  8.     char address[10];
  9.     char Gender;
  10.     char DOB[10];
  11.     Student()
  12.     {}
  13. };
  14. int main()
  15. {
  16.     cout<<"\nWritting on file \n";
  17.         Student *p=new Student[5];
  18.             for(int i=0;i<5;i++)
  19.     {
  20.         cout<<i+1<<": ";
  21.         cin>>p->name;
  22.         cout<<"\n";
  23.         p++;
  24.         }
  25.     ofstream osfile("Student.txt",ios::binary|ios::app);
  26.     osfile.write((char*)p,sizeof(Student)*5);
  27. osfile.close();    
  28.  
  29.     cout<<"\nreading\n";
  30.     Student *p2=new Student[5];
  31.  
  32.     ifstream isfile("Student.txt",ios::binary);
  33.     isfile.read((char*)p2,sizeof(Student)*5);
  34.     isfile.seekg(0);
  35.     isfile.close();
  36. for(int i=0;i<5;i++)
  37.     {
  38.         cout<<i+1<<": ";
  39.         cout<<p2->name;
  40.         cout<<"\n";
  41.         p2++;
  42.         }
  43.  
  44.     return 0;
  45. }
Feb 11 '13 #1

✓ answered by weaknessforcats

There's a lotta stuff wrong here. The big thing is you are incrementing p and p2. These variables contain the addresses of your arrays which you lose by incrementing these variables.

Use another variable to do the increment.

I would be writing two programs here. One to do the file input/outut and the other to implement your Student. Get the file working then add the Student and verify things still work. Then make the data private and write member functions to set and fetch the data Verfy things are still working.

I think you are trying to do too much at one time here. Take it in steps.

6 3430
weaknessforcats
9,208 Expert Mod 8TB
There's a lotta stuff wrong here. The big thing is you are incrementing p and p2. These variables contain the addresses of your arrays which you lose by incrementing these variables.

Use another variable to do the increment.

I would be writing two programs here. One to do the file input/outut and the other to implement your Student. Get the file working then add the Student and verify things still work. Then make the data private and write member functions to set and fetch the data Verfy things are still working.

I think you are trying to do too much at one time here. Take it in steps.
Feb 11 '13 #2
asoom
4
Thanks weaknessforcats :)

it works with me effeciently by applying your statment:
(Use another variable to do the increment.)


here's the code :)
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4. class Student
  5. {
  6. public:
  7.     char name[10];
  8.     char address[10];
  9.     char Gender;
  10.     char DOB[10];
  11.     Student()
  12.     {}
  13. };
  14. int main()
  15. {
  16.     cout<<"\nWritting on file \n";
  17.         Student *p=new Student[5];
  18.         Student *ptr=p;
  19.             for(int i=0;i<5;i++)
  20.     {
  21.         cout<<i+1<<": ";
  22.         cin>>ptr->name;
  23.  
  24.         cout<<"\n";
  25.         ptr++;
  26.         }
  27.     ofstream osfile("Student.txt",ios::binary|ios::app);
  28.     osfile.write((char*)p,sizeof(Student)*5);
  29. osfile.close();    
  30.  
  31.     cout<<"\nreading\n";
  32.     Student *p2=new Student[5];
  33.  
  34.     ifstream isfile("Student.txt",ios::binary);
  35.     isfile.read((char*)p2,sizeof(Student)*5);
  36.     //isfile.seekg(0);
  37.     isfile.close();
  38. for(int i=0;i<5;i++)
  39.     {
  40.         cout<<i+1<<": ";
  41.         cout<<p2->name;
  42.         cout<<"\n";
  43.         p2++;
  44.         }
  45.  
  46.     return 0;
  47. }
Feb 11 '13 #3
weaknessforcats
9,208 Expert Mod 8TB
You are still modifying p2. Remember, you have allocated the p and p2 arrays. You need to delete these arrays and you can't do that of the address in either p or p2 has been changed

But after you fix that, make a copy of this code and save it somewhere.

Then, make the data members in Student private.

Then, write member functions to assign/retrieve the data members.

Then use these functions.

Maybe like:

Expand|Select|Wrap|Line Numbers
  1. char arr[10];
  2. con >> arr;
  3. p[0].SetName(arr);
  4. etc...
  5. cout << p2[0].GetName();
  6. etc...
If you start with working code, after these modifications you should still have working code.
Feb 11 '13 #4
asoom
4
1)Should i need another ptr when i read once from the file?

2)u said:"You need to delete these arrays and you can't do that of the address in either p or p2 has been changed"
-> then u mean i willn't delete these array since there addresses
have been changed?
Feb 12 '13 #5
weaknessforcats
9,208 Expert Mod 8TB
Your p2 pointer to an array for reading is OK. What's not OK is this:
Expand|Select|Wrap|Line Numbers
  1. for(int i=0;i<5;i++)
  2.  39.     {
  3.  40.         cout<<i+1<<": ";
  4.  41.         cout<<p2->name;
  5.  42.         cout<<"\n";
  6.  43.         p2++;             <-----!!!!!!
  7.  44.         }
  8.  
When you allocate an array with the new operator, you need to delete the aloocation when you are finished with it.
Like this:

Expand|Select|Wrap|Line Numbers
  1. delete [] p2;
  2.  
However, if the address in p2 is not the one the new operator gave you, your program will crash on the delete.

You should handle the p2 array the same way as you did the p array.
Feb 12 '13 #6
asoom
4
Thanks weaknessforcats;
really u are Collaborator person :)
Feb 12 '13 #7

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

Similar topics

0
by: chris | last post by:
I'm writing a small app to help me learn more about cryptography. All it does is encrypt all of the files in directory A, and put the encrypted versions of the files in directory B. It then...
7
by: G-Factor | last post by:
Hi all I've just started learning about saving files. I got bit of a problem. The following code gives me an error about incompatible types. (Cannot covert from class character to char *). I...
4
by: nightflyer | last post by:
Hi all, [code snippet appended at the end.) my question: A class has a few string variables with not know length at design time. Now I declare lets say a 1000 of those classes and put them...
2
by: news.hku.hk | last post by:
i have a binary file "myfile.bin", its output of "od -t x1 myfile.bin" is: 0000000 7f 45 4c 46 01 02 01 and longer. i want the output to screen be: 7f 45 4c 46 01 02 01 i try to write the...
4
by: Jens Mittag | last post by:
Hi! In my code, I have an array of a structure, which I want to save to a binary file. When the array is just created, everything works fine, but when I change contents of the array, saving...
18
by: David Buchan | last post by:
Hi guys, This may be a dumb question; I'm just getting into C language here. I wrote a program to unpack a binary file and write out the contents to a new file as a list of unsigned integers....
4
by: Greg | last post by:
Using the HttpWebRequest I'm downloading a PDF file and am trying to save it to the hard drive, but it keeps ending up corrupt (according to adobe acrobat). I know the PDF file isn't corrupt...
5
by: Phil Kelly | last post by:
Hi I need to write the contents of a structure to a binary file - there is one string and 2 integers, but I can't seem to figure out how to write the data correctly. If I am simply writing...
1
by: Chunekit Pong | last post by:
How to save a Base64 encoded string to a binary file For instance, the following XML tag stores the binary data in a Base64 encoded string <data>TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx<data> ...
2
by: brixton | last post by:
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...
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: 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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
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...

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.