简体   繁体   English

如何更改 C++ boost::iostreams 中源/接收器设备的读/写方法?

[英]How to change read/write method of source/sink device in C++ boost::iostreams?

I'm trying to get familiar with boost::iostream, so in a example program I'm writing, I want to read a text from a file and write it to file.我正在尝试熟悉 boost::iostream,所以在我正在编写的示例程序中,我想从文件中读取文本并将其写入文件。
I would use my inherited class from file_source/file_sink as device to read/write.我会使用从 file_source/file_sink 继承的 class 作为读取/写入的设备。 In the read method, my class needs to add each character with one and write method needs to subtract one from each character.在读取方法中,我的 class 需要每个字符加一个,写入方法需要每个字符减一个。
First of all, I want to make sure that the reading part of the program works properly so you can see the code as follow:首先,我要确保程序的阅读部分可以正常工作,以便您可以看到如下代码:

#include <boost/iostreams/stream.hpp>
#include <fstream>
#include "MyFileSource.h"
#include <iostream>
using namespace std;
using namespace boost::iostreams;
int main()
{
    MyFileSource<char> fileSource("source.txt");
    stream<MyFileSource<char>> myStream(fileSource);
    std::cout << myStream.rdbuf();
    fileSource.close();
}  

and my inherited source device code is as follow:我继承的源设备代码如下:

template <typename charType>
class MyFileSource : public boost::iostreams::file_source
{
public:
    MyFileSource(const std::string& path) : boost::iostreams::file_source(path)
    {
        file_path = path;
        if (!is_open())
            open(path);
        seek(0, ios::end);
        sizeOfFile = seek(0, ios::cur);
        seek(0, ios::beg);
        buffer = new charType[sizeOfFile];
    }
    std::streamsize read(charType*, std::streamsize)
    {
        std::streamsize readCount = boost::iostreams::file_source::read(buffer, sizeOfFile);
        if (readCount > 0)
        {
            std::string result(buffer, readCount);
            for (char& c : result)
                c++;
            finalResult = result;
        }
        return readCount;
    }

private:
    string file_path;
    int sizeOfFile;
    charType* buffer;
    std::string finalResult;
};

unfortunately, the result of std::cout << myStream.rdbuf() with above read method is nothing while if I delete the read method of MyFileSource class to use the read method of parent, the result will be correct.不幸的是,使用上述读取方法的std::cout << myStream.rdbuf()的结果是什么,而如果我删除 MyFileSource class 的读取方法以使用父级的读取方法,结果将是正确的。
Any help will be appreciated.任何帮助将不胜感激。

I solved the problem...我解决了问题...

template <typename charType>
class MyFileSource : public boost::iostreams::file_source
{
public:
    MyFileSource(const std::string& path) : boost::iostreams::file_source(path)
    {
        if (!is_open())
            open(path);
    }
    std::streamsize read(charType* s, std::streamsize n)
    {
        std::streamsize readCount = boost::iostreams::file_source::read(s, n);
        if (readCount > 0)
        {
            std::string result(s, readCount);
            for (auto& c : result)
                c++;
            std::copy(result.begin(), result.end(), s);
        }
        return readCount;
    }
};

As you can see, the problem was solved by deleting the additional buffer variable and replacing it with s.如您所见,通过删除额外的缓冲区变量并将其替换为 s 解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM