[英]How to retrieve a value into an array using c++?
I tried to search into an array a value coming from a server java.The server send data after a click on a button and this value is received into estratto
which is a char array. 我试图将来自服务器java的值搜索到数组中。服务器在单击按钮后发送数据,并且此值被接收到
estratto
,这是一个char数组。 I need to find estratto
into the string array numeri
or into the string variable number
. 我需要找到
estratto
到字符串数组numeri
或成字符串变量number
。
How can I do this? 我怎样才能做到这一点?
Here's my code: 这是我的代码:
char estratto[2048];
int pos=-1;
char message[2048] = "";
//-- some code
while(recv(sock, buff, sizeof(buff),0) > 0){
strcat(message,buff); // received message form client
}
//-- some code
//-- divided message into a string array
istringstream iss(message);
string token;
string numeri[15];
int i=0,j=0,e=0;
while (std::getline(iss, token, ','))
{
numeri[i]= token.c_str();
i++;
}
//-- the part that has problems
string number(message);
while(recv(sock, estratto, sizeof(estratto),0)>0){
for(i=0; i<15; i++){
pos=number.find(estratto);
if(pos>0)
cout<<"TROVATO!"<<endl;
if(strcmp(numeri[i].c_str(),estratto)==0){
trovati_cartella[i]=1;
cout<<"TROVATO!"<<endl;
}
}
}
The cout <<"Trovato!" 喊声<<“ Trovato!” << endl;
<< endl; doesn't work if I try to change some parts of this code.
如果我尝试更改此代码的某些部分,将无法正常工作。 Someone can help me to find the solution?
有人可以帮助我找到解决方案吗?
Are you running into a unsigned vs signed character set issue. 您是否遇到未签名与已签名字符集的问题。 You may need to encode and decode the string with the 64 bit encoding standard.
您可能需要使用64位编码标准对字符串进行编码和解码。
I would modify the code like this: 我会这样修改代码:
while(recv(sock, estratto, sizeof(estratto),0)>0){
for(i=0; i<15; i++){
pos=number.find(estratto);
if(pos != std::string::npos)
cout<<"TROVATO!"<<endl;
if(strstr(numeri[i].c_str(),estratto) != NULL){
trovati_cartella[i]=1;
cout<<"TROVATO!"<<endl;
}
}
}
Your first check "pos>0" is wrong because if estratto string is contained into number string starting from index 0, your check will not find it. 您的第一个检查“ pos> 0”是错误的,因为如果estratto字符串包含在从索引0开始的数字字符串中,则您的检查将找不到它。 Your second check by strcmp() function instead would catch estratto string only if it exactly matches numeri[i] string, but not if it is a substring.
相反,通过strcmp()函数进行的第二次检查将仅在estratto字符串与numeri [i]字符串完全匹配时才捕获estratto字符串,但如果它是子字符串则不会。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.