简体   繁体   English

c ++ string.find()返回字符串的长度

[英]c++ string.find() returnes length of the string

Hi I am a little confused. 嗨,我有点困惑。 My program uses TCP to transfer messages over network. 我的程序使用TCP通过网络传输消息。 Which is in my opinion irrelevant to my question. 我认为这与我的问题无关。

std::stringstream tmp(buf);

            if (tmp.str().find("\r\n") == std::string::npos ) {
                std::cout << " doesnt have ending char" << std::endl;
            }else{
                std::cout << " position of ending char " << tmp.str().find("\r\n") << std::endl;
            }

when a message is read from client, it is pushed to stringstream. 从客户端读取消息时,消息被推送到stringstream。 Then I am trying to find escape character, unfortunately string.find("\\r\\n") always return length of the string, even though "\\r\\n" is not contained in the buf. 然后我试图找到转义字符,不幸的是string.find(“ \\ r \\ n”)总是返回字符串的长度,即使buf中不包含“ \\ r \\ n”。

I am using 我在用

telnet 远程登录

to test it, is it possible that this behavior is caused by the telnet? 要测试它,是否可能是由telnet引起的?

This is output of my terminal: 这是我终端的输出:

Connected to localhost.
Escape character is '^]'.
200 LOGIN
asdadsgdgsd

and this is output from the program: 这是从程序输出的:

3001 PORT NUM 
sent message: asdadsgdgsd  END  
position of ending char 11

The string entered in the console then sent and received is 在控制台中输入然后发送和接收的字符串是

"asdadsgdgsd\r\n"
 012345678901_2_

So the value 11 is correct 所以值11是正确的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM