ifstrem bytes buffer

I need to read whole binary file and pass bytes buffer to third party function.
Currently i am filling the file content into vector, but this seems somewhat redundant:
1
2
3
4
5
6
7
8
std::ifstream is("file.dat", std::ios::binary);
	is.seekg (0, is.end);
	auto length = is.tellg();
    is.seekg (0, is.beg);
	std::vector<char> vFile(length);
	is.read((char*)&vFile[0], vFile.size());
	is.close();
Foo( (LPCVOID)&vFile[0], vFile.size(), ...);


Is there some ifstream member that provide access to bytes buffer?

Thank you for your time.
an ifstream object does not hold the entire file, it only holds a few kb window into the file (the buffer), which slides forward as you keep hitting the end with ifstream::read() (conceptually: in practice, for a non-converting byte stream such as this, read() will be usually translated to a single OS read call or even a memory map)

If you want to avoid having to load the entire file into a vector, you could memory-map the file (using your OS API or boost.iostreams) and pass the pointer to the file's contents directly to foo(). Otherwise, a copy will have to happen, one way or another.
Last edited on
Thank you.
PS: looked at the execution of ifstream::read() for a large binary file on a few compilers out of curiosity:
xlc called read() in a loop (reading 4k chunks)
sun/libCstd called lseek/read() in a loop (reading 8K chunks)
sun/stlport4 called lseek/mmap/munmap in a loop (mapping 1M chunks)
gcc called read() for the whole thing (good job implementing that xsgetn)
Topic archived. No new replies allowed.