Advanced File I/O

Indexing array to keep track of records in a file.

Page 1 of 1

1 Replies - 2697 Views - Last Post: 09 November 2008 - 11:12 AM Rate Topic: -----

#1 SCStudent86  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 06-October 08

Advanced File I/O

Post icon  Posted 09 November 2008 - 10:13 AM

I kind of see where you are going at Martyr with the whole indexing array idea. I've made it where the program keeps track of how many records are in file when the user enters in a new record every time, but where would I declare the indexing array? As an argument in the function definition or in the whole program itself???

void newRecords(Account &customer) 
{ 
	int record = 1;
	char response; 
 
	//Create file object and open file 
	fstream citizen("DataBase.txt", ios::out | ios::app /*| ios::binary*/); 
 
	//Test for file open error 
	if(!citizen) 
	{ 
		cout << "Error opening file. Shutting down.\n"; 
		exit(0); 
	} 
	do 
	{ 
		//If file opens correctly get information entered by user 
		//and write information to the file in binary mode. 
		cout << "Enter the following information:\n\n"; 
		cout << "Name:					  ";		 
		cin.ignore(); 
		cin.getline(customer.name, NAME); 
		cout << "Address:				   "; 
		cin.getline(customer.address, ADDRESS); 
		cout << "City, State, and Zip:	  "; 
		cin.getline(customer.cityStateZip, HOMETOWN); 
		cout << "Telephone Number:		  "; 
		cin.getline(customer.teleNumber, TELEPHONE); 
		cout << "Account Balance:		   "; 
		cin  >> customer.accountBalance; 
		while(customer.accountBalance < 0.00) 
		{ 
			cout << "Invalid! Customer's account balance must be greater\n" 
				 << "than or equal to $0.00. Please re-enter in customer's\n" 
				 << "account balance:\n"; 
			cin  >> customer.accountBalance; 
		} 
		cout << "Last Payement(mm/dd/yyyy): "; 
		cin.ignore(); 
		cin.getline(customer.lastPayment, LAST_PAYMENT);		 
		citizen.write(reinterpret_cast<char *>(&customer), 
					  sizeof(customer));
		cout << "\n\nDo you want to enter in another record(Y/N)?	"; 
		cin  >> response; 
		cin.ignore();
		//If the user enters in another record then the number of records will increase
		//from 0 --> 1 --> 2 --> n until user breaks out of the DO-WHILE Loop.
		if(toupper(response) == 'Y')
			record++;
	}while(toupper(response) == 'Y'); 
	citizen.close(); 
} 



Is This A Good Question/Topic? 0
  • +

Replies To: Advanced File I/O

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4188
  • View blog
  • Posts: 11,852
  • Joined: 18-April 07

Re: Advanced File I/O

Posted 09 November 2008 - 11:12 AM

It is a design choice on your part really. You could pass this array in as a function parameter when you go to add or remove items from the file. The idea is that whenever you go to add accounts or remove accounts or some how change accounts, you want the array to change as well. The array and the file will be in sync with one another. The only times I really see you needing to modify the actual index array is....

1) When you go to add a new account to the file. You will then need to add the account to the array.

2) When you remove an account from the file, you will have to remove it from the array.

3) When you change the name on the account, because you will have to change the name in the index array.

Try declaring your index array in your main function and passing it to your new account function. When you get through adding it to the file, simply add it on to the array.

You will then have a search function which you can call from main that will do your searching for you. It will take the array and the name you want to search for. It will then return an Account object that it fetched from the file.

:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1