如何将文本文件中指定的变量值复制到我的c +代码中 [英] How can I copy values of variables assigned in a text file into my c+ code

查看:49
本文介绍了如何将文本文件中指定的变量值复制到我的c +代码中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的文本文件中输入了我的数据,如下所示

a = 1

b = 10

c = 15等等
我希望读取这些数据并直接将其分配给我的C ++代码中的变量a,b,c。

我想基于此编写代码,以便每当我改变时在我的文本文件中,当我运行代码时,我的变量会自动更新。

我想为我的变量分配不同的数据类型,这也应该完成自动化。



我尝试了什么:



我的文本文件看起来像

10

15

30

然后我在我的代码中逐行单独阅读并同时为其分配我想要的变量。但是我希望将值直接读取到我的文本文件中指定的变量。

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
10
15
30
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.

推荐答案

你可以有伪变量。为简单起见,使用整数变量,可以使用 std :: map< std :: string,int> (或 std :: unordered_map< std :: string,int> ),以及变量名称映射到它们的整数值。



另一种完全不同的方法是使用可嵌入的脚本语言,例如 Lua (参见例如此页: Lua编程:25 [ ^ ])。
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.


你想要的有点不寻常,但是你可以阅读文件而不是像这样扫描每一行示例代码并找到一些类似a =的模式,然后切换到 scanf 的价值。
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.


这篇关于如何将文本文件中指定的变量值复制到我的c +代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆