Results 1 to 4 of 4

Thread: convert file descriptor to FILE pointer or file stream on windows

  1. #1
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default convert file descriptor to FILE pointer or file stream on windows

    Hi,
    Short question..

    Is there any way I can convert a file descriptor to either a FILE* or a C++ stream ifstream on windows.
    Qt Code:
    1. HANDLE hFile;
    2. hFile = CreateFile(filename, // open One.txt
    3. GENERIC_READ, // open for reading
    4. 0, // do not share
    5. NULL, // no security
    6. OPEN_EXISTING, // existing file only
    7. FILE_ATTRIBUTE_NORMAL, // normal file
    8. NULL); // no attr. template
    9.  
    10. int fd = (int)hFile;
    11. FILE* pFile = fd; ///how to convert fd to FILE* ?????????
    12. or
    13. ifstream ifs = fd;///how to convert fd to ifstream ???????????????
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: convert file descriptor to FILE pointer or file stream on windows

    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...
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: convert file descriptor to FILE pointer or file stream on windows

    Quote Originally Posted by high_flyer View Post
    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????

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: convert file descriptor to FILE pointer or file stream on windows

    Use _fdopen once you have the file handle.
    See this: http://msdn2.microsoft.com/en-us/lib...82(VS.80).aspx

    Regards

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.