c++ - 在 C++ 中使用 RWCString 读取文本文件时获取 RWBoundsErr

标签 c++ file-io file-handling fileoutputstream rogue-wave

下面的代码给我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/

相关文章:

C++ 指向结构的指针失去值(value)

c++ - SSE 版本的不同结果

javascript - 是否有可能在发布之前重命名 html 文件输入的文件

c - 如何通过 block 设备打开文件

erlang - 更改 Erlang 文件句柄限制?

c++ - 在将第 N 个元素插入元素 K-1 的 vector 后,对象指针 vector 的元素 K 变为空

c++ - 图像每一行在 x 方向上的一维卷积

java - getResourceAsStream 返回 null 但 File.canRead() 返回 true

c - 将文件指针移至末尾

arrays - strtok 大小 1 的读取无效