下面的代码给我RWBoundsErr错误
terminate called after throwing an instance of 'RWBoundsErr'
bool filterData()
{
while(!inputFile_.eof())
{
currentLine_.readLine(inputFile_);
//outputFile_<<currentLine_(0,1)<<currentLine_(2,1)<<currentLine_(4,13)<<currentLine_(0,1)<<currentLine_(2,1)<<currentLine_(4,13)<<endl;
outputFile_<<currentLine_[0]<<currentLine_[2]<<currentLine_(4,13)<<currentLine_[0]<<currentLine_[2]<<currentLine_(4,13)<<endl;
}
return (TRUE);
}
但如果我改用注释掉的行,一切都会按预期工作。
currentLine_数据类型为RWCString
outputFile_ 和 inputFile_ 具有输入/输出 txt 文件的路径
文本文件的示例内容
ABCD1234567890123 2017/10/16 13:40:28
WXYZ9876543210987 2017/10/16 13:40:28
请注意每行末尾有一个尾随空格
尽管我已经确定了这个问题的解决方案,但我还是想了解为什么这段代码会失败。
最佳答案
查看与 operator<<()
相关的文档RWCString
的运算符:
RWvostream&
operator<<(RWvostream&, const RWCString& str);
RWFile&
operator<<(RWFile&, const RWCString& str);
Saves string str to a virtual stream or RWFile, respectively.
和RWFile
:
RWFile&
operator<<(RWFile&, const RWCString& str);
Saves string str to a virtual stream or RWFile, respectively.
...
RWFile&
operator<<(RWFile&, const RWInteger& x);
Saves the RWInteger x to a virtual stream or RWFile, respectively.
没有<<
需要 char
的运算符.
currentLine_(0,1)
是可以与 <<
一起使用的子字符串运算符
RWCSubString
operator()(size_t start, size_t len);
RWCConstSubString
operator()(size_t start, size_t len) const;
另一方面,currentLine_[0]
是 char
char&
operator[](size_t i);
char
operator[](size_t i) const;
Returns the ith byte.
在您的情况下,尝试使用 char
与 <<
运算符导致 RWBoundsErr
错误。
关于c++ - 在 C++ 中使用 RWCString 读取文本文件时获取 RWBoundsErr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48719896/