C++:从json文件中读取参数/创建文件夹/位拼接

目录

1、从json文件中读取参数

2、创建文件夹

3、位运算拼接


1、从json文件中读取参数

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,和xml类似。

Jsoncpp是个跨平台的开源库,下载地址:http://sourceforge.net/projects/jsoncpp/。

配置方法:

首先解压文件夹,将include和src两个文件夹放到项目目录下

然后,只需要在VS中添加配置一个包含目录就行

还需要把src/lib_json文件夹下的三个cpp文件添加到项目中

 使用示例

#include "include/json/json.h"

void readFileJson(const char* jsonpath)
{
    Json::Reader reader;
    Json::Value root;

    //从文件中读取,保证当前文件有demo.json文件  
    std::ifstream in(jsonpath, std::ios::binary);
    if (!in.is_open())
    {
        cout << "Error opening file\n";
        return;
    }
    if (reader.parse(in, root))
    {
        //将json中的参数读取出来,转为合适的类型即可
        SCREEN_WIDTH = root["screen_width"].asInt();//转为int
        SCREEN_HEIGHT = root["screen_height"].asInt();

        chess_pic = root["chess_pic"].asString();//转为string
        base_pic[0] = root["base_pic"]["base_pic_0"].asString();//二级参数
    }
    else
    {
        cout << "parse error\n" << endl;
        return;
    }

    in.close();
}

2、创建文件夹


    string folderPath = "tmp";  
  
    if (0 != _access(要创建的路径path, 0))
    {
        // if this folder not exist, create a new one.
        _mkdir(path.c_str());   // 返回 0 表示创建成功,-1 表示失败
    }
    else
    {
        cout << "文件夹已经存在.." << endl;
    }

Linux下,该函数为access,位于头文件<unistd.h>中,而在标准C++中,该函数为_access,位于头文件<io.h>中

需要注意的一点是,这样只能创建一级目录,即路径倒数第二级必须是已经存在的目录,不能创建多级


2022年10月17日add

#include <iostream>
#include <direct.h>
#include <io.h>
using namespace std;

int main()
{
    /// <summary>
    /// 在当前目录下创建一个文件夹tmp
    /// 新建文件,文件名以时间命名
    /// </summary>
    /// <returns></returns>
    string folderPath = "tmp";
    if (0 != _access(folderPath.c_str(), 0))
    {
        // if this folder not exist, create a new one.
        _mkdir(folderPath.c_str());   // 返回 0 表示创建成功,-1 表示失败
    }
    else 
    {
        cout << "文件夹已经存在" << endl;
    }

    char tmp_file[256];
    time_t currTime;
    struct tm* mt= (tm*)malloc(sizeof(tm));//这里要对结构体指针初始化
    currTime = time(NULL);
    //这里使用localtime_s,用法与localtime有所不同
    errno_t err_localtime = localtime_s(mt, &currTime);

    struct tm mt2;
    err_localtime = localtime_s(&mt2, &currTime);

    sprintf_s(tmp_file, "%s\\%d%02d%02d%02d%02d%02d.xml", folderPath, mt->tm_year + 1900, mt->tm_mon + 1, mt->tm_mday, mt->tm_hour, mt->tm_min, mt->tm_sec);
    free(mt);
    FILE* fp;
    //fopen_s与fopen的用法也有区别
    errno_t err = fopen_s(&fp, tmp_file, "w+");//r(read),w(write),a(append)
    fprintf(fp, "%s%d%02d%02d%02d%02d%02d.xml", folderPath, mt->tm_year + 1900, mt->tm_mon + 1, mt->tm_mday, mt->tm_hour, mt->tm_min, mt->tm_sec);
    fprintf(fp, "%s\\%d%02d%02d%02d%02d%02d.xml", folderPath, mt2.tm_year + 1900, mt2.tm_mon + 1, mt2.tm_mday, mt2.tm_hour, mt2.tm_min, mt2.tm_sec);
    fclose(fp);
    return 0;
}

3、位运算拼接

    //两个1字节的数拼成两字节数
    unsigned char x_8 = 11;//拼在左边
    unsigned char y_8 = 22;//拼在右边
    //两字节变量
    unsigned short xy_16 = 0;
    //拼在高位的左移8位
    xy_16 = x_8 << 8;
    //一个或运算
    xy_16 |= y_8;

脑子不行,需要经常记些东西,不然就忘了。。。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值