I was just wondering, is it possible to store variables from a C++ program into another file then read and use the variables from the file at a later date? And if it is possible, how would you accomplish it? Please help!
I was just wondering, is it possible to store variables from a C++ program into another file then read and use the variables from the file at a later date? And if it is possible, how would you accomplish it? Please help!
What part are you having troubles with ? Yes it is possible.
Yes, it's possible. There are two ways you can do it: with binary files, and with text files.
With binary files, you write the actual representation of the variable in bytes to the file. You can then come along and read it at a later date. (This doesn't work very well if the data you wrote contains pointers to other data.) This method has several pros and cons:
- Pro: the files use very little disk space.
- Pro: it's very simple to implement.
- Con: the files will be rather unportable.
- Con: complex data types (with pointers and references) must be written with care.
- Con (pro?): difficult to create and edit by hand.
Or you can convert your data into a textual representation, write that to a file, and at a later date read it in again. For example, you could write 123.456 to a file, rather than the individual bytes that 123.456 is stored with on your particular computer. This method also has several pros and cons:
- Pro: completely portable.
- Pro (con?): easy to create and modify by hand.
- Con: slightly larger file sizes (usually unimportant -- 2KB instead of 300 bytes or something).
- Con: a little more difficult to implement in code.
I usually use text files, just because I like being able to edit them by hand. (This could be a disadvantage in some cases, but only very rarely.) I also love their portability. Who cares if it takes a few more lines of code . . . .
So, which sounds more appealing? I'm sure, if you ask, someone will give you examples of each . . . .
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Yes, examples of using text files would be nice. I want to use data stored in the file as variables.
Last edited by DarkAlex; 11-28-2007 at 09:57 PM.
Both methods let you do that . . . Mmm'kay, I'll show you an example of both. It's been a while since I used C++ file I/O, but here's a shot at it.I want to use data stored in the file as variables.
If you change the linesCode:// Text file I/O example #include <iostream> #include <fstream> // required for fstream int main() { double before = 3.14159265358979324, after; const char *filename = "file.txt"; std::ofstream output(filename); output << before; // just like you'd use cout output.close(); std::ifstream input(filename); input >> after; input.close(); // not strictly required, but hey -- it's a good habit std::cout << "Before: " << before << std::endl << "After: " << after << std::endl; return 0; }
toCode:std::ofstream output(filename); std::ifstream input(filename);
you get binary files.Code:std::ofstream output(filename, ios::binary); std::ifstream input(filename, ios::binary);
Note: I haven't tested that code or even tried compiling it. You'll have to try it and see what happens.
Also have a look at the output file generated. You can read it with the text file.
It should be noted that the binary file will almost certainly be more accurate. You can fix this by changing
toCode:output << before;
and including <iomanip>. Then the result from the text file should be reasonably close to the binary file.Code:output << std::setw(20) << before;
Last edited by dwks; 11-28-2007 at 10:14 PM.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Your code worked, thanks. How do I store/read multiple vaiable to/from a file?
In my code, just treat output as cout and input as cin.
Note that if you're writing to binary files, that output statement would work. But if you're writing to text files, you need to separate the variables, for example withCode:int one, two, three; // ... output << one << two << three; // ... input >> one >> two; input >> three;
Otherwise, if one held 100, two held 200, and three 300, the file would look like this:Code:output << one << ' ' << two << ' ' << three << std::endl;
And of course the program sees that as one number. You need whitespace between the numbers, likeCode:100200300
It's just like cin.Code:100 200 300
In fact, cout is a special output file and cin is a special input file. Anything you can do with cout and cin, you should be able to do with files.
You can do more with files, in fact. For example, you can open a file for input and output, so you can read and write from the same object.
Last edited by dwks; 11-28-2007 at 10:24 PM.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Thank you. I needed this for a text-based game I'm planning on making. This really helps.
You'll have to show it to us when it's done, so we can poke holes in your coding style. No, seriously, I'm sure it would be interesting to see.
You should also try learning from tutorials and references . . . I'm not online all the time, you know.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.