The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
reading from a file and pass as variables and ignore # in the file konark Shell Programming and Scripting 4 11-08-2007 03:55 AM
Reading a file and writing the file name to a param file. thebeginer UNIX for Advanced & Expert Users 1 10-05-2007 04:38 PM
Getting a random file davidY Shell Programming and Scripting 6 01-01-2007 05:03 AM
Reading file names from a file and executing the relative file from shell script anushilrai Shell Programming and Scripting 4 03-10-2006 05:25 AM
Reading specific contents from a file and appending it to another file dnicky Shell Programming and Scripting 5 10-04-2005 05:45 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-12-2008
VersEtreOuNe VersEtreOuNe is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 1
Question [C++] 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;
}
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:04 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0