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.
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, /*...*/);
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
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.
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;
constchar *Newline = "\r\n";
int WindowWidth = basic_string<WidthString, WidthValue, Newline>; // Problem on this line
}
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.