File I/O (Reading from a Random-Access File) NEW

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2008
Posts: 17
Reputation: VersEtreOuNe is an unknown quantity at this point 
Solved Threads: 0
VersEtreOuNe VersEtreOuNe is offline Offline
Newbie Poster

File I/O (Reading from a Random-Access File) NEW

 
0
  #1
Feb 12th, 2008
Circle.h
  1. #ifndef CIRCLE_H
  2. #define CIRCLE_H
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6. using namespace std;
  7.  
  8. class Circle
  9. {
  10. friend ostream& operator <<(ostream& output, const Circle& aCircle);
  11. friend istream& operator >>(istream& input, Circle& aCircle);
  12.  
  13. public:
  14. Circle();
  15. Circle(double radius, int id);
  16. Circle(const Circle& aCircle);
  17.  
  18. void SetCircleRadius(double radius) { _circleRadius = radius; }
  19. void SetCircleId(int id) { _circleId = id; }
  20.  
  21. double GetCircleRadius() const { return _circleRadius; }
  22. int GetCircleId() const { return _circleId; }
  23.  
  24. private:
  25. double _circleRadius;
  26. int _circleId;
  27. };
  28.  
  29. #endif

Circle.cpp
  1. #include "Circle.h"
  2.  
  3. Circle::Circle()
  4. {
  5. _circleId = 0;
  6. _circleRadius = 0.0;
  7. }
  8.  
  9. Circle::Circle(double radius, int id)
  10. {
  11. _circleId = id;
  12. _circleRadius = radius;
  13. }
  14.  
  15. Circle::Circle(const Circle& aCircle)
  16. {
  17. _circleId = aCircle._circleId;
  18. _circleRadius = aCircle._circleRadius;
  19. }
  20.  
  21. ostream& operator <<(ostream& output, const Circle& aCircle)
  22. {
  23. output << "\n ------------" << endl;
  24. output << " Id radius" << endl;
  25. output << " ------------" << endl;
  26. output << " " << aCircle._circleId << " " << aCircle._circleRadius << endl;
  27. output << " ------------" << "\n\n";
  28.  
  29. return (output);
  30. }
  31.  
  32. istream& operator >>(istream& input, Circle& aCircle)
  33. {
  34. int quantity;
  35. cout << "\nHow many circles do you want to add ";
  36. input >> quantity;
  37.  
  38. for (int i = 0; i < quantity; ++i)
  39. {
  40. cout << "\n\nEnter the ID of Circle #" << (i + 1) << " : ";
  41. input >> aCircle._circleId;
  42.  
  43. cout << "Enter Radius of Circle #" << (i + 1) << " : ";
  44. input >> aCircle._circleRadius;
  45.  
  46. cout << endl;
  47. }
  48.  
  49. return (input);
  50. }

main.cpp
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. #include "Circle.h"
  7.  
  8. int main ()
  9. {
  10. Circle aCircle;
  11. fstream file;
  12. int option;
  13.  
  14. do
  15. {
  16. cout << "Menu \n"
  17. << " (1) Add Circle(s) \n"
  18. << " (2) Find a Circle by ID \n"
  19. << " (3) Exit \n"
  20. << "Your Selection -> ";
  21. cin >> option;
  22.  
  23. switch (option)
  24. {
  25. case 1:
  26. file.close();
  27. file.clear();
  28.  
  29. file.open("Circle.dat", ios::out | ios::app | ios::binary);
  30.  
  31. if (!file)
  32. {
  33. cerr << "\n\nFailed to open file.\n\n";
  34. system("PAUSE");
  35. exit(1);
  36. }
  37. else
  38. {
  39. cin >> aCircle;
  40. file.write(reinterpret_cast<char*> (&aCircle), sizeof(Circle));
  41. }
  42. break;
  43.  
  44. case 2:
  45. file.close();
  46. file.clear();
  47.  
  48.  
  49. cout << "Enter id: ";
  50. int id;
  51. cin >> id;
  52. aCircle.SetCircleId(id);
  53.  
  54. file.open("Circle.dat", ios::in | ios::app | ios::binary);
  55.  
  56. if (!file)
  57. {
  58. cerr << "\n\nFailed to open file.\n\n";
  59. system("PAUSE");
  60. exit(1);
  61. }
  62. else
  63. {
  64. file.seekg(id * sizeof(Circle), ios::beg);
  65. file.read(reinterpret_cast<char *> (&aCircle), sizeof(Circle));
  66.  
  67. cout << aCircle;
  68. }
  69. break;
  70.  
  71.  
  72. case 3:
  73. file.close();
  74. cout << "\n\nG o o D B y E\n\n";
  75. exit(0);
  76. break;
  77.  
  78.  
  79. default:
  80. cerr << "\nERROR: Wrong Option menu. Please try again.\n\n";
  81.  
  82. }
  83.  
  84. } while (option != 3);
  85.  
  86. return EXIT_SUCCESS;
  87. }

The searching from the file is not working properly ????
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,679
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: 1504
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: File I/O (Reading from a Random-Access File) NEW

 
0
  #2
Feb 12th, 2008
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.
I told Santa what I wanted for Christmas and he washed my mouth out with soap.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 17
Reputation: VersEtreOuNe is an unknown quantity at this point 
Solved Threads: 0
VersEtreOuNe VersEtreOuNe is offline Offline
Newbie Poster

Re: File I/O (Reading from a Random-Access File) NEW

 
0
  #3
Feb 12th, 2008
Didn't work...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,679
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: 1504
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: File I/O (Reading from a Random-Access File) NEW

 
0
  #4
Feb 12th, 2008
>>Didn't work...
what didn't work. I can't help you if you don't post your most recent attempt. Just saying something didn't work does not help at all.
Last edited by Ancient Dragon; Feb 12th, 2008 at 11:11 pm.
I told Santa what I wanted for Christmas and he washed my mouth out with soap.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: File I/O (Reading from a Random-Access File) NEW

 
0
  #5
Feb 12th, 2008
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 17
Reputation: VersEtreOuNe is an unknown quantity at this point 
Solved Threads: 0
VersEtreOuNe VersEtreOuNe is offline Offline
Newbie Poster

Re: File I/O (Reading from a Random-Access File) NEW

 
0
  #6
Feb 13th, 2008
What if i make the Id and array of 100 Id's that way they will not be replaced. Right ????
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: File I/O (Reading from a Random-Access File) NEW

 
0
  #7
Feb 13th, 2008
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,679
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: 1504
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: File I/O (Reading from a Random-Access File) NEW

 
0
  #8
Feb 13th, 2008
Originally Posted by vmanes View Post
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.
.
ios::app means
With that flag it isn't possible to write a record in any random place in the file. The default is not ios::trunc -- you have to set that flag specifically if you want the file truncated each time it is open.
  1. int main()
  2. {
  3. string word;
  4. fstream out("myfile.dat",ios::out | ios::binary);
  5. out << "Hello World\r\n";
  6. out.close();
  7. out.clear();
  8. out.open("myfile.dat",ios::out | ios::binary);
  9. out << "Hello World\r\n";
  10. out.close();
  11. out.clear();
  12. out.open("myfile.dat",ios::in | ios::binary);
  13. while( out >> word)
  14. cout << word << "\n";
  15. return 0;
  16. }
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: File I/O (Reading from a Random-Access File) NEW

 
1
  #9
Feb 13th, 2008
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
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1917 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC