C++ 使用大小差异复制字符串的一部分
我试图遍历一个字符串,并根据初始键值和标识信息块结尾的键值复制信息块。然而,当我试图减去我的初始值和最终值以找到我要查找的块的长度时,我收到了一个看似任意的值 因此,开始和结束标记可通过以下方式找到:C++ 使用大小差异复制字符串的一部分,c++,size-t,C++,Size T,我试图遍历一个字符串,并根据初始键值和标识信息块结尾的键值复制信息块。然而,当我试图减去我的初始值和最终值以找到我要查找的块的长度时,我收到了一个看似任意的值 因此,开始和结束标记可通过以下方式找到: currentstringlocation = mystring.find("value_im_looking_to_start_at, 0); endlocation = mystring.find("value_im_looking_to_stop_at", currentstringloca
currentstringlocation = mystring.find("value_im_looking_to_start_at, 0);
endlocation = mystring.find("value_im_looking_to_stop_at", currentstringlocation);
然后我试着做一些类似的事情:
mystring.copy(newstring,(endlocation-currentlocation), currentlocation);
topoinfo.copy(address, stringlocation2 - (stringlocaion + 11),
stringlocation + 11);
然而,这并没有给我想要的结果。下面是我的代码摘录及其产生的输出
stringlocation2=topoinfo.find("\n",stringlocation+11);
topoinfo.copy(address,(stringlocation2-stringlocation+11),stringlocation+11);
cout << (stringlocation2-stringlocation+11) << "\n";
cout << stringlocation2 << "\t" << stringlocation+11 << "\n";
stringlocation2=topoinfo.find(“\n”,stringlocation+11);
拓扑信息副本(地址,(stringlocation2 stringlocation+11),stringlocation+11);
cout应该是
topoinfo.copy(address,stringlocation2-(stringlocation+11),stringlocation+11);
cout << stringlocation2-(stringlocation+11) << "\n";
topoinfo.copy(地址,stringlocation2-(stringlocation+11),stringlocation+11);
cout应该是
topoinfo.copy(address,stringlocation2-(stringlocation+11),stringlocation+11);
cout << stringlocation2-(stringlocation+11) << "\n";
topoinfo.copy(地址,stringlocation2-(stringlocation+11),stringlocation+11);
cout如果计算长度错误,请尝试以下方法:
mystring.copy(newstring,(endlocation-currentlocation), currentlocation);
topoinfo.copy(address, stringlocation2 - (stringlocaion + 11),
stringlocation + 11);
在此之后,地址
将包含复制的字符串。但是请记住:如果地址
是字符数组或字符指针,那么您应该自己添加终止的'\0'
字符
获取子字符串的更好解决方案是实际使用函数:
std::string address = topoinfo.substr(stringlocation + 11,
stringlocation2 - (stringlocaion + 11));
计算长度时出错,请尝试以下操作:
mystring.copy(newstring,(endlocation-currentlocation), currentlocation);
topoinfo.copy(address, stringlocation2 - (stringlocaion + 11),
stringlocation + 11);
在此之后,地址
将包含复制的字符串。但是请记住:如果地址
是字符数组或字符指针,那么您应该自己添加终止的'\0'
字符
获取子字符串的更好解决方案是实际使用函数:
std::string address = topoinfo.substr(stringlocation + 11,
stringlocation2 - (stringlocaion + 11));
你确定你要做的是字符串操作而不是解析器吗?你确定你要做的是字符串操作而不是解析器吗?当然。当然,谢谢你指出这一点并提出方法建议。感谢您指出这一点并提出方法建议。