ideas for render a string to a variable

This is a discussion on ideas for render a string to a variable within the C Programming forums, part of the General Programming Boards category; I'd like to teach the world to c...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    82

    ideas for render a string to a variable

    I'd like to teach the world to c

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    Huh?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    82
    ehem, cough, sorry ahd some problems there with awriting a post thinking I was logged in, but turns out I wasn't, so I lost it.

    Anyhow, keeping with the subject, I'd like to read in some variable values from a setting text file, which goes like so:
    varname = value
    so
    fscanf(filein, "%s = %i", varname, &value);
    would *roughly* be what I'd do ...

    If the order of the variables names is observed, and known to the program the job is not too difficult. But if not - and it would be more robust - the program needs to recognise the varname is the correct. strcmp() would be the way here and then a conditional assigning.

    Is there a more direct way? JUst looking for hints. I apologise for the poor wording.Please understand it's the second time I write this post.

    Thanks in advance. Cheers.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    So I take it that if it says "a = 0", then the variable a should be assigned 0?
    If so, then I suggest you use a map. There is no standard map in C, however, so you may need to google a bit to find a good one.
    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
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Still not sure what you mean, need to explain by giving a clearer example.

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    I think what he means is if having a config file like for example
    Code:
    Name = My Name
    Age = 123
    DecimalVar = 3.3
    he wants to parse the file and use the rvalue in his code by looking up the lvalue. If so then any implementation of a linked list should do, and you could do something like
    Code:
    struct Variable
    {
        const char* Name;
        void* Data;
        int VariableType;
        struct Variable* next;
    }
    
    struct Variable* list = NULL;
    ParseConfigFile("filename.txt", &list);
    const char* name = GetStringVar(list, "Name");
    int age = GetIntVar(list, "Age");
    float floatVar = GetFloatVar(list, "DecimalVar");
    SetStringVar(list, "NewStringVar", "Hello World!");
    SaveConfigFile("filename.txt", list);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM

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