Variables

This is a discussion on Variables within the C++ Programming forums, part of the General Programming Boards category; Is there any way to store variables in a file, so that the program does not have to ask the ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    75

    Question Variables

    Is there any way to store variables in a file, so that the program does not have to ask the user for imput that will never change over and over again.

    EX: storing a name string forever in a file.

    Thanks

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes, do you mean like settings?

    If so then you should look at ini or 'conifg' files. There are a few C++ ini classes on the interzweb, that could mean your file looks like:
    Code:
    [settings]
    name="Some string"
    And then,
    Code:
    string name = iniClass.read("somefile.ini", "settings", "name");
    Or something

    Or just write the name to a file, and read the file to get the name (no formatting, just a name in a file).
    Last edited by zacs7; 11-04-2007 at 07:21 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    If it's the same every time you run the program, then store it in the program itself.

    Otherwise, to store it in a file just use fstreams. Use an ofstream to write it out when you get it, and an ifstream to read it in when you need it.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    75

    Question

    Quote Originally Posted by Daved View Post
    If it's the same every time you run the program, then store it in the program itself.

    Otherwise, to store it in a file just use fstreams. Use an ofstream to write it out when you get it, and an ifstream to read it in when you need it.
    It will vary depending on the name of the user. Could you show me how to store it in fstreams. Thanks

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    That's a big topic for a simple post. There is a tutorial on fstreams on the main site, and it should also be explained in detail in your book.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 01:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 03:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 04:25 AM
  5. Replies: 6
    Last Post: 01-02-2004, 01:01 PM

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