如何使用 c++ 将值检索到数组中
How to retrieve a value into an array using c++?
我试图在数组中搜索来自服务器 java 的值。服务器在单击按钮后发送数据,并将此值接收到estratto
这是一个字符数组。我需要在字符串数组numeri
或字符串变量中找到estratto
number
.
我该怎么做?
这是我的代码:
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 <<"Trovato!"<<endl;如果我尝试更改此代码的某些部分,则不起作用。有人可以帮助我找到解决方案吗?
您是否遇到了无符号与有符号字符集问题。您可能需要使用 64 位编码标准对字符串进行编码和解码。
我会像这样修改代码:
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;
}
}
}
您的第一个检查"pos>0"是错误的,因为如果从索引 0 开始的数字字符串中包含 estratto 字符串,您的检查将找不到它。相反,strcmp() 函数的第二次检查只会在 estratto 字符串完全匹配时捕获 estratto 字符串,但如果它是子字符串,则不会。
相关文章:
- 初始化类中的指针数组,并在另一个类中检索它
- 从C++(非托管代码)检索数组到 C 尖锐形式(托管)
- 我正在编写一个代码来将 int 数组存储在文件中,然后用 c++ 检索它,但是检索第一项是假值,我该如何解决这个问题?
- Getter 函数,用于在 2d 数组是类的数据成员时检索 2d 数组元素
- ifstream不使用C 中的数组检索数据
- 无法从数组二次表达式中检索数据值
- 如何从DLL检索数组变量?(视觉C )
- 当我将字符串数组从 VBA 传递到 c++ 并将它们用作映射中的键时,为什么我无法从映射中检索数据(使用键)
- 如何在三维数组中使用std::map来存储/检索具有特定值的键
- 检索字符数组数据时出现问题
- 如何在 C++ 中传递和检索 char 数组
- 以数组的形式存储组合框项,并在 WPF 中检索 SelectedId
- 正在从数组中检索变量
- 如何检索智能指针数组的大小?(例如std::unique_ptr<int[]>)
- 无法从数组中检索字符串
- 如何使用 c++ 将值检索到数组中
- 减少从外部数据文件中检索字符数组的重复
- 从单独函数中的结构中检索数组成员
- 从 C++ 中的字节数组中检索整数的最佳方法是什么
- 如何将数组从c++传递给python函数,并将python返回的数组检索到c++