/*
1 c++ 配置文件读取
*/
#pragma comment(lib,"ws2_32.lib")
#include <cstdio>
#include <iostream>
#include <string>
#include <stdio.h>
#include <windows.h>
#include "Shlwapi.h"
#pragma comment(lib,"shlwapi.lib")
using namespace std;
#define INI_FILE "config.ini"
int camera_id = 0;
int show_width;
int show_height;
int preview_width;
int preview_height;
void initparame() {
//1 读取配置文件里的参数,文件名 config.ini
//“cannot convert parameter 1 from 'char []' to 'LPCWSTR'”)
// 解决办法:右击项目工程,依次选择Properties — Configuration Properties — General,
// 右边有个Character Set,将其由“Unicode Character Set”改为“Multi-Byte Character Set”即可。
// config.ini内容如下:
// [SETTINGS]
// camera_id = 0
// screen_width = 1920
// screen_height = 1080
// camera_width = 1920
// camera_height = 1080
CHAR str[245];
CHAR szAbsPathFile[MAX_PATH + 1] = { NULL };
GetModuleFileName(NULL, szAbsPathFile, MAX_PATH);
PathRemoveFileSpec(szAbsPathFile);
PathAppend(szAbsPathFile, INI_FILE);
int num;
num = GetPrivateProfileStringA("SETTINGS", "camera_id", NULL, str, MAX_PATH, szAbsPathFile);
if (num > 0) {
float value = strtod(str, NULL);
camera_id = value;
}
num = GetPrivateProfileStringA("SETTINGS", "screen_width", NULL, str, MAX_PATH, szAbsPathFile);
if (num > 0) {
double value = strtod(str, NULL);
show_width = value;
}
num = GetPrivateProfileStringA("SETTINGS", "screen_height", NULL, str, MAX_PATH, szAbsPathFile);
if (num > 0) {
int value = strtod(str, NULL);
show_height = value;
}
num = GetPrivateProfileStringA("SETTINGS", "camera_width", NULL, str, MAX_PATH, szAbsPathFile);
if (num > 0) {
int value = strtod(str, NULL);
preview_width = value;
}
num = GetPrivateProfileStringA("SETTINGS", "camera_height", NULL, str, MAX_PATH, szAbsPathFile);
if (num > 0) {
int value = strtod(str, NULL);
preview_height = value;
}
}
int main(int argc, char ** argv) {
initparame();
std::cout << "camera_id=" << camera_id << std::endl;
std::cout << "show_width:" << show_width << std::endl;
std::cout << "show_height:" << show_height << std::endl;
std::cout << "preview_width:" << preview_width << std::endl;
std::cout << "preview_height:" << preview_height << std::endl;
// 暂停控制台,不退出。
system("pause");
return 0;
}