Saving code?

This is a discussion on Saving code? within the C Programming forums, part of the General Programming Boards category; I'm rather new to C and I'm making my first lengthy program. I wish for the user to be able ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    3

    Saving code?

    I'm rather new to C and I'm making my first lengthy program. I wish for the user to be able to save their progress in the middle of the program so they do not have to start from scratch upon closing it.

    Is file manipulation required, or is there a simpler way?

  2. #2
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    Just print the information into a file and then when they open the program again read the information from the file.
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  3. #3
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    i have two answers for you.

    1. save game and load game

    give luser choose which work to load. option.

    2. append file

    add to current work or datas

  4. #4
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by superpickle
    I'm rather new to C and I'm making my first lengthy program. I wish for the user to be able to save their progress in the middle of the program so they do not have to start from scratch upon closing it.

    Is file manipulation required, or is there a simpler way?
    Its unclear precisely what 'progress' you wish to save.
    However to save something after a program has terminated
    you will need to use a file in one way or another.

    A simple way to save info is to redirect stuff the program
    prints into a file (printf).

    So you the say:-

    Myprogram.exe >saveoutput.txt
    and then all your printf outputs are saved in the txt file.

  5. #5
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Its all about how you can represent this information in your
    program - printing all output to a file may not be a good
    idea - how would your program interpret this information when
    you want to reload?

    Its hard to give a specific solution without knowing what
    information your game has in use, so I'll just show describe a
    basic scenario:

    in your game, a user has a current level and a current score.
    you want to save this data to be restored later. To save,
    write those two numbers out to a file - use some form of
    delimiting - for example, putting the two numbers on separate
    lines, or using a specific character to delimit (eg a space).

    Then to load, read that information back in - the order being
    important - you don't want a person to save a game on level
    10 and a score of 5000, then reload to level 5000 with a score
    of 10!

    Hope that clarifies the matter. In summary

    1) output the data in a manner in which it can be retieved easily
    from the file
    2) remember to assign the correct data to the correct variables

    When you read in these numbers (particularly the level), you're
    program examines the data and decides what level to go to.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    3
    I'm still not sure what the actual code should be, and I don't want to save all my printfs.

    Say I have variables A, B and C (type int if it's necessary). I need code to save the values of the three variables to a file, and another code to be able to read them upon prompt (I can do that with scanf and an if-else). What code would I use to accomplish these tasks?

  7. #7
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Take a look at this little program I wrote - demonstrates
    file io and should be sufficiently commented.

    Code:
    #include <stdio.h>
    
    int main (void)
    {
    	int a = 10, b = 20, c = 30; /*variables you want to save*/
    	int x, y, z;	/*just using these to show you reloading in operation*/
    	
    	FILE *in;	/*I could have used 1 pointer twice, but I decided this may be clearer for you*/
    	FILE *out;
    
    	/*SAVING DATA*/
    	if ((out = fopen("savefile.txt", "w")) == NULL)
    	{
    		printf ("Error opening file for writing");
    		return 1;
    	}
    
    	fprintf (out, "%d %d %d", a, b, c); /*writing out separated by spaces*/
    
    	fclose (out);
    
    	/*END SAVING DATA*/
    
    	/*LOADING DATA*/
    
    	if ((in = fopen ("savefile.txt", "r")) == NULL)
    	{
    		printf ("Error opening file for reading");
    		return 1;
    	}
    
    	fscanf (in, "%d %d %d", &x, &y, &z);	/*reading in separated by spaces*/
    
    	fclose (in);
    
    	/*END LOADING DATA*/
    
    	printf ("\nX = %d, Y = %d, Z = %d\n\n", x, y, z);
    
    	printf ("Press Enter to quit");
    	getchar ();
    
    	return 0;
    }
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 08:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 11:20 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21