我正在研究boost::asio::streambuf
并发现我可以使用它来发送/获取结构,但是当我发送结构时,我无法得到它,就像我发送它一样。文档说应该使用commit()
和consume()
,但我应该在哪里使用它们?
struct person
{
int m_id;
std::string m_message;
};
std::istream& operator>>(std::istream& in, struct person& p)
{
return in >> p.m_id >> p.m_message;
}
std::ostream& operator<<(std::ostream& out, struct person& p)
{
return out << p.m_id << " " << p.m_message;
}
int main()
{
boost::asio::streambuf buf;
std::ostream out(&buf);
person p;
p.m_id = 1;
p.m_message = "Hello World!";
out << p;
std::istream in(&buf);
person p1;
in >> p1;
cout << "ID: " << p1.m_id << endl;
cout << "Message: " << p1.m_message << endl;
return 0;
}
问题在于字符串所以当我只键入“hello”(没有世界)时,它工作正常,但如果我添加“世界!”如上所示它只是没有看到添加的“世界!”,为什么?
答案 0 :(得分:1)
有很多问题。
首先,尽可能使参数const&
:
std::ostream &operator<<(std::ostream &out, person const &p) {
其次,确保流冲洗到缓冲区。我认为限制ostream
或istream
实例的生命周期
第三,选择一个强大的格式。当你有m_id = 1
和m_message = "123"
时,你的样本已经有了更大的问题(你能看到吗?)。
在文本格式中,您需要固定长度字段或分隔协议。让我们解决它:
std::ostream &operator<<(std::ostream &out, person const &p) {
return out << p.m_id << ";" << p.m_message.length() << ";" << p.m_message;
}
现在阅读它时,你会看到你需要更加精确:
std::istream &operator>>(std::istream &in, person &p) {
char separator;
size_t length;
bool ok = in >> p.m_id
&& in >> separator && separator == ';'
&& in >> length
&& in >> separator && separator == ';'
;
if (ok) {
p.m_message.resize(length);
in.read(&p.m_message[0], length);
p.m_message.resize(in.gcount());
}
// ensure the expected number of bytes were read
ok = ok && (p.m_message.length() == length);
if (!ok)
in.setstate(std::ios::failbit);
return in;
}
让人惊讶。真?对真的。至少!
执行错误处理
<强> Live On Coliru 强>
#include <boost/asio.hpp>
#include <iostream>
struct person {
int m_id;
std::string m_message;
};
std::ostream &operator<<(std::ostream &out, person const &p) {
return out << p.m_id << ";" << p.m_message.length() << ";" << p.m_message;
}
std::istream &operator>>(std::istream &in, person &p) {
char separator;
size_t length;
bool ok = in >> p.m_id
&& in >> separator && separator == ';'
&& in >> length
&& in >> separator && separator == ';'
;
if (ok) {
p.m_message.resize(length);
in.read(&p.m_message[0], length);
p.m_message.resize(in.gcount());
}
// ensure the expected number of bytes were read
ok = ok && (p.m_message.length() == length);
if (!ok)
in.setstate(std::ios::failbit);
return in;
}
int main() {
boost::asio::streambuf buf;
std::ostream(&buf) << person{ 1, "Hello World!" };
person received;
if (std::istream(&buf) >> received) {
std::cout << "ID: " << received.m_id << std::endl;
std::cout << "Message: " << received.m_message << std::endl;
} else {
std::cout << "Couldn't receive person\n";
}
}
打印
ID: 1
Message: Hello World!
C ++ 14添加了std::quoted
:
#include <iomanip>
std::ostream &operator<<(std::ostream &out, person const &p) { return out << p.m_id << std::quoted(p.m_message); }
std::istream &operator>>(std::istream &in, person &p) { return in >> p.m_id >> std::quoted(p.m_message); }
在这种情况下,也可以完成这项工作: Live On Coliru
答案 1 :(得分:0)
来自http://en.cppreference.com/w/cpp/string/basic_string/operator_ltltgtgt(强调和省略号):
2)...从is中读取字符并将它们附加到str ...直到一个 以下条件成为现实:
- ...
- ...
- std :: isspace(c,is.getloc())为真,用于下一个字符c in(此空白字符保留在输入流中)。 ...
基本意味着这意味着如果您使用istream
从operator >>
中提取字符串,它会在空格处停止。