如何将变量分配给持久数据?

时间:2021-06-02 04:27:28

Basically, I want to have my program retrieve various variables from the hard drive disk for use in the program. I guess, before asking for details, I should verify that is even possible. Is it?

基本上,我想让我的程序从硬盘驱动器磁盘中检索各种变量,以便在程序中使用。我想,在询问细节之前,我应该验证这是可能的。是吗?

If it is, what is the best way? I've seen the use of .ini files, but I don't know how to retrieve data specifically from .ini files. Is it the same as a text file that you have messed with to make sure variables will always be on x line? For instance:

如果是,最好的方法是什么?我见过使用.ini文件,但我不知道如何从.ini文件中专门检索数据。是否与您搞砸的文本文件相同,以确保变量始终位于x行?例如:

ifstream variables("variables.ini");

//some code later
//assume line 'x' holds a variable

if(variables.good())
{
   variables.get(x);
}

//or whatever =/

I'm kind of at a loss for what to do here.

我在这里做什么都有点不知所措。

Edit: my language of choice is C++.

编辑:我选择的语言是C ++。

3 个解决方案

#1


The first questions you need to address are the who and the why. Your options on the how will follow from those.

您需要解决的第一个问题是谁和原因。您将如何选择这些方案。

So who (or what) will be accessing the data? If it is just the program itself then you can store the data however you want - binary file, xml, database, ini file, whatever. However if the data needs to be easily accessible to the user so they can change it prior to a run then a text file like an ini, which can be easily edited, makes sense. In order to edit the data stored in other formats you may have to write an entirely separate program just to manipulate the stored parameters. Maybe that makes sense in your situation or maybe not but it will be more work.

那么谁(或什么)将访问数据?如果它只是程序本身,那么你可以存储你想要的数据 - 二进制文件,xml,数据库,ini文件,等等。但是,如果用户需要轻松访问数据,以便他们可以在运行之前更改它,那么像ini这样可以轻松编辑的文本文件是有意义的。为了编辑以其他格式存储的数据,您可能必须编写一个完全独立的程序来操作存储的参数。也许这在你的情况下是有意义的,也可能不是,但这将是更多的工作。

If you choose to go the ini route then you are on the right track. They are just text files. A common format is to have sections (usually in brackets), and then key/value pairs within the sections. Usually comment lines start with semicolon, a nice touch for the users who may want to flip back and forth between settings.

如果您选择进入ini路线,那么您就在正确的轨道上。它们只是文本文件。常见的格式是包含部分(通常在括号中),然后是部分中的键/值对。通常注释行以分号开头,对于可能想要在设置之间来回切换的用户来说是个不错的选择。

So something like this:

所以像这样:

[System]
    datapath = /home/me/data

[Application]
    start_count = 12
;   start_count = 20  //this is a comment

You don't have to worry about specific lines for your data. You just read through the file line by line. Empty or comment lines get tossed. You take note of what section you are in and you process the key/value pairs.

您不必担心数据的特定行。您只需逐行阅读文件。空或评论行被抛出。您注意到您所在的部分并处理键/值对。

There are many ways to store the parsed file in your program. A simple one may be to concatenate the section name and key into a key for a map. The value (of the key/value pair) would be the data for the map.

有许多方法可以将解析后的文件存储在程序中。一个简单的方法可能是将节名称和键连接成地图的键。 (键/值对的值)将是地图的数据。

So "Systemdatapath" could be one map key and its value would be "/home/me/data". When your program needs to use the values it just looks it up by key.

因此“Systemdatapath”可以是一个映射键,其值为“/ home / me / data”。当您的程序需要使用值时,它只需按键查找。

That's the basics. Ultimately you will want to embellish it. For instance, methods to retrieve values by type. E.g. getString(), getInteger(), getFloat(), etc.

这是基础知识。最终你会想要修饰它。例如,按类型检索值的方法。例如。 getString(),getInteger(),getFloat()等。

#2


You decide the format of the .ini file of your application. I usually work with XML because then you can organize your information by scope and there is already a bunch of libs to handle storing and retrieving information from XML trees.

您决定应用程序的.ini文件的格式。我通常使用XML,因为那时你可以按范围组织你的信息,并且已经有一堆lib来处理从XML树中存储和检索信息。

Edit: for C++ - http://xerces.apache.org/xerces-c/

编辑:for C ++ - http://xerces.apache.org/xerces-c/

#3


If you're going to be pulling values from the files frequently, and especially if you need to update(persist) those values, a database is a great option. It also will give you good performance as your dataset grows.

如果您要经常从文件中提取值,特别是如果您需要更新(持久化​​)这些值,则数据库是一个很好的选择。随着数据集的增长,它也将为您提供良好的性能。

#1


The first questions you need to address are the who and the why. Your options on the how will follow from those.

您需要解决的第一个问题是谁和原因。您将如何选择这些方案。

So who (or what) will be accessing the data? If it is just the program itself then you can store the data however you want - binary file, xml, database, ini file, whatever. However if the data needs to be easily accessible to the user so they can change it prior to a run then a text file like an ini, which can be easily edited, makes sense. In order to edit the data stored in other formats you may have to write an entirely separate program just to manipulate the stored parameters. Maybe that makes sense in your situation or maybe not but it will be more work.

那么谁(或什么)将访问数据?如果它只是程序本身,那么你可以存储你想要的数据 - 二进制文件,xml,数据库,ini文件,等等。但是,如果用户需要轻松访问数据,以便他们可以在运行之前更改它,那么像ini这样可以轻松编辑的文本文件是有意义的。为了编辑以其他格式存储的数据,您可能必须编写一个完全独立的程序来操作存储的参数。也许这在你的情况下是有意义的,也可能不是,但这将是更多的工作。

If you choose to go the ini route then you are on the right track. They are just text files. A common format is to have sections (usually in brackets), and then key/value pairs within the sections. Usually comment lines start with semicolon, a nice touch for the users who may want to flip back and forth between settings.

如果您选择进入ini路线,那么您就在正确的轨道上。它们只是文本文件。常见的格式是包含部分(通常在括号中),然后是部分中的键/值对。通常注释行以分号开头,对于可能想要在设置之间来回切换的用户来说是个不错的选择。

So something like this:

所以像这样:

[System]
    datapath = /home/me/data

[Application]
    start_count = 12
;   start_count = 20  //this is a comment

You don't have to worry about specific lines for your data. You just read through the file line by line. Empty or comment lines get tossed. You take note of what section you are in and you process the key/value pairs.

您不必担心数据的特定行。您只需逐行阅读文件。空或评论行被抛出。您注意到您所在的部分并处理键/值对。

There are many ways to store the parsed file in your program. A simple one may be to concatenate the section name and key into a key for a map. The value (of the key/value pair) would be the data for the map.

有许多方法可以将解析后的文件存储在程序中。一个简单的方法可能是将节名称和键连接成地图的键。 (键/值对的值)将是地图的数据。

So "Systemdatapath" could be one map key and its value would be "/home/me/data". When your program needs to use the values it just looks it up by key.

因此“Systemdatapath”可以是一个映射键,其值为“/ home / me / data”。当您的程序需要使用值时,它只需按键查找。

That's the basics. Ultimately you will want to embellish it. For instance, methods to retrieve values by type. E.g. getString(), getInteger(), getFloat(), etc.

这是基础知识。最终你会想要修饰它。例如,按类型检索值的方法。例如。 getString(),getInteger(),getFloat()等。

#2


You decide the format of the .ini file of your application. I usually work with XML because then you can organize your information by scope and there is already a bunch of libs to handle storing and retrieving information from XML trees.

您决定应用程序的.ini文件的格式。我通常使用XML,因为那时你可以按范围组织你的信息,并且已经有一堆lib来处理从XML树中存储和检索信息。

Edit: for C++ - http://xerces.apache.org/xerces-c/

编辑:for C ++ - http://xerces.apache.org/xerces-c/

#3


If you're going to be pulling values from the files frequently, and especially if you need to update(persist) those values, a database is a great option. It also will give you good performance as your dataset grows.

如果您要经常从文件中提取值,特别是如果您需要更新(持久化​​)这些值,则数据库是一个很好的选择。随着数据集的增长,它也将为您提供良好的性能。