1

I've been searching all over and cannot figure out how to do this. I am trying to read in a text file with contents like

x 4
y 6
z 9

and set the int values to the corresponding variable name before it; is there anyway to do this or do I have to assign the names in the program after reading in the values.

dsolimano
  • 8,870
  • 3
  • 48
  • 63
zach adams
  • 19
  • 4
  • you mean you want to read the variable names from the file too? – yano Jan 23 '18 at 22:28
  • 1
    What is the question? How to read a file? How to assign a variable? – Eugene Sh. Jan 23 '18 at 22:28
  • 6
    you have to do it the hard way, c does not have any reflection capabilities – pm100 Jan 23 '18 at 22:28
  • 2
    I think you're looking for a dictionary - https://stackoverflow.com/questions/4384359/quick-way-to-implement-dictionary-in-c, does that look right? – dsolimano Jan 23 '18 at 22:29
  • yes i was just trying to figure out if it was possible to read the name then read the int value and assign the int value to the correspoding name i read in so for example i read x and 3 then will set and int x=3 – zach adams Jan 23 '18 at 22:31
  • if you really need this type of capability you can use a dictionary as per @dsolimano - or you could have an array of variables and have `vn 42` in your file and fill out the nth value in the array. – pm100 Jan 23 '18 at 22:31
  • 2
    no "easy" way to do this. If you know the variable names ahead of time simply declare `int x, y, z;` in your code and assign the appropriate one when you parse the file. A linked list with `struct node{ char* varName; int varValue; };` is also an option, but that is overkill. – yano Jan 23 '18 at 22:38
  • The simple, bad, but maybe good enough way to do it would be to read each line, parse the elements, and use a big switch() to assign values: case 'x': x=data; It would be better to rewrite your program so that the data is held in a more structured way, like with a dictionary, as @dsolimano said. – Ask About Monica Jan 23 '18 at 22:39
  • Either do it the hard way, or use [x-macro](https://en.wikipedia.org/wiki/X_Macro). – HolyBlackCat Jan 23 '18 at 22:42
  • I just made my values another c file and just defined them as global variables and linked them to my main file. – zach adams Jan 23 '18 at 22:55
  • 1
    Sounds like an [XY Problem](http://xyproblem.info/). What problem are you _actually_ trying to resolve? – Jabberwocky Jan 23 '18 at 23:02
  • https://imgur.com/78K5qDF – zach adams Jan 23 '18 at 23:45
  • this was what was on my assignment and was a little confused – zach adams Jan 23 '18 at 23:45

2 Answers2

2

There are a couple of methods you could use to get the data into the program associated with a name, depending on how your program is structured.

If your program doesn't contain variables with those names already, then there's not a way to generate variables from a file. You could create a dictionary mapping the names to the values and use that as your "variable".

I suppose you could also have a dictionary mapping the names to the addresses of existing variables... so that you look up the name and then deref the variable to set if its in the dictionary.

As a really far-out solution... if you can recompile each time there is a new file (ie. the file is really compile-time input, not run-time input) then you might be able to use some kind of preprocessing to #include the (possibly itself preprocessed into c-code) file of data.

Shadow Radiance
  • 1,349
  • 13
  • 20
0

I think you are asking for dynamic code generator those I used for different purpose back then (generating long byte arrays with certain values). The generator will generate a .c file with variable names read from another file. This problem is, it's hard to do this for runtime, if you asking for runtime solution, other dudes are already given the answer.

the kamilz
  • 1,860
  • 1
  • 15
  • 19