how to create a ofstream from a Windows file handle?

243 views
Skip to first unread message

Buboi Peng

unread,
Sep 30, 2005, 4:01:28 AM9/30/05
to
Hi All,

I get the file handle using API GetStdHandle, but how to convert this handle
to an ofstream?
Thanks in advance.

Buboi


Ulrich Eckhardt

unread,
Sep 30, 2005, 4:34:58 AM9/30/05
to
Buboi Peng wrote:
> I get the file handle using API GetStdHandle, but how to convert this
> handle to an ofstream?

There is no way to do so directly, ostreams (why oFstream?) don't have an
API fo that. However, there is help:
- Many vendors include an extension to their IOStream lib that takes handles
or filedescriptors.
- You could wrap the handle in a streambuf and use that with any IOStream.
- You could take a look at Boost's latest IO library, which includes such
functionality.

Uli

Igor Tandetnik

unread,
Sep 30, 2005, 8:03:45 AM9/30/05
to
"Buboi Peng" <bu...@public2.sta.net.cn> wrote in message
news:uoz7qUZx...@TK2MSFTNGP11.phx.gbl

> I get the file handle using API GetStdHandle, but how to convert this
> handle to an ofstream?

Standard handles are already wrapped into streams - cin, cout, cerr.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


CheckAbdoul

unread,
Sep 30, 2005, 11:59:25 AM9/30/05
to
See if the following link helps you

http://www.flounder.com/handles.htm

--
Cheers
Check Abdoul [VC++ MVP]
-----------------------------------

"Buboi Peng" <bu...@public2.sta.net.cn> wrote in message

news:uoz7qUZx...@TK2MSFTNGP11.phx.gbl...

m_r_v...@hotmail.com

unread,
Apr 15, 2018, 12:46:18 PM4/15/18
to
What worked for me was Ulrich suggestion to use boost iostreams library:

My system config is Windows 7, GCC 5.1.0, Boost 1_64_0.

My single test.cpp file is:

#define STRICT 1
#include <windows.h>
#include <cstddef>
#include <cstdio>
#include <cstdint>

#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>

namespace io = boost::iostreams;

int main()
{
HANDLE hFile = CreateFile(
"filename.txt",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL );

if ( hFile == INVALID_HANDLE_VALUE )
{
printf( "CreateFile failed %d\n", GetLastError() );

getchar();
return EXIT_FAILURE;
}

io::file_descriptor_sink fd_sink_device(
hFile,
io::never_close_handle );

io::stream_buffer<io::file_descriptor_sink> streambuf( fd_sink_device );
std::ostream os( &streambuf );

os << "File contents" << std::endl;

CloseHandle( hFile );

getchar();
return EXIT_SUCCESS;
}

the makefile I used was:
FLAGS = -std=c++14 -pedantic -Wextra -Wlogical-op
INCPATHBOOST = -I"$(BOOST)"
LIBPATHBOOST = -L"$(BOOST_LIB)"
LIBSBOOST = -l:libboost_iostreams-mgw51-mt-s-1_64.a
LIBSWIN = -l:libkernel32.a -l:libuser32.a -l:libshell32.a -l:libadvapi32.a\
-l:libws2_32.a -l:liboleaut32.a -l:libimm32.a -l:libwinmm.a -l:libole32.a\
-l:libuuid.a -l:libopengl32.a -l:libole32.a -l:libgdi32.a

all: test.exe

test.exe : test.o
g++ -static -mconsole $(LIBPATHBOOST) test.o $(LIBSBOOST) $(LIBSWIN) -o test.exe

test.o : test.cpp
g++ -c $(INCPATHBOOST) $(FLAGS) test.cpp

clean:
del test.o test.exe

Ok, you might argue not all of this is necessary to communicate. But when I speak for myself I've such an extreme dislike for any documentation that is incomplete that left me wondering how all of the concepts fit together to make an program work. This including the Boost iostreams documentation I've been fighting with today. So I hope my example will be useful for many people. And experts are very welcome to comment and improve.
Reply all
Reply to author
Forward
0 new messages