A question on file streaming in C
Hey, just wanted to let you guys know. I just read some tutorials at gametutorials.com and the way they write to files is complicating. I've found a easier way. Why didn't gametutorials use this?
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *fp;
char filename[30]="SomeFile.txt";
fp= fopen(filename, "w");
fprintf(fp, "This is Test");
fclose(fp);
system("pause");
return 0;
}
Re: A question on file streaming in C
Quote:
Originally posted by Golden Bunny
Hey, just wanted to let you guys know. I just read some tutorials at gametutorials.com and the way they write to files is complicating. I've found a easier way. Why didn't gametutorials use this?
I don't see your problem? What exactly is your complaint? What is so hard to understand? For that matter, how is their example any different than yours?
Quote:
Code:
////////////////////// FPRINTF() ////////////////////////
// Like printf(), fprintf() works exactly the same. Instead of printing to the screen,
// It prints it to a file. You need to give it a file pointer as the first parameter.
fprintf(fp, "\n\n"); // This will move the file pointer down 2 lines in the text file.
fprintf(fp, "Player: %s\n", szName); // Here we save the Name of the player to the file.
fprintf(fp, "Health: %d\n", health); // Here we save the "health" of the player to the file.
fprintf(fp, "Gold: %d\n", gold); // Here we save the "gold" of the player to the file.
// fclose() passes in the file pointer and closes the file for us. (very important)
fclose(fp); // We must always close the file after we are done with it. It frees memory and make's it so we can access it again.
How the ____ is that confusing??
Do you just not know how to use printf? That is the whole point of using fprintf: so you can use it exactly like printf and print formatted data to a file.
Quzah.