简体   繁体   English

您如何将变量导出到持久数据?

[英]How do you farm out variables to persistent data?

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. 我已经看到了.ini文件的使用,但是我不知道如何从.ini文件中专门检索数据。 Is it the same as a text file that you have messed with to make sure variables will always be on x line? 它是否与您弄乱的文本文件相同,以确保变量始终位于x行? For instance: 例如:

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 ++。

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. 如果只是程序本身,那么您可以按需要存储数据-二进制文件,xml,数据库,ini文件等等。 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. 但是,如果用户需要轻松访问数据,以便他们可以在运行之前更改数据,则可以轻松编辑像ini这样的文本文件。 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. 也许这对您的情况有意义,但可能会更多。

If you choose to go the ini route then you are on the right track. 如果您选择走ini路线,那么您在正确的道路上。 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. 通常,注释行以分号开头,这对于希望在设置之间来回切换的用户来说是一种不错的选择。

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". 因此,“ Systemdatapath”可以是一个映射键,其值为“ / home / me / data”。 When your program needs to use the values it just looks it up by key. 当您的程序需要使用值时,它只是按键查找它。

That's the basics. 这就是基础。 Ultimately you will want to embellish it. 最终,您将需要修饰它。 For instance, methods to retrieve values by type. 例如,按类型检索值的方法。 Eg getString(), getInteger(), getFloat(), etc. 例如getString(),getInteger(),getFloat()等。

You decide the format of the .ini file of your application. 您确定应用程序.ini文件的格式。 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. 我通常使用XML,因为这样您就可以按范围组织信息,并且已经有了很多库来处理从XML树中存储和检索信息。

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

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. 随着数据集的增长,它也将为您提供良好的性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM