Trouble with Binary files

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2009
Posts: 9
Reputation: abhaymv is an unknown quantity at this point 
Solved Threads: 0
abhaymv abhaymv is offline Offline
Newbie Poster

Trouble with Binary files

 
0
  #1
28 Days Ago
I am a student in C++.We undertook a school project to develop a library information system.We are only allowed to use Turbo C++ version 3 compiler.We cannot use ANSI/ISO C++(Not in our syllabus.)The following is my problem:

In my project, I have a class books.When the program is first run, it would create n number of object of books class.The data members would then be given values.It is then saved in a binary file.When the software is run again,the program asks whether the books are to be initialized?(i.e, clear the binary file and make another set of objects and write them to the disk.)If no is selected,the program will display the information of the books.i.e, it will retreive the information from memmory.In my program,Even though I create many objects,only the values of one object is outputed.

A simplified version of the code is given below:
  1. #include<fstream.h>
  2. #include<conio.h>
  3. #include<stdio.h>
  4. class books
  5. {
  6. int bcopies;
  7. public:
  8. void init()
  9. {
  10. cout<<"Enter the no.of copies:";
  11. cin>>bcopies;
  12. }
  13. void putvalue()
  14. {
  15. cout<<"Copies:"<<bcopies<<"\n";
  16. }
  17. };
  18. int choice,n,i;
  19. int main()
  20. {
  21. clrscr();
  22. fstream file;
  23. cout<<"Initialize?(1/2)";
  24. cin>>choice;
  25. if(choice==1)
  26. {
  27. cout<<"Enter the no. of books:";
  28. cin>>n;
  29. --n;
  30. file.open("library.dat",ios::out|ios::binary|ios::trunc);
  31. fstream f;
  32. f.open("n.dat",ios::out|ios::binary);
  33. f.write((char*)&n,sizeof(int));
  34. books * B=new books[n];
  35. for(i=0;i<=n;i++)
  36. {
  37. clrscr();
  38. B[i].init();
  39. file.write((char*)&B[i],sizeof(books));
  40. }
  41. file.close();
  42. f.close();
  43.  
  44. }
  45. fstream f;
  46. file.open("library.dat",ios::in|ios::binary);
  47. f.open("n.dat",ios::in|ios::binary);
  48. books * B=new books[n];
  49. file.seekg(0,ios::beg);
  50. file.read((char*)&B[i],sizeof(books));
  51. for(i=0;i<=n;i++)
  52. {
  53.  
  54.  
  55. B[i].putvalue();
  56. }
  57. getch();
  58.  
  59. return 0;
  60. }

OUTPUT:
__________________________
Initialize?(1/2) 1
Enter the number of books: 3
__________________________
Enter the number of copies:2
__________________________
Enter the number of copies:1
__________________________
Enter the number of copies:3
Copies:2
Copies:1
Copies:3
__________________________
<Program is closed,and excecuted again:>
__________________________
Initialize(1/2) 2
Copies:2
__________________________
why the data from the other objects are not displayed?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-7
  #2
28 Days Ago
line 48: it is not reading the value of n from the data file. You need to put f >> n; between lines 47 and 48.


delete line 49 because that's the default when files are opened for reading.

line 50 should be this: file.read((char*)B,sizeof(books)); . The value of i is probably outside the bounds of the array anyway at that point.


line 51 is wrong. use < operator, not the <= operator.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 39
Reputation: niXman is an unknown quantity at this point 
Solved Threads: 10
niXman's Avatar
niXman niXman is offline Offline
Light Poster
 
0
  #3
28 Days Ago
The only one right decidion is to overload the operator<< () and operator>> ()
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 9
Reputation: abhaymv is an unknown quantity at this point 
Solved Threads: 0
abhaymv abhaymv is offline Offline
Newbie Poster
 
0
  #4
27 Days Ago
Sorry sir, the data entry phase of the output remains unchanged and if option 2 is given in the beginning, Nothing happens..btw,isnt there (char*)&B?
The code I changed to:

Only the part after if:

  1. fstream f;
  2. file.open("library.dat",ios::in|ios::binary);
  3. f.open("n.dat",ios::in|ios::binary);
  4. f>>n;
  5. books * B=new books[n];
  6. file.read((char*)B,sizeof(books));
  7. for(i=0;i<n;i++)
  8. {
  9. B[i].putvalue();
  10. }
  11.  
  12. getch();
  13. return 0;
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-7
  #5
27 Days Ago
>>.btw,isnt there (char*)&B?
NO. B is already a pointer, so what you would create is a pointer to a pointer.

You didn't post the entire program so I can't help you more. I have no clue what Option 2 (or any other option) is.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-7
  #6
27 Days Ago
Originally Posted by niXman View Post
The only one right decidion is to overload the operator<< () and operator>> ()
Yes, that is another way to do it, but not the only way. Overloading the operators would take more processing time because the program would have to read/write the structures/classes one at a time. The way he is doing it the entire array is read/written all in one shot.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 9
Reputation: abhaymv is an unknown quantity at this point 
Solved Threads: 0
abhaymv abhaymv is offline Offline
Newbie Poster

Solved!

 
0
  #7
27 Days Ago
Thank you all for your help.However,I am extremely happy to say that I found an Alternative logic.I stopped using the dynamic initialization and object array.My new code:
  1. #include<fstream.h>
  2. #include<conio.h>
  3. #include<stdio.h>
  4. class books
  5. {
  6. int bcopies;
  7. public:
  8. void getdata()
  9. {
  10. cout<<"Enter the no.of copies:";
  11. cin>>bcopies;
  12. }
  13. void dispdata()
  14. {
  15. cout<<"Copies:"<<bcopies<<"\n";
  16. }
  17. };
  18. int main()
  19. {
  20. clrscr();
  21. char ch;books B;
  22. fstream file;
  23. file.open("library.dat",ios::out|ios::ate|ios::binary|ios::in);
  24. cout<<"Would you like to add a book?";
  25. cin>>ch;
  26. while(ch=='y'||ch=='Y')
  27. {
  28. B.getdata();
  29. file.write((char*)&B,sizeof(books));
  30. cout<<"Continue?(y/n)";
  31. cin>>ch;
  32. }
  33. cout<<"data:\n";
  34. file.seekg(0,ios::beg);
  35. while(file.read((char*)&B,sizeof(B)))
  36. {
  37. B.dispdata();
  38. }
  39. getch();
  40. return 0;
  41. }
Once again, thank you!
Reply With Quote Quick reply to this message  
Reply

Tags
files, information, library

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC