gpt4 book ai didi

C++ iostream, stringstream 终止

转载 作者:行者123 更新时间:2023-11-30 05:43:22 25 4
gpt4 key购买 nike

我正在对简单的 TCP 服务器进行编程,但我遇到了流终止的问题。

我正在接收一些数据,定义数据的结尾是组合 \r\n ,但是流终止于该数据中的第一个 \0 .

bool read(char &chr_glob, stringstream &ss ){
char chr;
char buffer[BUFFER_SIZE];
while (!ss.get(chr)){
ss.clear();
recv(c, buffer, BUFFER_SIZE, 0);
ss<<buffer;
}
chr_glob = chr
if ( chr == '\r' ){
char tmp = ss.peek();
if ( tmp == '\n'){
ss.get(chr_glob);
return false;
}
}
return true;
}

我尝试使用 iostream ,但我遇到了编译问题,不知道如何解决。

In file included from /usr/include/c++/4.8/iostream:40:0,
from robot.cpp:7:
/usr/include/c++/4.8/istream: In function ‘int main(int, char**)’:
/usr/include/c++/4.8/istream:830:7: error: ‘std::basic_iostream<_CharT, _Traits>::basic_iostream() [with _CharT = char; _Traits = std::char_traits<char>]’ is protected
basic_iostream()
^
robot.cpp:148:14: error: within this context
iostream ss;
^

最佳答案

the stream is terminating on the first \0 in this data.

ss<<buffer;

期望以 NULL 结尾的 C 字符串(NULL 用于确定数据长度)。对二进制数据使用write:

size = recv(c, buffer, BUFFER_SIZE, 0);
ss.write(buffer, size);

I was trying to use iostream , but I've get compilation problem, and don't know how to fix it.

iostream 是一个基类。它不能直接实例化。像您一样使用 stringstream

关于C++ iostream, stringstream 终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30248471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com