line.find("*") == string::npos

string::find()函数:是一个字符或字符串查找函数,该函数有唯一的返回类型,即string::size_type,即一个无符号整形类型,可能是整数也可能是长整数。如果查找成功,返回按照查找规则找到的第一个字符或者子串的位置;如果查找失败,返回string::npos,即-1(当然打印出的结果不是-1,而是一个很大的数值,那是因为它是无符号的)
string::npos静态成员常量:是对类型为size_t的元素具有最大可能的值。当这个值在字符串成员函数中的长度或者子长度被使用时,该值表示“直到字符串结尾”。作为返回值他通常被用作表明没有匹配。

string::npos是这样定义的:static const size_type npos = -1;
因为string::size_type描述的是size,故需为无符号整数型类别。因为缺省配置为size_t作为size_type,于是-1被转换为无符号整数类型,npos也就成为了该类别的最大无符号值。不过实际值还是取决于size_type的实际定义类型,即无符号整型(unsigned int)的-1与无符号长整型(unsigned long)的-1是不同的

查找不含*的行


void readOTP(){
	double dbTargetWith=0;
	long SetAdressbits=0;
	long CodeType=0;
	long LongKeyPress=0;
	long Iidle=0;
	string line;
	ifstream ifs;
    ifs.open("D:\\OTP-INFO");
	if (ifs.is_open())
	{
	   while (getline(ifs, line))
	   {
		   cout << line << endl;
		   char key[20];
		   int value = 20;
		   if (line.find("*") == string::npos && line.length() != 0) //查找不含*的行
		   {
				   sscanf(line.c_str(), "%[a-z A-Z]:%d", key, &value);

				   if (strcmp(key, "TargetWidth") == 0)
				   {
					   dbTargetWith = value;
				   }
				   else if (strcmp(key, "AddressBits") == 0)
				   {
					   SetAdressbits = value;
				   }
				   else if (strcmp(key, "CodeType") == 0)
				   {
						   CodeType = value;
				   }
				   else if (strcmp(key, "LongKeyPress") == 0)
				   {
						   LongKeyPress = value;
				   }
				   else if (strcmp(key, "Iidle") == 0)
				   {
						   Iidle = value;
				   }
		   }
	   }
	   ifs.close();
	   printf("TargetWidth:%d, AddressBits:%d, CodeType:%d, LongKeyPress:%d, Iidle:%d\n",
			   dbTargetWith,
			   SetAdressbits,
			   CodeType,
			   LongKeyPress,
			   Iidle);
	}
}


 

struct SMS { int index; std::string state; std::string sender; std::string timestamp; std::string message; }; std::vector<SMS> smsList; bool serial_port::serial_port_write_read_at_bySMS(const std::string &at_command, const std::string &resp_prefix, std::vector<SMS>& smsList) { std::vector<std::string> read_lines; bool ret_value = false; pthread_mutex_lock(&read_write_mutex); if (!serial_port_write_at_cmd(at_command.c_str())) { goto END; } while (true) { int index = -1, n = -1; char state[32] = {}, phone_num[32] = {}, phone_time[64] = {}; read_lines = serial_port_read_multiple_lines(); for (size_t i = 0; i < read_lines.size(); i++) { //LOG_F(INFO, "read one line from serial: %s", read_line); if (read_lines[i].find("AT") != std::string::npos || read_lines[i].find("at") != std::string::npos) { //Do nothing } else if (read_lines[i].find("OK") != std::string::npos) { //LOG_F(INFO, "find final response OK"); ret_value = true; break; } else if (read_lines[i].find("ERROR") != std::string::npos || read_lines[i].find("+CME ERROR") != std::string::npos) { //LOG_F(INFO, "find final response ERROR"); ret_value = false; break; } else if (read_lines[i].find("+CMGL") != std::string::npos) { LOG_F(INFO, "response_data[%d]: %s", i, read_lines[i]); sscanf(read_lines[i], R"(+CMGL: %d,"%s","%s","%s")", &n, state, phone_num, phone_time); LOG_F(INFO, "n: %d, state: %s, phone_num: %s, phone_time: %s", n, state, phone_num, phone_time); SMS sms; index = i; sms.index = index; sms.state = state; sms.sender = phone_num; sms.timestamp = phone_time; if (read_lines[i].find('\n') != std::string::npos) { sms.message = read_lines[i+1]; } smsList.push_back(sms); // break; } else { LOG_F(INFO, "response_data[%d]->message: %s", i, read_lines[i]); // 继续往下读一行 } } if (index == -1) { break; // 未找到新的响应,退出循环 } } END: pthread_mutex_unlock(&read_write_mutex); return ret_value; } 这段代码有问题吗?如何改正
07-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值