我試圖從數組中搜索一個來自服務器java的值。服務器在點擊一個按鈕後發送數據,並將該值接收到estratto
這是一個char數組。 我需要找到estratto
到字符串數組numeri
或字符串變量number
。如何使用C++將值檢索到數組中?
我該怎麼做?
這裏是我的代碼: 「已找到」
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;
}
}
}
的COUT < < endl; < < endl;如果我嘗試更改此代碼的某些部分,則不起作用。有人可以幫助我找到解決方案?
你是什麼意思的「不工作」?你沒有得到任何輸出?錯誤的輸出?構建錯誤?請詳細說明並提供詳細信息。還包括所涉及的所有變量的類型,以及可能的初始化方式。 –
哦,順便說一下,你知道C風格的字符串(我想你從服務器端收到的)應該由'\ 0'字符來終止嗎?您收到的數據是以這種方式終止的嗎? –
@JoachimPileborg我看不到輸出'cout <<「Trovato!」 << endl;' –