c++ main函数基本操作(2)-- 自定义配置文件读取config.ini

/*
 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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值