C++ iostream, stringstream 终止

标签 c++ stream

我正在对简单的 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/

相关文章:

c++ - 在 WxWidgets 中使用位图

c++ - Visual C++ 2010 - CL.exe 退出代码 5

c++ - Apache Kafka 可以与 C++ 一起使用吗?

c - 在实际 EOF 之前的 cmd 转发输出中的文件结尾

C++ 在枚举中使用 ASCII 而不是字母

c++ - 缩短 If 语句并减少冗余

c++ - 编译器如何知道何时不重新编译?

c# - 等待流的人口

c# - 读取可能包含非 ASCII 字符的流

java - 无法读取本地资源