Teen Programmers Unite  
 

 

Return to forum top

Saving Variables

Posted by Faisal [send private reply] at April 17, 2002, 04:36:07 PM

Is it possible to save Variables onto a file or something??

Posted by Faisal [send private reply] at April 17, 2002, 04:36:35 PM

Like Game Points?

Posted by Psion [send private reply] at April 17, 2002, 05:01:10 PM

Yes, in what format do you want to save them and what programming language are you using?

Posted by diegoeskryptic [send private reply] at April 17, 2002, 05:02:30 PM

Can sumone explain to me what a file is?

Posted by metamorphic [send private reply] at April 17, 2002, 05:22:48 PM

Something that you use on your nails ;)

Posted by Mycroft [send private reply] at April 17, 2002, 05:27:25 PM

a file is any data file, such as a text file, .mod file, anything that is not exacutable.

Posted by Cobbs [send private reply] at April 17, 2002, 05:40:25 PM

not really, executable FILES are files

Posted by CodeRed [send private reply] at April 17, 2002, 07:07:51 PM

A file is a logical grouping of data stored on a physical medium.

Posted by CodeRed [send private reply] at April 17, 2002, 07:09:38 PM

...and for heavens sakes all you do is output the contents of the variables to a file in a logical order, so that you know how to use that file later. If you need help with file IO, thats a different question.

Posted by Sord [send private reply] at April 17, 2002, 07:41:18 PM

an ini file would be good for a game, or you could save to the registry (assuming that youre using windows). like Psion said, it depends on the language

Posted by vikram_1982 [send private reply] at April 18, 2002, 06:04:17 AM

This i the algorithm for doing it: I am assuming the lang is C

1. OPen the file in write/append mode . : fopen stmt
2. Write into the file using the output statement, but include the name of the filehandler

3. Close the file : fclose

This forum prohibits display of actual program code. Hence I am giving u the alg. If u want the program stmts, feel free to mail me. The address is in the profile.

Posted by RedX [send private reply] at April 18, 2002, 01:48:40 PM

If you're using C++. Make a ofstream object

#include <iostream>
#include <fstream>

std::ofstream debugFile;

int main()
{
   std::cout << "begin" << std::endl;
   int someInt = 123;
   char someChar = 'c';

   debugFile.open("debug.txt",std::ios::app);
  
   debugFile << someInt << someChar << "a string" << std::endl;

   std::cout << "done" << std::endl;
   return 0;
}


It makes an object like cout but it outputs to a file.

RedX
Posted by RedX [send private reply] at April 18, 2002, 01:50:28 PM

Forgot one line at the end:

debugFile.close();

should go just before the :

return 0;
}

RedX

Posted by Sord [send private reply] at April 18, 2002, 02:00:57 PM

The only bad thing about your code, is that you can only save one thing to it unless you make a parser, and save it to a certain format.
For example, you could make it follow the C/C++ format by seperating things by a ";" ie(Score=100;Lives=1;)

Posted by RedX [send private reply] at April 18, 2002, 02:33:54 PM

Actually reading it in isn't difficult and a seperator is unnecessary.

 
ofstream fout;
ifstream fin;
int score = 150;
int lives = 3;

fout.open(...);
fout << score << lives;
fout.close();

fin.open(...) 
fin >> score >> lives;
fin.close();


I haven't tested a parser in binary mode(only sequences of variables, not a real parser), but in ASCII a simple parser isn't a problem.

In ASCII I used a file like:

SomeName 12
SomethingElse 15

Then reading it:
string foo;
int a;
int b;
cin >> foo >> a; 
cin >> foo >> b;


this reads the 2 ints and removes the strings before it.
By checking foo before reading the next string you can determen what the next variable will be.
Ofcourse with they nessesary code to check for errors.

This might not be so efficient in terms of diskspace, but it is totally portable (No little/big endian problems) and it doesn't need a special editor to make/change it. (notepad or edit will do)

This ofcourse won't do for big fast parsers, but it will do for keeping track of user settings, so you can change the background color of your app without recompiling.

And for binary files: overload the '>>' and '<<' operators in your class and use those to save and load the data.
I haven't done this yet, but it makes things a lot easier.

TsomeData data;   // class with lots of data
ifstream fin;
ofstream fout;

fin.open(...);
fin >> data;
fin.close();

DoStuffWith(data);

fout.open(...);
fout << data;
fout.close();
DoSomeOtherStuff();



If you really need a big parser, then look for both Yacc and Bizon. These are classics for parsing.

RedX
Posted by Psion [send private reply] at April 18, 2002, 05:23:56 PM

You really ought to be saving fields directly, not converted to string representations. For instance, output an integer as its 4 bytes, and then no "parsing" is necessary to read it back in. The only reason you might not want to do this is that it can be more work if your datafiles are used on machines with different representation conventions.

Posted by Faisal [send private reply] at April 21, 2002, 12:37:45 PM

i am kind of lost... i just starting learning about fstream yesterday.

Posted by Faisal [send private reply] at April 21, 2002, 12:38:07 PM

in my book

Posted by gian [send private reply] at April 22, 2002, 03:11:46 AM

Vlad: Who ever said giving code solutions was banned? It is encouraged! What we don't like is people giving answers to things that people should know before they proceed: eg.
"How do I do a function in C for my 3d directx game?".

Faisal: Keep reading, you will get it soon enough. Ask any specific questions.

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.