| | |
having problem with random access files
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jul 2008
Posts: 1
Reputation:
Solved Threads: 0
I'm having a problem with retrieving records from my file just started programming over a month a go please help this is for my class project.
I need to do delete,update,query,and add to binary file and prompt the user to enter which record they need to retrieve and i started adding.please if you could give me an example of each i would be very gratefully.my project needs to be submmitted by 28/7/08.
here is my code:
I need to do delete,update,query,and add to binary file and prompt the user to enter which record they need to retrieve and i started adding.please if you could give me an example of each i would be very gratefully.my project needs to be submmitted by 28/7/08.
here is my code:
C++ Syntax (Toggle Plain Text)
#include<iostream.h> #include<string.h> #include<fstream.h> #include<windows.h> #include<conio.h> using namespace std; class houses { int house_number; int number_of_rooms; string furnished; double cost; public: /* houses() { house_number = 0; number_of_rooms = 0; furnished = ""; cost = 0; occupied = ""; }*/ void get_house() { cout<<"Please enter the house number(integer data only): "; cin>>house_number; cout<<"Please enter the number of rooms(integer data only): "; cin>>number_of_rooms; cout<<"Please enter the cost of the house: "; cin>>cost; cout<<"Please indicate if the house is furnished(yes,no): "; cin>>furnished; } void show_house() { cout<<"house number: "<<house_number<<endl; cout<<"number of rooms: "<<number_of_rooms<<endl; cout<<"cost of the house: "<<cost<<endl; cout<<"Is house the furnished: "<<furnished<<endl; } void housewrite() { houses h; ofstream out("house.bin",ios::app); if(out == NULL) { for(int i = 0; i < 15;i++) { cout<<".."; Sleep(200); } cout<<"......FILE COULD NOT BE OPENED"; } else { get_house(); out.write((char *)&h,sizeof(h)); out.close(); for(int i = 0; i < 15;i++) { cout<<".."; Sleep(200); } cout<<".....FILE WRITTEN SUCESSFULLY"; } } void display_house() { int house_temp ; houses h; ifstream in("house.bin",ios::in|ios::binary); if(in == NULL) { for(int i = 0; i < 15; i++) { cout<<".."; Sleep(200); } cout<<"..........FILE COULD NOT BE OPENED"; } else { do { cout<<"ENTER HOUSE NUMBER: "; cin>>house_temp; in.seekg(house_temp*sizeof(h),ios::beg); in.read((char *)&h,sizeof(h)); if(house_temp == h.house_number) { h.show_house(); break; } else { cout<<"HOUSE NUMBER DOES NOT EXIST......."; break; } }while(!in.eof()); }in.close(); } }; int main() { houses obj; obj.housewrite(); getch(); system("cls"); obj.display_house(); return 0; }
Last edited by Ancient Dragon; Jul 25th, 2008 at 1:56 am. Reason: add code tags
•
•
Join Date: Apr 2008
Posts: 70
Reputation:
Solved Threads: 18
The idea is generally there; however, there are some things you need to look at:
1) Reading & writing the entire object out - that can be problematic. What if, the first time you write out a "houses" object, the "furnished" string is set to "no"? Then the second, it's set to "yes"? The object may not look like you expect internally. Better to write each variable out knowing what length each is:
Then, in your display_house() function, do the same when reading in:
2) your total record size (needed for the seekg operation) will now no longer be
3) Internal use of the object you're defining. I guess it can be OK, but not like this (in the housewrite() method):
The method "get_house()" loads the data into the object variables of the current instance, but then you attempt to write out "h" - an uninitialized local version of your object. You'll be fixing that anyway if you follow the steps in 1 & 2, above.
4) The method you're using to locate records is a combo of relative record number, and "house_number" the user enters, which can be easily broken. You use the house_number to perform the seek, then check the record you read in to see if the house number from the file matches what the user typed. If you run the program for the first time, enter a "7" as the house number and then try to search on that number, you'll seek past the end of file. Either use the relative record number, or just start at the top and read each record, attempting to match the house number the user entered. Choose one or the other.
See if these help and let me know how it comes out.
1) Reading & writing the entire object out - that can be problematic. What if, the first time you write out a "houses" object, the "furnished" string is set to "no"? Then the second, it's set to "yes"? The object may not look like you expect internally. Better to write each variable out knowing what length each is:
C++ Syntax (Toggle Plain Text)
out.write((char *)&house_number, sizeof(int)); ...
Then, in your display_house() function, do the same when reading in:
C++ Syntax (Toggle Plain Text)
in.read((char *)&house_number, sizeof(int)); ...
2) your total record size (needed for the seekg operation) will now no longer be
sizeof(h) , but the combined size of each of your elements (allow 4 bytes for the string, since you are storing "yes" or "no").3) Internal use of the object you're defining. I guess it can be OK, but not like this (in the housewrite() method):
C++ Syntax (Toggle Plain Text)
houses h; ... get_house(); out.write((char *)&h,sizeof(h)); out.close();
The method "get_house()" loads the data into the object variables of the current instance, but then you attempt to write out "h" - an uninitialized local version of your object. You'll be fixing that anyway if you follow the steps in 1 & 2, above.
4) The method you're using to locate records is a combo of relative record number, and "house_number" the user enters, which can be easily broken. You use the house_number to perform the seek, then check the record you read in to see if the house number from the file matches what the user typed. If you run the program for the first time, enter a "7" as the house number and then try to search on that number, you'll seek past the end of file. Either use the relative record number, or just start at the top and read each record, attempting to match the house number the user entered. Choose one or the other.
See if these help and let me know how it comes out.
-Mike
Where are you having the problem?
What does the program do wrong?
What makes it wrong?
What should it do instead?
When asking for help it's necessary to explain the problem, not just toss 3 ambiguous sentences together without punctuation making it hard to decipher your meaning.
What does the program do wrong?
What makes it wrong?
What should it do instead?
When asking for help it's necessary to explain the problem, not just toss 3 ambiguous sentences together without punctuation making it hard to decipher your meaning.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Never write objects with std::string members (see furnished) in address-sizeof style. It's wrong to write (and read) individual std::string type members with this C-like mechanics. The size in bytes of std::string (
So if you want to compute relative file offset for i-th record, use only basic types members with fixed length in your record structure type.
sizeof(std::string) is well-defined and CONSTANT value. It does not include true size of contained text. The pointer to std::string object does not refer to the contained text. You may write std::string object in a such manner but never get it back (w/o possible program crash).So if you want to compute relative file offset for i-th record, use only basic types members with fixed length in your record structure type.
![]() |
Similar Threads
- fstream Tutorial (C++)
- Forms in Random access files (Visual Basic 4 / 5 / 6)
- VB 6.0. Creating a file for random access (Visual Basic 4 / 5 / 6)
- traversing files and skipping lines (C)
- Help needed with VB Assignment (Visual Basic 4 / 5 / 6)
- pls help me (Visual Basic 4 / 5 / 6)
- file processing, Random-access Files (C++)
- Intermitent access to google and other sites (Viruses, Spyware and other Nasties)
- Random Reboots when Booting From a CD (Troubleshooting Dead Machines)
- Random popups, even when not online (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Quick style question: NULL
- Next Thread: another c++ problem: doesnt show messages
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






