Click here to Skip to main content
15,029,715 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have entered my data in my text file as below
a = 1
b = 10
c = 15 and so on
I want this data to be read and directly be assigned to variables a,b,c in my C++ code.
I want to write a code based on this so that whenever I change in my text file, my variables get automatically updated when I run the code.
And I want to assign different data types for my variables and this automation should also be done.

What I have tried:

My text file looks like
Then I read separately line by line in my code and assigned it my desired variable simultaneously. But I want the values to be read directly to the variables specified in my text file automatically.
Updated 25-Feb-19 1:09am

You could have pseudo-variables. Starting, for simplicity, with integer variables, you might use a std::map<std::string, int> (or a std::unordered_map<std::string, int>) to, well, map you variable names to their integer values.

Another, completely different, approach would be using an embeddable script language, like Lua (see, for instance this page: Programming in Lua : 25[^]).
Basically, you can't. You can't "invent" variables, and you certainly can't change datatypes "on the fly" in your code because once compiled your code doesn't really have those variables anymore - they are optimised into the code and may become registers, or even get removed completely. Even if they aren't, then the compilation process selects the appropriate functions to operate on the specific datatype and that can't change once compiled.

There are probably ways to achieve the end you want - but we have no idea exactly what you are trying to do so we can't suggest them based on so little information. It's certainly possible to read values from a file, and use them inside your app, but you need to think about what you are trying to in better detail before you continue - you are not heading down the right route at present.
It is a bit unusual what you want, but you may read the file and than scan each line like in this example code and find some pattern like "a =" and than switch to a scanf for the value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900