参数查找出错

时间:2014-10-20 14:44:15

标签: c++ string

我想在字符串中找到单词的位置。这是我的代码,但我收到一条错误消息:

error: request for member find in buffer1, which is of non-class type std::string [50]
  {aka std::basic_string<char> [50]}.

我该如何解决?

n = write(SocketFD,"GET /index.html HTTP/1.1\r\nHost: www-net.cs.umass.edu\r\nUser-Agent: Firefox/3.6.10\r\nAccept: text/html,application/xhtml+xml\r\nAccept-Language: en-us,en;q=0.5\r\nAccept-Encoding: gzip,deflate\r\nAccept-Charset: ISO-8859-1,utf-8;q=0.7\r\nKeep-Alive: 115\r\nConnection: keep-alive\r\n\r\n",47);

string buffer1[50];
bzero(buffer1,50);

n=read(SocketFD,buffer1,50);

size_t found = buffer1.find("Lenght");

if(found!=string::npos)
cout<<"posicion"<<found;

2 个答案:

答案 0 :(得分:0)

buffer1是std::string的数组。因此,当您致电buffer1.find("Lenght");时,您实际上是在尝试调用find的方法string[]。没有这样的方法,所以你得到你看到的错误。我的猜测是你打算构造一个大小为50的字符串:string buffer1(50, '\0');

请注意,这很难解决代码中的所有问题。我的猜测是,读取需要char*,您将无法将其与std::string一起使用。

答案 1 :(得分:-1)

这可以帮助您入门:

#include <string>
#include <iostream>
using namespace std;
int main() {
  char buf[50];
  int n=read(SocketFD,buffer1,50);
  //might need to add a null terminator
  //especially if it overflows
  string s(buffer);
  size_t found = s.find("brown");
  cout << "position=" << found << endl;
}