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);
}
}