PDA

View Full Version : convert file descriptor to FILE pointer or file stream on windows



vermarajeev
24th May 2007, 15:16
Hi,
Short question..

Is there any way I can convert a file descriptor to either a FILE* or a C++ stream ifstream on windows.

HANDLE hFile;
hFile = CreateFile(filename, // open One.txt
GENERIC_READ, // open for reading
0, // do not share
NULL, // no security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template

int fd = (int)hFile;
FILE* pFile = fd; ///how to convert fd to FILE* ?????????
or
ifstream ifs = fd;///how to convert fd to ifstream ???????????????

high_flyer
24th May 2007, 15:48
1. What does this have to do with Qt programming?
2. Why do you use CreateFile if you want to use FILE or fstreams?
Just initilaize an fstream object, or use FILE C style all the way...

vermarajeev
25th May 2007, 08:39
1. What does this have to do with Qt programming?
2. Why do you use CreateFile if you want to use FILE or fstreams?
Just initilaize an fstream object, or use FILE C style all the way...

I need to lock the file but Qt doesnt provide any, so using operating systems API's to make that happen... Any idea????

marcel
25th May 2007, 08:49
Use _fdopen once you have the file handle.
See this: http://msdn2.microsoft.com/en-us/library/dye30d82(VS.80).aspx

Regards