Retrieving Variables from Config File

I'm trying to make a windows application that get's the window width and height from a config file. So far, everything I've seen that has to deal with files is based on console applications. I'm quite new at C++ and I do not know how to translate the console application tutorials so that they can be used in windows applications.

Here's some of the code (derived from book "Beginning Visual C++ 2008").
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Above here, I have the code set to accept the file as
//ifstream config("Config.ini");
	hWnd = CreateWindow
	(
		szAppName,
		L"Application Name",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		800,    // Needs to be configurable (window width)
		600,    // Needs to be configurable (window height)
		0,
		0,
		hInstance,
		0
	);
The code to read from a file should be the same for a console program as for a GUI; file operations aren't UI related. You just need to read the values from the file into variable and then pass these to CreateWindow().

But if you want to use an ini-file, you could use the calls GetPrivateProfileInt() and GetPrivateProfileString(). But note that these are seen as old fashioned these days: they are to support old, 16-bit apps (from the mid-90s and earlier...).

Nowadays it's common practice to store config information in the registry rather than a config/ini-file. See RegOpenKeyEx(), RegQueryValueEx(), RegCloseKey(), etc.

Andy
Last edited on
I think I get it. Would this code work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int getWidth(ifstream config)
{
    int wndwidthval;

    // What about various white-spaces in the lines?  Comments?
    string wndwidth = config.getline("resolution_x = " &wndwidthval);
    return wndwidthval;
}

// WinMain()

WndWidth = getWidth(config);

hDC = CreateWindow(/*...*/, WndWidth, /*...*/);
As it stands, the answer is nope.

Have you tried to compile this line???

string wndwidth = config.getline("resolution_x = " &wndwidthval)

See:
http://www.cplusplus.com/reference/string/getline/
http://www.cplusplus.com/reference/iostream/istream/getline/

If you use GetPrivateProfileInt()/GetPrivateProfileString() it's pretty straightforward.

But to read an ini-file with ifstream, you'll have to deal with

; Comment, to ignore

[window]
width=800
height=600
visible=true

[and]
key1=value 1
key2=value 2
key3=42

[etc]
...

Which means you have to:
- find the relevant section
- find the line with the key-value pair you want and read it
- split the value from the key (split at '=')
- trim and return the value

Andy
Last edited on
Is the config file something you already have, or can you create your own? As Andy said, if its pre-existing, then you likely need to parse it.

Don't know if this will help, but if you are trying to get the dimensions of the display, the GetSystemMatrics function (I believe that's the right name) will give this. I believe there are others too.

Also, the are C Runtime functions that read text files pretty easily.
Thanks for the references. I'll be looking over them for a little while.

----------------------------------------------------------------

I made a "concept" config file so that I will know exactly what is going to be in it.

Concept File:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[screen_display]
resolution_x = 800
resolution_y = 600
bits_per_pixel = 32
driver_mode = direct_x
display_device = D3D9 Driver Name 5100-HD
full_screen_mode = 1

[programs]
number_programs = 1
program_directory = programs/
base_resources_directory = resources/
program_0 = my_program_1/ ; Directory
program_0_name = My Program 1 ; Name

I know it's a lot for a newbie like me, so I'm just creating a simple application and expanding on it after each test to make it what I want.

The config file is not pre-existing. The only thing that's in place in the application is the "ifstream config("Config.ini");" part.

I am trying to get the dimensions of the display, but I'm also wanting to make those dimensions configurable. Thanks for the advice.
If you read carefully:
Unlike the c-string versions of istream::getline, these string versions are implemented as global functions instead of members of the stream class.
I don't understand what that means? Are they different means to getting the same results? Do they require different methods to retrieve the variables that they store?
I need some help identifying the problem with this code:

1
2
3
4
5
6
7
8
int GetWindowWidth(ifstream config)
{
	istream& getline(istream& is, string& str, char delim);
	char WidthString[14] = "resolution_x=";
	char WidthValue;
	const char *Newline = "\r\n";
	int WindowWidth = basic_string<WidthString, WidthValue, Newline>;  // Problem on this line
}
What do you think is supposed to happen on line 7 ???
It's supposed to retrieve the value in the config file that says:
resolution_x=800
or
resolution_x = 800
I think I found something that I'm looking for. As it turns out, I have to know how to handle strings before I can know how to handle files, which are VERY LONG strings! I believe I found my answer and I'm sorry that I couldn't convey what I was trying to do.

Here's the page I found which I believe has answered my questions.
http://www.mochima.com/tutorials/strings.html
Topic archived. No new replies allowed.