简体   繁体   中英

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++). 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.

How can I do it in 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. If you're using MFC, which it looks like you are, you should use these methods.

Otherwise, on Windows, consider using the win32 Registry APIs which is the normal way to store application configuration on Windows.

Or use an XML configuration file if you prefer; see the manual for MSXML .

Alternately, there are numerous 3rd party libraries for parsing and storing configuration files in various formats, including .ini -style.


Personally I recommend that you do not use MFC, which I am guessing you are using based on your use of the CString class (?). IMO MFC is obsolete. Use plain C++ if you're writing a console application. If you're writing a GUI app, consider using Qt, or make your life easier by using C# and 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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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