Thread: getting variables from an external .cfg file

  1. #1
    Registered User Ion Blade's Avatar
    Join Date
    May 2002
    Posts
    35

    getting variables from an external .cfg file

    as i mentioned in my last post i don't know much about file streams... my question is, how can i have my program read from a line in a specified file, say that line would be:
    Code:
     ;version = 1.0
    and then the program assigns it's own variable called "version" to that number 1.0 in that line of yes, THAT file?

    note: assume the file is just called settings.cfg
    "Im going to have peaceful dreams of brackets and semicolons strapped on crucifixes, screaming for mercy" - Someone who doesn't like programming.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Something like

    - Open File
    - Read Line
    - If line doesn't start ";version" Goto Read Line

    Now we have the line
    >;version = 1.0
    Find the first = sign, assign everything after it to a variable as you see fit.

    et voila!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User Ion Blade's Avatar
    Join Date
    May 2002
    Posts
    35
    ok, but the thing is i don't know how to do that
    "Im going to have peaceful dreams of brackets and semicolons strapped on crucifixes, screaming for mercy" - Someone who doesn't like programming.

  4. #4
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    The thread below has some code I wrote to find occurences of a string in a file. It prints out the location in the file of the start of the string. Using fseek, you could jump to that point in a file and write to it. If your cfg file is not that large and never will be, you may just want to read the whole file into a set of variables line by line, and output a new one once you've manipulated the variable, delete the old one, then rename your new file in place of the old file (I'm not suggesting that is the best way, and certainly not the safest if this is a a critical bit of software; but it's what just sprang to mind, and it would be easy).

    Be aware of Shadows input re: eof, which has not been implemented in the code I posted in the thread below.

    http://www.cprogramming.com/cboard/s...threadid=18036

    I hope it at least gives you some directions to look. Have a crack and post some code, people will pitch in
    Demonographic rhinology is not the only possible outcome, but why take the chance

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, 09:35 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. debug to release modes
    By DavidP in forum Game Programming
    Replies: 5
    Last Post: 03-20-2003, 03:01 PM