c++ - 将文件的一部分读入 std::vector<char> 的有效方法?

标签 c++ c++11

引用这个问题:Efficient way of reading a file into an std::vector<char> ? 我需要一个执行以下操作的函数:

void readFromFile( std::vector< unsigned char >& buffer,
                   string filename,
                   size_t offset, size_t count );

所以函数从文件中读取偏移量到偏移量+计数到 vector 中;

void readFromFile( std::vector< unsigned char >& buffer,
                   string filename,
                   size_t offset, size_t count )
{
    // get file size and reallocate the buffer
    size_t fsize = filesize( filename );
    buffer.reserve( buffer.size() + size );

    // open the file
    ifstream file( filename );

    // first way
    file.seekg( offset );
    file.read( ???? )

    // second way
    istreambuf_iterator< unsigned char > from( file );
    istreambuf_iterator< unsigned char > eof;

    advance( from, offset );
    copy( from, eof, back_inserter( buffer );
}

第一种方式我不知道如何一次读取文件。在第二种方式中,读取操作非常慢,因为我逐字节读取。

有更好的选择吗?

编辑

感谢@Ben Voigt

我写了这两个简单的函数:

inline std::streamsize filesize( const std::string& filename )
{
    std::ifstream in( filename, std::ifstream::in | std::ifstream::binary );
    if ( !in ) throw std::invalid_argument
    {
        "filesize error: invalid filename"
    };

    in.seekg( 0, std::ifstream::end );
    return in.tellg();

    // here the file is closed. so no need to restore the get pointer
}

inline std::streamsize filesize( std::ifstream& file )
{
    file.seekg( 0, std::ifstream::end );
    const auto size = file.tellg();
    file.seekg( 0 );      // restore the get pointer
    return size;
}

template< typename RAIter >
inline void read_file( std::istream& file,
                       RAIter first, RAIter last,
                       std::streamsize offset = 0
                        )
{
    const auto size = last - first;
    file.seekg( offset, std::ifstream::beg );
    file.read( reinterpret_cast< char* >( &*first ), size );
}

template<>
inline void read_file( std::istream& file,
                       unsigned char*  first, unsigned char*  last,
                       std::streamsize offset /*= 0 no default argument in template spacalization. */
        )
{
    const auto size = last - first;
    file.seekg( offset, std::ifstream::beg );
    file.read( reinterpret_cast< char* >( first ), size );
}

所以这个函数现在变得简单了:

vector< unsigned char > buffer;
// do something with buffer

const string filename{ "blabla" };

const auto size = filesize( filename );

// resize the buffer
auto const OLD_LEN = buffer.size();
buffer.resize( OLD_LEN + size );

size_t startOffset = 0;       // from where to star reading from file
size_t cont = size;           // how manny bytes read from file

// read filename from startOffset to startOffset + count, appendeing in buffer
ifstream file( filename );
read_file( file,
           buffer.data() + OLD_LEN,
           buffer.data() + OLD_LEN + count,
           startOffset
           );

最佳答案

auto old_end = buffer.size();
buffer.resize( old_end + blocksize );

//...

file.read( &buffer[old_end], blocksize );
auto actual_size = file.gcount;
if (actual_size < blocksize) buffer.resize(old_end + actual_size);

关于c++ - 将文件的一部分读入 std::vector<char> 的有效方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18495790/

相关文章:

c++ - 在 Linux 和 Eclipse IDE 中使用 FFTW 库的问题

c++ - 改进点集的最小距离过滤器

c++ - 读取字符串直到行尾?

c++ - 欧拉计划问题 17 - 怎么了?

c++ - 以原子方式读取非原子变量?

c++ - 为什么代码不能打印这个二维数组的元素?

c++ - 指向 std::array 成员的指针

c++ - 试图区分不同类型的右值——文字和非文字

linux - 在双网卡 Linux 系统中对特定网卡执行连接

c++ - 内存初始化/删除这么耗时?