| | |
Trouble with Binary files
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 9
Reputation:
Solved Threads: 0
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:
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?
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:
C++ Syntax (Toggle Plain Text)
#include<fstream.h> #include<conio.h> #include<stdio.h> class books { int bcopies; public: void init() { cout<<"Enter the no.of copies:"; cin>>bcopies; } void putvalue() { cout<<"Copies:"<<bcopies<<"\n"; } }; int choice,n,i; int main() { clrscr(); fstream file; cout<<"Initialize?(1/2)"; cin>>choice; if(choice==1) { cout<<"Enter the no. of books:"; cin>>n; --n; file.open("library.dat",ios::out|ios::binary|ios::trunc); fstream f; f.open("n.dat",ios::out|ios::binary); f.write((char*)&n,sizeof(int)); books * B=new books[n]; for(i=0;i<=n;i++) { clrscr(); B[i].init(); file.write((char*)&B[i],sizeof(books)); } file.close(); f.close(); } fstream f; file.open("library.dat",ios::in|ios::binary); f.open("n.dat",ios::in|ios::binary); books * B=new books[n]; file.seekg(0,ios::beg); file.read((char*)&B[i],sizeof(books)); for(i=0;i<=n;i++) { B[i].putvalue(); } getch(); return 0; }
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?
-5
#2 24 Days Ago
line 48: it is not reading the value of n from the data file. You need to put
delete line 49 because that's the default when files are opened for reading.
line 50 should be this:
line 51 is wrong. use < operator, not the <= operator.
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.
•
•
Join Date: Oct 2009
Posts: 9
Reputation:
Solved Threads: 0
0
#4 23 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:
The code I changed to:
Only the part after if:
C++ Syntax (Toggle Plain Text)
fstream f; file.open("library.dat",ios::in|ios::binary); f.open("n.dat",ios::in|ios::binary); f>>n; books * B=new books[n]; file.read((char*)B,sizeof(books)); for(i=0;i<n;i++) { B[i].putvalue(); } getch(); return 0;
-5
#5 23 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.
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.
-5
#6 23 Days Ago
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.
•
•
Join Date: Oct 2009
Posts: 9
Reputation:
Solved Threads: 0
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:
Once again, thank you!
C++ Syntax (Toggle Plain Text)
#include<fstream.h> #include<conio.h> #include<stdio.h> class books { int bcopies; public: void getdata() { cout<<"Enter the no.of copies:"; cin>>bcopies; } void dispdata() { cout<<"Copies:"<<bcopies<<"\n"; } }; int main() { clrscr(); char ch;books B; fstream file; file.open("library.dat",ios::out|ios::ate|ios::binary|ios::in); cout<<"Would you like to add a book?"; cin>>ch; while(ch=='y'||ch=='Y') { B.getdata(); file.write((char*)&B,sizeof(books)); cout<<"Continue?(y/n)"; cin>>ch; } cout<<"data:\n"; file.seekg(0,ios::beg); while(file.read((char*)&B,sizeof(B))) { B.dispdata(); } getch(); return 0; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: How to extract dynamic data from a .txt file/fstream?
- Next Thread: Need to know how to Start
| Thread Tools | Search this Thread |
access advice aliased anti archive array british classification control corners curved curves cybercrime data files forensic form hottub identitytheft information internet library linux loss military news operating php privacy pygame python raf ransomware school security system theft upload users video website







