473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

intOwnsVoid
This program compiles but it's not searching properly.

[COLOR="Red"]Circle.h[/COLOR]
Expand|Select|Wrap|Line Numbers
  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
  30.  
  31.  
[COLOR="Red"]Circle.cpp[/COLOR]
Expand|Select|Wrap|Line Numbers
  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. }
  51.  
  52.  
[COLOR="Red"]main.cpp[/COLOR]
Expand|Select|Wrap|Line Numbers
  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. }
  88.  
  89.  
Feb 12 '08 #1
1 1864
It works fine for me, change:

Expand|Select|Wrap|Line Numbers
  1. file.open("Circle.dat", ios::in | ios::app | ios::binary);
  2.  
to:
Expand|Select|Wrap|Line Numbers
  1. file.open("Circle.dat", ios::in |  ios::binary);
  2.  
ios::app isn't appropriate in this situation, because the file is being read and not written to.
Feb 26 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Pernell Williams | last post by:
Hi all: I am new to Python, and this is my first post (and it won't be my last!), so HELLO EVERYONE!! I am attempting to use "xreadlines", an outer loop and an inner loop in conjunction with...
4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
4
by: Marty | last post by:
Hi everybody, Is it possible to do that in VB.NET: I have a text file that is filled with new lines of text every n seconds. This text file get very massive. I want to have my program to...
1
by: Marc Cromme | last post by:
I would like to ask a question about (good ?) style and possibilities in mixing C FILE* and C++ file streams. The background is that I want to use the C libpng library from within C++, but I...
2
by: Brian Ward | last post by:
First: sorry as a relative newbie for previously not including code. My question: Reading C++ books I almost always find programs such as the one below give the following type of code for reading...
20
by: sahukar praveen | last post by:
Hello, I have a question. I try to print a ascii file in reverse order( bottom-top). Here is the logic. 1. Go to the botton of the file fseek(). move one character back to avoid the EOF. 2....
2
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { ...
30
by: siliconwafer | last post by:
Hi All, I want to know tht how can one Stop reading a file in C (e.g a Hex file)with no 'EOF'?
9
by: Use*n*x | last post by:
Hello, I have a binary file (image file) and am reading 4-bytes at a time. The File size is 63,480,320 bytes. My assumption is that if I loop through this file reading 4 bytes at a time, I...
0
by: jigsmshah | last post by:
I have a windows service developed in C#.I am reading the connection string from an ini file and also i am reading 3 image file from the bin directory. now the new requirement is that there will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.