您当前的位置: 首页 >  Java

qianbo_insist

暂无认证

  • 0浏览

    0关注

    399博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

自由读写配置文件的艺术[java c++ node](二)

qianbo_insist 发布时间:2021-03-16 13:37:38 ,浏览量:0

为何要自己写配置文件

1 解决引入很多库的问题 2 解决流程问题而非语言问题 3 学会简化问题

读写配置文件的知识

一下是一个txt配置文件,注意文件格式,如果我们在utf8 文件下读写,分为两种,一种是utf8 带BOM,一种不带,我们都要考虑

#id(int):address(varchar):width(int):height(int) #418511899@qq.com author:钱波 qianbo 2021-02-28 //camera //tcp or udp 1 rtsp://127.0.0.1/qianbo2.264 1280 720 live/1001 #2 rtsp://127.0.0.1/out.264 1280 720 live/1001 #2 rtsp://127.0.0.1/1.264 640 360 live/1002

ok,# 和//都是注释,如果加上这个符号,就必须要把这一行忽略 读出一行,标准的c++非常方便,一句话就出来,用java使用readLine函数 std::getline(infile, temp); 注意在linux下和windows下是不一样的,linux下读出来的会有一个’\r’,所以要去除

static void trim(std::string &s)
{
	if (s.empty())
		return;
	s.erase(s.find_last_not_of('\r') + 1); //the linux system getline must remove the '\r'
	s.erase(s.find_last_not_of(' ') + 1);
	//s.erase(s.find_last_not_of('\t') + 1);
	s.erase(0, s.find_first_not_of(' '));
	//s.erase(0, s.find_first_not_of('\t'));
}

这是语言无关的,如果是是用c,c++,问题都是一样,需要了解系统和知识。接下来这个问题是也会经常碰到的,就是带BOM头部的utf8文件,要做产品,就必须把各种情况测试到,并且解决掉这个问题。

if (temp.length() > 3 /*0xEF 0xBB 0xBF*/)
{
   uint8_t *pos = (uint8_t*)temp.c_str();
   if (*pos == 0xEF)
   {
   	temp = temp.substr(3);//去除前三字节
   	printf("%02x %02x %02x \n", *(pos), *(pos + 1), *(pos + 2));
   	cout             
关注
打赏
1663161521
查看更多评论
0.0390s