Store variables in another file

This is a discussion on Store variables in another file within the C++ Programming forums, part of the General Programming Boards category; I was just wondering, is it possible to store variables from a C++ program into another file then read and ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105

    Store variables in another file

    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!

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    What part are you having troubles with ? Yes it is possible.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    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 08:57 PM.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I want to use data stored in the file as variables.
    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.

    Code:
    // 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;
    }
    If you change the lines
    Code:
        std::ofstream output(filename);
        std::ifstream input(filename);
    to
    Code:
        std::ofstream output(filename, ios::binary);
        std::ifstream input(filename, ios::binary);
    you get binary files.

    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
    Code:
    output << before;
    to
    Code:
    output << std::setw(20) << before;
    and including <iomanip>. Then the result from the text file should be reasonably close to the binary file.
    Last edited by dwks; 11-28-2007 at 09: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.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    Your code worked, thanks. How do I store/read multiple vaiable to/from a file?

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    In my code, just treat output as cout and input as cin.
    Code:
    int one, two, three;
    // ...
    output << one << two << three;
    // ...
    input >> one >> two;
    input >> three;
    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 with
    Code:
    output << one << ' ' << two << ' ' << three << std::endl;
    Otherwise, if one held 100, two held 200, and three 300, the file would look like this:
    Code:
    100200300
    And of course the program sees that as one number. You need whitespace between the numbers, like
    Code:
    100 200
    300
    It's just like cin.

    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 09: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.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    Thank you. I needed this for a text-based game I'm planning on making. This really helps.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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