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

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    9

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

    INFO:
    The program should enter a circle radius and Id for that circle to a file, then it should search for that id and print the radius for that circle.

    PROBLEM:
    This program compiles but it's not searching properly.

    Circle.h
    Code:
    #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
    Code:
    #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
    Code:
    #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;
    }

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    If I do your homework, do I get the A?

    I will say that you are overcomplicating that class. You should probably think more along the lines of a linked list.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    9
    I've done my homework, I just need a little help.

    & linked list ???

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    A linked list is a method of storing multiple objects in memory as a list that can be searched and updated at run time. Objects can be added or removed fom the list by linking or unlinking them into the chain. Each object contains 1 or more pointers and the data for the object itself. The individual links are usually dynamically allocated in memory on creation and free'd on deletion.

    Basically its a way to store items when you dont know how many there will be at compile time.

    Its perfect for this example because they are easy to implement as a class and you can add any number of object to the list up to the limit of RAM.

    a link struct -
    Code:
    struct LINK {
        LPVOID Next;
        LPVOID Prev;
        LPVOID pObject;
        };

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't think it's that good idea to use defines or typedefs such as LPVOID. It can confuse and it typically used with Windows API to specify what type it takes.
    And I object to the bracket placement. K&R style is like this:
    Code:
    struct LINK {
        void* Next;
        void* Prev;
        void* pObject;
    };
    It's easier for the eye and the brain.
    However, since this is C++, I'd also recommend a type-safe struct in such case, unless you haven't learn templates yet:
    Code:
    template<typename T> struct LINK {
        T* Next;
        T* Prev;
        T* pObject;
    };
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    9
    The archive has many circles, I only need to search for 1 at a time, that means only 1 object in memory..

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    the next and prev should not be void *, they should be LINK *.
    Last edited by robwhit; 02-12-2008 at 04:07 PM. Reason: LINK to LINK *.

  8. #8
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by Elysia View Post
    And I object to the bracket placement.
    Elysia, you sound like an ex-girlfirend of mine, are you applying for the vacancy?


    As for using template, the guy is just learning the basics, there is no point in throwing too much at him too fast. Stick to basic types, yeah im just so used to using LPVOID I shoudl have used void*, but thats better than using templates.

    yes Next and Prev should be LINK*

    It's late in the day, my fingers are numb and the weed is wearing off...
    Last edited by abachler; 02-12-2008 at 04:06 PM.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Asking for the ID on input doesn't make sense. The user has no control over this. The ID should be a counter starting at zero, incremented for each new circle.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Im thinking maybe its intended for entering from a list only certain items. Like a job completion database where you enter the ID of the completeed jobs as they finish, btu have to leave room for the ones that arent finished yet and may have their ID assigned by some other process.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Then he needs to seek before writing.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Agreed.

  13. #13
    Registered User
    Join Date
    Feb 2008
    Posts
    9
    I added a file.seekp(ios::beg) before writing, still not searching properly.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, you misunderstand.

    But perhaps you can first describe what you do, what you expect to happen, and what actually happens. Because that's far more interesting than "it doesn't work".
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    Registered User
    Join Date
    Feb 2008
    Posts
    9
    I hope this screen shot gives you an idea of what I'm trying to do.

    http://i29.tinypic.com/ip9xkk.jpg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM