Well , I am trying to create a bees game as part of my assignment ..
Now in this simple game I should use a text file .. and the text file is to create different game stats in different lines..

eg..
"10 (number of bees)
0 (number ofinfected bees)
1000 (money balance)
40 (amount of honey)
0 (amount of beeswax)"

I am trying to create the function that is to update the file but can't manage to complete it.
in the function should I pass all the variables ? .. and then use fprintf and recall them there ?

void updatefile( //should I pass anything there ? )
{
	FILE *f;
	f=fopen("gamestat.txt","w");
	fprintf(f, 
  	.....
	fclose(f);
}

The question is impossible to answer without more info from you. We don't know what variables might be needed, we don't know which of them might be global, we don't know if there are any structures set up, we don't know precisely what you need to save in the file in order to recreate the game(you've given a start on that one, but one can't assume that each element of that list is stored in a variable. Perhaps it is, perhaps it isn't), etc.

printf probably wouldn't be used in the function. If you're writing to a file, you'd likely use fprintf. Again, we'd need more information before advising you on the exact functions to use.

Can you be a little more explicit about the program requirements? Like, what should the file look like before and after updatefile() is called, in a sample run? The sample code you've written would overwrite anything already in the file, but you called it "update", so... I'm a little puzzled.

I can tell you right now, you don't need to pass it anything. Whether you should is a question of what will best fit the program requirements and make your code cleanest, and is ultimately up to you. You might want to at least pass the name of the file ("gamestat.txt").

printf probably wouldn't be used in the function. If you're writing to a file, you'd likely use fprintf. Again, we'd need more information before advising you on the exact functions to use.

yes yes .. i typed fprintf infact ..

well potential variable names that I would use are:
numberofbees
numberofinfected bees
moneybalance
amountofhoney
amountofbeeswax
and I intend to make them global since they are to be used in each function ..

(This might give you a clearer picture http://goo.gl/AJ9uu)

In the
void (....) part at the beginning of the function ..
should i mention all the variable names (in brackets) and then use them in the function in this way .. fprintf(f, "%d \n", numberofbees); eg.. ?

Thanks

Can you be a little more explicit about the program requirements? Like, what should the file look like before and after updatefile() is called, in a sample run? The sample code you've written would overwrite anything already in the file, but you called it "update", so... I'm a little puzzled.

I can tell you right now, you don't need to pass it anything. Whether you should is a question of what will best fit the program requirements and make your code cleanest, and is ultimately up to you. You might want to at least pass the name of the file ("gamestat.txt").

Yes yes , i shhould have told here. The lecturer advised us (to make things simpler) that for eg when something is altered.. The file is written from the start (overwrited will be the correct term i think)

So, I should pass the name of the file .. not its variables? I thought assigning to a variable in the function was enough ..

Basically there are two ways to do this, which your instructor probably is trying to get you to think about.

One, you can give the inventory variables "file scope" -- that is, declare them outside of and before your functions so that they'll be available in updatefile() as well as main() and whatever other functions might need them. This is the simplest solution and it doesn't involve passing any variables other than the file name. It has a few drawbacks you should be aware of, e.g. you couldn't easily extend the program to track the inventory of two or more players at once. It would be a poor solution for production code, but is probably fine for an assignment at your level (I'm guessing CS1).

Two, you can declare all your inventory variables inside main() and pass them as function parameters whenever they're needed. That means not only do you need to pass bees, infected, balance, honey, and beeswax to updatefile() (in addition to the file name), but you also need to pass money to buybees(), because that function needs to know how much money is in your inventory to do error checking. This is a little more complicated to implement, but it's more modular and easier to read (since the variable declaration is always close to its use). Depending on your grader, you might get higher marks for this solution than for the other, but it requires using pointers to change the value of money, which you might not have covered in class yet.

There's a hybrid solution (declare money with file scope and pass the others in to updatefile() as parameters) that avoids changing the parameters of buybees() and doesn't require using pointers; however, that solution is ugly and I don't recommend it.

Neither of these is the best solution to the problem given, but I'm trying to help you make one simple decision, not teach you the entire language. I'd suggest trying both approaches, in different files -- they only differ by like 10 lines, max, if you do them right. Ask your professor if it's okay to change the parameters of buybees(). If he says yes, turn in solution 2; if he says no, turn in solution 1. You'll learn more from doing it both ways than you would from me telling you which way you should do it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.