Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 是否将数据从fstream复制到没有缓冲区的stringstream?_C++_Stl_Buffer_Fstream_Stringstream - Fatal编程技术网

C++ 是否将数据从fstream复制到没有缓冲区的stringstream?

C++ 是否将数据从fstream复制到没有缓冲区的stringstream?,c++,stl,buffer,fstream,stringstream,C++,Stl,Buffer,Fstream,Stringstream,我是否可以将数据从fstream(文件)传输到stringstream(内存中的流) 目前,我使用的是缓冲区,但这需要两倍的内存,因为需要将数据复制到缓冲区,然后将缓冲区复制到stringstream,直到删除缓冲区,数据才会在内存中复制 std::fstream fWrite(fName,std::ios::binary | std::ios::in | std::ios::out); fWrite.seekg(0,std::ios::end); //Seek to the end

我是否可以将数据从
fstream
(文件)传输到
stringstream
(内存中的流)

目前,我使用的是缓冲区,但这需要两倍的内存,因为需要将数据复制到缓冲区,然后将缓冲区复制到stringstream,直到删除缓冲区,数据才会在内存中复制

std::fstream fWrite(fName,std::ios::binary | std::ios::in | std::ios::out);  
    fWrite.seekg(0,std::ios::end); //Seek to the end  
    int fLen = fWrite.tellg(); //Get length of file  
    fWrite.seekg(0,std::ios::beg); //Seek back to beginning  
    char* fileBuffer = new char[fLen];  
    fWrite.read(fileBuffer,fLen);  
    Write(fileBuffer,fLen); //This writes the buffer to the stringstream  
    delete fileBuffer;`
有人知道我如何在不使用中间缓冲区的情况下将整个文件写入stringstream吗?

//需要包括和,当然还有和
// need to include <algorithm> and <iterator>, and of course <fstream> and <sstream>
ifstream fin("input.txt");
ostringstream sout;
copy(istreambuf_iterator<char>(fin),
     istreambuf_iterator<char>(),
     ostreambuf_iterator<char>(sout));
ifstream fin(“input.txt”); 奥斯汀河南区; 复制(istreambuf_迭代器(fin), istreambuf_迭代器(), ostreambuf_迭代器(sout));
ifstream f(fName);
细绳;
如果(f){

S+P>使用C++标准库的唯一方法是使用<代码> OSTFROWS而不是<代码> StrugSuth < /P> 您可以使用自己的char缓冲区构造一个
ostream
对象,然后它将获得缓冲区的所有权(因此不需要更多的复制)


但是,请注意,
stream
头已被弃用(尽管它仍然是C++03的一部分,而且很可能在大多数标准库实现中始终可用),如果您忘记null终止提供给ostream的数据,您将遇到很大的麻烦。这也适用于流运算符,例如:
ostreamobject在
ostream
的文档中,有。其中一个运算符使用
streambuf*
并读取所有streambuffer的内容

以下是一个使用示例(已编译和测试):

#包括
#包括
#包括
#包括
int main(int,char**)
尝试
{
//将保存文件内容。
std::stringstream内容;
//以尽可能短的时间打开文件。
{std::ifstream文件(“/path/to/file”,std::ios::binary);
//确保我们有东西要读。
如果(!file.is_open()){
抛出(std::异常(“无法打开文件”);
}
//“尽可能高效地”复制内容。
内容如果您正在使用,这很简单:

#include <Poco/StreamCopier.h>

ifstream ifs(filename);
string output;
Poco::StreamCopier::copyToString(ifs, output);
#包括
ifs流ifs(文件名);
字符串输出;
Poco::StreamCopier::copyToString(ifs,输出);

@Charles——尽管如此,我认为这是他的意图。他不想分配新的字符数组。他想直接从fstream对象读取到stringstream对象。@BenVoigt,你说得对-我误读了OP。我以为他们在问如何直接读取到stringstream。这似乎就是我要找的!@PigBen你说得对,我不想使用char数组。我有一个问题:copy方法是否考虑了stringstream的当前写入位置?如果我使用tellp(8),那么使用copy()它仍然有效,还是从stringstream的开始就开始编写?编辑:我自己尝试过,似乎效果很好。谢谢!尽管如此,我还是喜欢pinkfloydx的解决方案(由Andre制作成一个完整的示例)更好。
std::copy
必须一次移动一个元素,而
operator@Ben Voigt:我也是。我真的不知道你能这么容易做到。我的测试显示了一点区别。在一个50k文件上,两种方法各复制10次,他的平均每次复制1.45秒,我的平均1.62秒。结果是什么要点?您是否在尝试提高吞吐量?您可能需要放弃
fstream
在这种情况下,iostream速度很慢。您是否在尝试减少内存占用?将文件分块而不是一次全部读取可能会有所帮助。
#include <exception>
#include <iostream>
#include <fstream>
#include <sstream>

int main ( int, char ** )
try
{
        // Will hold file contents.
    std::stringstream contents;

        // Open the file for the shortest time possible.
    { std::ifstream file("/path/to/file", std::ios::binary);

            // Make sure we have something to read.
        if ( !file.is_open() ) {
            throw (std::exception("Could not open file."));
        }

            // Copy contents "as efficiently as possible".
        contents << file.rdbuf();
    }

        // Do something "useful" with the file contents.
    std::cout << contents.rdbuf();
}
catch ( const std::exception& error )
{
    std::cerr << error.what() << std::endl;
    return (EXIT_FAILURE);
}
#include <Poco/StreamCopier.h>

ifstream ifs(filename);
string output;
Poco::StreamCopier::copyToString(ifs, output);