Thread: A question on file streaming in C

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    156

    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;
    }
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: [email protected]

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I've found a easier way. Why didn't gametutorials use this?
    Probably because you do no error checking:

    fp= fopen(filename, "w");
    Anyway, without knowing anything about what you're talking about (Where's your link? What tutorial? Where's the code you're comparing this too?), how am I supposed to guess why they didn't use your code?

    Besides, there are many ways to write to a file.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    156
    http://www.gametutorials.com

    click on tutorials, then C , and look for their file outputing tutorial.
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: [email protected]

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: A question on file streaming in C

    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?

    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.
    Last edited by quzah; 06-20-2002 at 07:44 PM.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    156
    ok.........calm down sir.

    From a newbie standpoint, how would......nevermind. Forget it ,I'll probably be bashed anyways.
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: [email protected]

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Is that the code from the tutorial ? If it is then:

    >I've found a easier way
    What easier way did you find ? Please explain, because it appears both your (Golden Bunny's) example and the tutorial use fprintf().

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Golden Bunny
    ok.........calm down sir.

    From a newbie standpoint, how would......nevermind. Forget it ,I'll probably be bashed anyways.
    I wasn't bashing you. I was asking how is that not simplistic? What point are you having trouble with?

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. A question about file streams.
    By Lawn Gnomusrex in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2008, 06:05 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM