C++ 在没有缓冲区的情况下将数据从 fstream 复制到 stringstream?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4064601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:27:34  来源:igfitidea点击:

Copy data from fstream to stringstream with no buffer?

c++stlbufferfstreamstringstream

提问by Brad

Is there anyway I can transfer data from an fstream(a file) to a stringstream(a stream in the memory)?

无论如何我可以将数据从fstream(文件)传输到stringstream(内存中的流)?

Currently, I'm using a buffer, but this requires double the memory, because you need to copy the data to a buffer, then copy the buffer to the stringstream, and until you delete the buffer, the data is duplicated in the memory.

目前,我正在使用缓冲区,但这需要双倍内存,因为您需要将数据复制到缓冲区,然后将缓冲区复制到字符串流,直到删除缓冲区,数据才会在内存中重复。

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;`

Does anyone know how I can write a whole file to a stringstream without using an inbetween buffer?

有谁知道如何在不使用中间缓冲区的情况下将整个文件写入字符串流?

回答by pinkfloydx33

 ifstream f(fName);
 stringstream s;
 if (f) {
     s << f.rdbuf();    
     f.close();
 }

回答by Benjamin Lindley

// 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));

回答by André Caron

In the documentation for ostream, there are several overloads for operator<<. One of them takes a streambuf*and reads all of the streambuffer's contents.

在 for 的文档中ostream,有几个 for 的重载operator<<。其中一个使用 astreambuf*并读取流缓冲区的所有内容。

Here is a sample use (compiled and tested):

这是一个示例使用(编译和测试):

#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);
}

回答by smerlin

The only way using the C++ standard library is to use a ostrstreaminstead of stringstream.

使用C ++标准库的唯一方法是使用ostrstream代替stringstream

You can construct a ostrstreamobject with your own char buffer, and it will take ownership of the buffer then (so no more copying is needed).

您可以ostrstream使用自己的字符缓冲区构造一个对象,然后它将获得缓冲区的所有权(因此不再需要复制)。

Note however, that the strstreamheader is deprecated (though its still part of C++03, and most likely, it will always be available on most standard library implementations), and you will get into big troubles if you forget to null-terminate the data supplied to the ostrstream.This also applies to the stream operators, e.g: ostrstreamobject << some_data << std::ends;(std::endsnullterminates the data).

但是请注意,strstream头文件已被弃用(尽管它仍然是 C++03 的一部分,并且很可能在大多数标准库实现中始终可用),如果您忘记空终止数据提供给ostrstream.This也适用于流运营商,例如:ostrstreamobject << some_data << std::ends;std::endsnullterminates的数据)。