C++iostream读取,分隔符为MULTIPLE字节
C++ iostream read with delimiter of MULTIPLE bytes
我们知道要从输入流中读取,我们可以使用以下标准C++函数
istream& getline (char* s, streamsize n, char delim );
但是,我们只能提供一个字节/字符的分隔符。
如果我希望使用多个字节的分隔符,该怎么办?有什么巧妙的东西我可以利用,推动吗?
最佳,
Lyn
我认为您可以使用cin.get()
而不是cin.getline()
。每次读取一个字符,并测试是否出现分隔符。
int main(void){
string str;
int length_of_delimiter = 3;
const char *delimiter = "ABC";
char temp = '0';
bool over = false;
cout<<"Enter the stream"<<endl;
temp = cin.get();
int i = 0;
while(over == false){
for(i = 0; temp == delimiter[i] && i < length_of_delimiter; i++){
str += temp;
temp = cin.get();
}
if(i == length_of_delimiter){
//chop off the delimiter
str.erase(str.end() - length_of_delimiter, str.end());
over = true;
}
else {
str += temp;
temp = cin.get();
}
}
cout<<"The stream we wanted is: "<<str<<endl;
return 0;
}
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 在UNIX系统中使用DIR查找文件的字节大小
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- std::当在256字节边界上写入整数时,流的奇怪行为
- 当比特(而不是字节)的顺序至关重要时的持久性
- 从文件中读取多个字节,并将它们存储在C++中进行比较
- 如何在文件中查找字节序列
- luaL_dofile在已知良好的字节码上失败,可以使用未编译的版本
- 字节到位运算符重载C++
- 在java中读取c++字节的位字段
- 使用 std::vector::reverse_iterator 将 int 序列化为字节向量?
- 字节真的是最小可寻址单元吗
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- C++ 链接到单独的.cpp文件说"multiple definitions"
- 读取文件中所有可能的十六进制 16 字节序列并打印每个序列
- 如何使用 OpenCV 解码在两个 UWP 应用之间发送的图像字节?
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- asn1c 不会从 asn.1 模块中提取八位字节字符串的默认值
- C++iostream读取,分隔符为MULTIPLE字节