简体   繁体   English

从MFC中的配置文件加载字符串

[英]Loading a string from a config file in MFC

I have installed Postgres database and I am trying to connect to it from C++ code (VC++). 我已经安装了Postgres数据库,并且试图从C ++代码(VC ++)连接到它。 Instead of passing hard-coded values to the Connect function ( edit: PQconnectdb ? ), I would like to write a config file and read the values and send it to connect function. 而不是将硬编码的值传递给Connect函数( 编辑: PQconnectdb ),我想编写一个配置文件并读取这些值并将其发送给connect函数。

How can I do it in vc++? 如何在vc ++中做到这一点? Please help me. 请帮我。

CString Connect= _T("host=x.x.x.x port=5432 dbname=xxxxxx user=cdfvv password=hccjjj"); 

Instead of assigning value like this, I would like to write a config and process it. 我不想编写这样的值,而是想编写一个配置并对其进行处理。

( I have edited your question with my best guess as to what you are actually trying to ask. ) 对于您实际要问的问题,我已经用最好的猜测编辑了您的问题。

MFC provides the CWinApp::GetProfileString and CWinApp::SetProfileString methods, which abstract configuration to use an ini file or the registry. MFC提供了CWinApp::GetProfileStringCWinApp::SetProfileString方法,这些方法抽象化配置以使用ini文件或注册表。 If you're using MFC, which it looks like you are, you should use these methods. 如果您使用的是MFC,则应使用这些方法。

Otherwise, on Windows, consider using the win32 Registry APIs which is the normal way to store application configuration on Windows. 否则,在Windows上,请考虑使用win32注册表API ,这是在Windows上存储应用程序配置的常规方法。

Or use an XML configuration file if you prefer; 如果愿意,也可以使用XML配置文件。 see the manual for MSXML . 请参阅MSXML手册

Alternately, there are numerous 3rd party libraries for parsing and storing configuration files in various formats, including .ini -style. 另外,还有许多第三方库可用于解析和存储各种格式的配置文件,包括.ini样式。


Personally I recommend that you do not use MFC, which I am guessing you are using based on your use of the CString class (?). 就个人而言,我建议您不要使用MFC,我猜您是根据对CString类(?)的使用而使用的。 IMO MFC is obsolete. IMO MFC已过时。 Use plain C++ if you're writing a console application. 如果要编写控制台应用程序,请使用纯C ++。 If you're writing a GUI app, consider using Qt, or make your life easier by using C# and WinForms. 如果要编写GUI应用程序,请考虑使用Qt,或者使用C#和WinForms简化生活。


(By the way: I've never written an MFC application as I only work on cross-platform applications. I just did a few Google searches.) (顺便说一句:我从未编写过MFC应用程序,因为我只在跨平台应用程序上工作。我只做了一些Google搜索。)

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

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