Thread: storing variables permanentely

  1. #1
    C++Pandit
    Join Date
    Jul 2008
    Posts
    49

    storing variables permanentely

    how does one store a number or a character permanently in c++ .
    for ex

    the program asks you for a number lets say 5
    and every time you run the program it prints 5?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    To store anything permanently, you'd write it to a file.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Like anon said, write it to a file. I write my variables (or option choices) to .ini files and use the GetPrivateProfileString/Int functions (declared in windows.h) to get them every time the program starts.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There are many ways.
    Other ways include XML, raw data and readable data of own choosing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you want something more portable than what scwizzo uses, you can either write code to parse your own file format, use a portable library that parses an existing file format, or use a database management system. All these are fundamentally variants of the same theme of writing to file.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you happen to be on a mainframe, you can write the data to CSA memory. Other OS's have shared memory, but I think one program would always need to be running to keep the shared memory handle open.
    But obviously a reboot would lose the data in these cases. Saving to a file is the only way for data to survive a reboot.

  7. #7
    C++Pandit
    Join Date
    Jul 2008
    Posts
    49
    can an novice like me understand it or what should i do understand it?

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    166
    Carve it into rock, then it will be there for generations to come.

    Then every time a person runs your program the program sends you an email, when you get the email you check the rock for what the value is and reply to the email with that value. Should be the same value everytime.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Saimadhav View Post
    can an novice like me understand it or what should i do understand it?
    Writing to a file is easy (you can use << just like writing to the screen -- google for ofstream and ifstream). The other point we're trying to get you to think about is deciding what you want to write to the file; ain't nobody gonna be able to do that but you. Do you need a bunch of stuff? Do you always want to have the same data, or are some things optional? The more flexible your data file is, the more complicated your program is going to get.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing classes in variables?
    By jw232 in forum C++ Programming
    Replies: 10
    Last Post: 02-19-2009, 06:34 PM
  2. storing alot of variables...
    By MikeyIckey in forum C Programming
    Replies: 11
    Last Post: 05-30-2008, 12:31 PM
  3. Variable Storing in memory
    By karb0noxyde in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2004, 07:31 PM
  4. Replies: 2
    Last Post: 01-04-2004, 05:52 PM
  5. inputting of files and storing to variables
    By curtner in forum C++ Programming
    Replies: 2
    Last Post: 10-11-2001, 03:10 PM