因此,我需要创建一个包含4行文本(EN_us)的字符缓冲区,如
first line
line with some number like 5
line 3
empty line从用户那里获取这样的字符缓冲区的正确方式是什么,以及如何获取它的长度?
发布于 2010-11-17 06:45:31
与获得这样的缓冲区相比,使用getline将四行代码从标准输入中读取到单独的string中可能更容易(如果您愿意,可以使用循环):
那么总数据长度就是各个string长度的总和。或者,使用此方法从用户检索数据,然后将它们连接到一个四行的stringstream中。
组合代码示例:
#include <string>
#include <sstream>
#include <iostream>
std::string s[4];
size_t length(0);
std::ostringstream output;
for (size_t index = 0; index < 4; ++index)
{
getline(std::cin, s[index]);
length += s[index].length();
output << s[index] << std::endl;
}
output.flush();
streamoff streamLength = output.tellp();https://stackoverflow.com/questions/4199629
复制相似问题