| | |
File I/O (Reading from a Random-Access File) NEW
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2008
Posts: 17
Reputation:
Solved Threads: 0
Circle.h
Circle.cpp
main.cpp
The searching from the file is not working properly ????
c++ Syntax (Toggle Plain Text)
#ifndef CIRCLE_H #define CIRCLE_H #include <iostream> #include <fstream> using namespace std; class Circle { friend ostream& operator <<(ostream& output, const Circle& aCircle); friend istream& operator >>(istream& input, Circle& aCircle); public: Circle(); Circle(double radius, int id); Circle(const Circle& aCircle); void SetCircleRadius(double radius) { _circleRadius = radius; } void SetCircleId(int id) { _circleId = id; } double GetCircleRadius() const { return _circleRadius; } int GetCircleId() const { return _circleId; } private: double _circleRadius; int _circleId; }; #endif
Circle.cpp
c++ Syntax (Toggle Plain Text)
#include "Circle.h" Circle::Circle() { _circleId = 0; _circleRadius = 0.0; } Circle::Circle(double radius, int id) { _circleId = id; _circleRadius = radius; } Circle::Circle(const Circle& aCircle) { _circleId = aCircle._circleId; _circleRadius = aCircle._circleRadius; } ostream& operator <<(ostream& output, const Circle& aCircle) { output << "\n ------------" << endl; output << " Id radius" << endl; output << " ------------" << endl; output << " " << aCircle._circleId << " " << aCircle._circleRadius << endl; output << " ------------" << "\n\n"; return (output); } istream& operator >>(istream& input, Circle& aCircle) { int quantity; cout << "\nHow many circles do you want to add "; input >> quantity; for (int i = 0; i < quantity; ++i) { cout << "\n\nEnter the ID of Circle #" << (i + 1) << " : "; input >> aCircle._circleId; cout << "Enter Radius of Circle #" << (i + 1) << " : "; input >> aCircle._circleRadius; cout << endl; } return (input); }
main.cpp
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <conio.h> #include <fstream> using namespace std; #include "Circle.h" int main () { Circle aCircle; fstream file; int option; do { cout << "Menu \n" << " (1) Add Circle(s) \n" << " (2) Find a Circle by ID \n" << " (3) Exit \n" << "Your Selection -> "; cin >> option; switch (option) { case 1: file.close(); file.clear(); file.open("Circle.dat", ios::out | ios::app | ios::binary); if (!file) { cerr << "\n\nFailed to open file.\n\n"; system("PAUSE"); exit(1); } else { cin >> aCircle; file.write(reinterpret_cast<char*> (&aCircle), sizeof(Circle)); } break; case 2: file.close(); file.clear(); cout << "Enter id: "; int id; cin >> id; aCircle.SetCircleId(id); file.open("Circle.dat", ios::in | ios::app | ios::binary); if (!file) { cerr << "\n\nFailed to open file.\n\n"; system("PAUSE"); exit(1); } else { file.seekg(id * sizeof(Circle), ios::beg); file.read(reinterpret_cast<char *> (&aCircle), sizeof(Circle)); cout << aCircle; } break; case 3: file.close(); cout << "\n\nG o o D B y E\n\n"; exit(0); break; default: cerr << "\nERROR: Wrong Option menu. Please try again.\n\n"; } } while (option != 3); return EXIT_SUCCESS; }
The searching from the file is not working properly ????
main.cpp line 54: ios::app is only for output files, not input, so you might as well remove that from the open statement.
The main reason your program doesn't work is because of the way you are writing out the records. So NOT use ios::app (append) in the open statement, and before writing seekp() to the correct location, very similar to how you did it in main.cpp line 64. Do the same thing at main.cpp line 39.
The main reason your program doesn't work is because of the way you are writing out the records. So NOT use ios::app (append) in the open statement, and before writing seekp() to the correct location, very similar to how you did it in main.cpp line 64. Do the same thing at main.cpp line 39.
I told Santa what I wanted for Christmas and he washed my mouth out with soap.
I think the ios::app is absolutely necessary in the output option, otherwise every new circle will overwrite the previous data (default file open mode is ios::trunc). ios::app has no meaning to an input file action, as AD said.
In the search section, you ask user for circle ID. Then you use that to index into the file to find a circle. But, when you enter circles, the user can enter any value for ID, can't they? So is there any correspondence?
For that matter, your circle input ( >> overload) asks how many circles to enter, and allows the user to enter multiple circle data. But it all is stored to the same circle object, each input overwriting the previous. Usually the input operator should not display prompts - it should just grab input from the stream and store it to the correct data member, so that you can use it with any stream. How would this implementation work reading from a text file with circle data? Not well.
In the search section, you ask user for circle ID. Then you use that to index into the file to find a circle. But, when you enter circles, the user can enter any value for ID, can't they? So is there any correspondence?
For that matter, your circle input ( >> overload) asks how many circles to enter, and allows the user to enter multiple circle data. But it all is stored to the same circle object, each input overwriting the previous. Usually the input operator should not display prompts - it should just grab input from the stream and store it to the correct data member, so that you can use it with any stream. How would this implementation work reading from a text file with circle data? Not well.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
First, clarify what the problem is you're trying to solve. It's not really clear from the code.
Once you have a good definition for the circle class, you can do whatever you want with it in your main( ). Input a circle, write it to file. Create an array of circles (not of IDs), write or read the collection to file. All depends on what you want to do.
Once you have a good definition for the circle class, you can do whatever you want with it in your main( ). Input a circle, write it to file. Create an array of circles (not of IDs), write or read the collection to file. All depends on what you want to do.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
•
•
I think the ios::app is absolutely necessary in the output option, otherwise every new circle will overwrite the previous data (default file open mode is ios::trunc). ios::app has no meaning to an input file action, as AD said.
.
•
•
•
•
(append) Set the stream's position indicator to the end of the stream before each output operation.
C++ Syntax (Toggle Plain Text)
int main() { string word; fstream out("myfile.dat",ios::out | ios::binary); out << "Hello World\r\n"; out.close(); out.clear(); out.open("myfile.dat",ios::out | ios::binary); out << "Hello World\r\n"; out.close(); out.clear(); out.open("myfile.dat",ios::in | ios::binary); while( out >> word) cout << word << "\n"; return 0; }
Last edited by Ancient Dragon; Feb 13th, 2008 at 8:15 am.
I told Santa what I wanted for Christmas and he washed my mouth out with soap.
AD, I run your sample, and only one instance of "hello world" exists in the file. It is being truncated at the second file opening operation.
ios::app sets the initial position of the file pointer to end of file when it's opened, so previous content is preserved. If you want random access to the file (read/write anywhere), preserving previous content, use mode ios::ate (At End).
Now I'm gonna have to set up a test - what happens if file opened in ios::app mode and I try to seek to a previously existing position and write something new?
--Added--
For random writing within existing file content, use ios:: in | ios::out | ios::ate. Without also having the in mode, you can't go backwards from end of file.
ios::app, even when set up as above, will not seek to previous portion of file.
Val
ios::app sets the initial position of the file pointer to end of file when it's opened, so previous content is preserved. If you want random access to the file (read/write anywhere), preserving previous content, use mode ios::ate (At End).
Now I'm gonna have to set up a test - what happens if file opened in ios::app mode and I try to seek to a previously existing position and write something new?
--Added--
For random writing within existing file content, use ios:: in | ios::out | ios::ate. Without also having the in mode, you can't go backwards from end of file.
ios::app, even when set up as above, will not seek to previous portion of file.
Val
Last edited by vmanes; Feb 13th, 2008 at 10:01 am.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Similar Threads
- problems with reading random access line from a file (C++)
- File I/O (Reading from a Random-Access File) (C++)
- random access file help please (Visual Basic 4 / 5 / 6)
- random access file and creating an index file (C)
- Pascal : Access a file of 100 Quiz questions so 12 Random ones are displayed? (Pascal and Delphi)
- Reading in a random line from a file (C++)
- Random access read write (Visual Basic 4 / 5 / 6)
- Help with searching (C)
Other Threads in the C++ Forum
- Previous Thread: the difference of using fork() and exec() with system()
- Next Thread: Problem with "getline(cin, name)"
Views: 1917 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






