3 Replies - 416 Views - Last Post: 20 February 2012 - 01:06 PM Rate Topic: -----

#1 xkaijinx  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 09-March 11

Updating input data in a file

Posted 19 February 2012 - 11:24 PM

Hey guys,

So I've been making alot of progress in a program where I have two files.
(1) a file containing data for baseball players
(2) a file where the data outputs when ran

I included a menu in the program, but I am trying to figure out how I would change the info in the file.

Lets say I wanted to change the stats on a player how does one change those? I know I need to make another function, beyond that no idea. I figured out how to include functions as well as using the ifstream / ofstream. So progress is definitely being made.

For easier debugging purposes I separate my code into 3 files. The header file, and two source files.

**MAINPROGRAM.CPP**


#include "myFunctions.h"
#include <iostream>
#include <string> 
#include <fstream>

using namespace std; 

int main()
{
		char response; 

		ifstream in;  // The input stream
		ofstream out; // The output stream

		in.open("baseball.txt");  //This will input to the file
		out.open("baseballout.txt"); // This will output to the file

		cout << "Baseball player data:" << endl << endl;
		cout << "Please choose from one of the following menu options: " << endl << endl;
	
		cout << "A - Print player info: " << endl; 
		cout << "B - Search player name: " << endl << endl;
		cin  >> response; 

		if (response == 'A', 'a')
{
	
		cout << outputBaseballData;
}

		else if (response != 'A' )
		cout << "Thank you for using our system" << endl;
	
		baseball playerInfo[10];



	for (int i = 0 ; i < 10 ;  i++)
	{
		 inputBaseballData(playerInfo[i], in);
		 outputBaseballData(playerInfo[i], out);
	}

	system("pause");
	in.close();
	out.close();
	return 0;
}


**MYFUNCTIONS.CPP**

#include "myFunctions.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std; 



void inputBaseballData (baseball& stats, ifstream& input)
{
	
 
	input >> stats.Name;
	input >> stats.HomeRuns;
	input >> stats.Hits;

}

void outputBaseballData(baseball& stats, ofstream& output)
{
	output << "Player name: "<< stats.Name << endl; 
	output << "\n# of Home runs: " << stats.HomeRuns << endl; 
	output << "\n# of Hits: " << stats.Hits << endl; 
}



**MyFUNCTIONS.H (Header file) **

#include <string>
#include <iostream>
#include <fstream>

using namespace std; 


struct baseball
{
		string Name;
		int HomeRuns;
		int Hits;  

};

void inputBaseballData (baseball& stats, ifstream& input); 



void outputBaseballData (baseball& stats, ofstream& output);

This post has been edited by xkaijinx: 19 February 2012 - 11:25 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Updating input data in a file

#2 nuclearfroggy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 82
  • View blog
  • Posts: 167
  • Joined: 04-August 08

Re: Updating input data in a file

Posted 20 February 2012 - 12:55 AM

To find the player's information in the file you need something to identify each player. For this purpose their name is probably good. To find a player you'll want to read through the file comparing names, until the player is found, or the end of the file is reached. You know a name occurs every 3 lines, or basically if there's a mismatch you can ignore 2 lines containing the home runs and the hits.

Often, for things like this where we essentially have a database, it's much simpler to have fixed width entries. This makes it easier to find the next entry, but also makes it easier to overwrite entries. Using this method you'll be using seekp (for output) and seekg (for input), to tell the program where to write and read next. You'll also want to probably use read() instead to read in the fixed block of known characters.

See how far you can get with this. Hope it helps!
Was This Post Helpful? 0
  • +
  • -

#3 xkaijinx  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 09-March 11

Re: Updating input data in a file

Posted 20 February 2012 - 12:11 PM

So just to make sure.. I need to hard coat each of the names in the program so that someone could identify that player?
Was This Post Helpful? 0
  • +
  • -

#4 nuclearfroggy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 82
  • View blog
  • Posts: 167
  • Joined: 04-August 08

Re: Updating input data in a file

Posted 20 February 2012 - 01:06 PM

Oh no, what I meant is when you search through the file, what will you pass to the search function and use when searching through the file? You could pass an entire player structure and compare each field to see if you have a match, so you could ignore I ever said that. Otherwise you could assume each name is unique, or assign each player a unique number and when searching use the number.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1