[C++] File I/O (Reading from a Random-Access File)


 
Thread Tools Search this Thread
Top Forums Programming [C++] File I/O (Reading from a Random-Access File)
# 1  
Old 02-12-2008
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;
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh Script, Reading A File, Grepping A File Contents In Another File

So I'm stumped. First... APOLOGIES... my work is offline in an office that has zero internet connectivity, as required by our client. If need be, I could print out my script attempts and retype them here. But on the off chance... here goes. I have a text file (file_source) of terms, each line... (3 Replies)
Discussion started by: Brusimm
3 Replies

2. UNIX for Dummies Questions & Answers

Reading Xml file and print the values into the text file in columnwise?

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (4 Replies)
Discussion started by: sravanreddy
4 Replies

3. UNIX for Dummies Questions & Answers

Reading XML file and print the values in the text file using Linux shell script

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (1 Reply)
Discussion started by: sravanreddy
1 Replies

4. Shell Programming and Scripting

Need to generate a file with random data. /dev/[u]random doesn't exist.

Need to use dd to generate a large file from a sample file of random data. This is because I don't have /dev/urandom. I create a named pipe then: dd if=mynamed.fifo do=myfile.fifo bs=1024 count=1024 but when I cat a file to the fifo that's 1024 random bytes: cat randomfile.txt >... (7 Replies)
Discussion started by: Devyn
7 Replies

5. Shell Programming and Scripting

Searching for Log / Bad file and Reading and writing to a flat file

Need to develop a unix shell script for the below requirement and I need your assistance: 1) search for file.log and file.bad file in a directory and read them 2) pull out "Load_Start_Time", "Data_File_Name", "Error_Type" from log file 4) concatinate each row from bad file as... (3 Replies)
Discussion started by: mlpathir
3 Replies

6. Programming

Random Access Iterator Error

I am unable to resolve the below code compilation issue: class A { public: int x; }; void sort_A(TArray<A> &_Atype) { std::stable_sort (_Atype.begin(), _Atype.end()); } bool operator< (const A & _a1, const A & _a2) { return _a1.x < _a2.x; } When we compile using gcc 4.1.2... (4 Replies)
Discussion started by: uunniixx
4 Replies

7. Shell Programming and Scripting

Not access variable outside loop when a reading a file

I am writing a shell script using the korn shell. It seems that I am only able to use local variables within a while loop that is reading a file. (I can't access a variable outside a previously used while loop.) It's been a while since I wrote shell scripts. Here is a sample cat file.txt... (4 Replies)
Discussion started by: ricardo.ludwig
4 Replies

8. Shell Programming and Scripting

how to change the current file processing to some other random file in awk ?

Hello, say suppose i am processing an file emp.dat the field of which are deptno empno empname etc now say suppose i want to change the file to emp.lst then how can i do it? Here i what i attempted but in vain BEGIN{ system("sort emp.dat > emp.lst") FILENAME="emp.lst" } { print... (2 Replies)
Discussion started by: salman4u
2 Replies

9. UNIX for Advanced & Expert Users

Reading a file and writing the file name to a param file.

Hi All, Not sure if this would be in a dummies sectiin or advanced. I'm looking for a script if someone has doen something like this. I have list of files - adc_earnedpoints.20070630.txt adc_earnedpoints.20070707.txt adc_earnedpoints.20070714.txt adc_earnedpoints.20070721.txt... (1 Reply)
Discussion started by: thebeginer
1 Replies

10. Shell Programming and Scripting

Reading file names from a file and executing the relative file from shell script

Hi How can i dynamically read files names from a list file and execute them from a single shell script. Please help its urgent Thanks in Advance (4 Replies)
Discussion started by: anushilrai
4 Replies
Login or Register to Ask a Question