文件的HANDLE如何转化为FILE* ?

力为
博客专家认证
2005-10-10 04:28:19
win32SDK中操作文件用HANDLE,如何通过HANDLE得到文件的FILE指针呢?
HANDLE hFile = CreateFile(...);
FILE* pFile = xxxx(hFile);

这个xxxx函数如何写呢?
FILE* xxxx(HANDLE hFile)
{
.....
}
...全文
962 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
vcmute 2005-10-12
  • 打赏
  • 举报
回复
Do Not Mix Operating System and CRT File Handles

Q139640


--------------------------------------------------------------------------------
The information in this article applies to:

Microsoft Visual C++, versions 2.0, 2.1, 2.2, 4.0
Microsoft Visual C++, 32-bit Enterprise Edition, versions 5.0, 6.0
Microsoft Visual C++, 32-bit Professional Edition, versions 5.0, 6.0
Microsoft Visual C++, 32-bit Learning Edition, version 6.0

--------------------------------------------------------------------------------


SUMMARY
In 32-bit Windows-based applications, Windows API functions (OpenFile, CreateFile, and so on) return a file handle that cannot be substituted for a handle returned by a C Runtime Function (_open, _wopen, and so on). You can convert an operating system handle to a CRT handle by using the CRT function _open_osfhandle() as demonstrated in the "Sample Code" section of this article.

NOTE: This distinction is not Win32 specific. However, some programmers using 16-bit products on 16-bit operating systems have mixed operating system and CRT file handles.



MORE INFORMATION
The following sample code shows how to open a file stream from a file handle returned by the OpenFile Windows API function.

Sample Code

/* Compile options needed:none
*/
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <windows.h>

void main( void )
{
HFILE OsFileHandle;
FILE *stream;
int CrtFileHandle;
OFSTRUCT Buffer;
int count = 0;
char inbuf[128];

if ( (OsFileHandle = OpenFile("test.dat", &Buffer, OF_READ))== -1){
printf( "OpenFile Failed");
exit(1);
}

/* convert OS file handle to CRT file pointer */


if ( (CrtFileHandle=_open_osfhandle(OsFileHandle,_O_RDONLY))==-1){
printf( "_open_osfhandle Failed");
exit(1);
}


/* Change handle access to stream access. */
if( (stream = _fdopen( CrtFileHandle, "r" )) == NULL ) {
printf( "_fdopen Failed");
exit( 1 );
}

while( fgets( inbuf, 128, stream ) != NULL )
count++;

/* After _fdopen, close with fclose */
fclose( stream );
printf( "Lines in file: %d\n", count );
}
力为 2005-10-12
  • 打赏
  • 举报
回复
特别感谢Atry() 、djfu(一马平川)、vcmute(横秋)
djfu 2005-10-11
  • 打赏
  • 举报
回复
楼主,这个函数是没有什么问题的,
int n = fputs("write by FILE*!", pFile);
//执行后返回的结果 n为 0,没有写入文件
这个问题是由于你没有把缓存写入文件,改为:
int n = fputs("write by FILE*!", pFile);
//执行后返回的结果 n为 0,没有写入文件
fflush(pFile);//立即写入文件
即可。

完整测试源码如下:
//////////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <io.h>
#include <fcntl.h>
#include <iostream>
using namespace std;

void Test()
{
HANDLE hFile = CreateFile("e:\\test.txt",
GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

char szText[] = "Hello world!\n";
DWORD dwWritten;
WriteFile(hFile, szText, strlen(szText), &dwWritten, NULL);

FILE* pFile = NULL;
int nHandle = _open_osfhandle((long)hFile, _O_TEXT | _O_APPEND);
if (nHandle != -1)
pFile = _fdopen(nHandle, "wt");

if(pFile)
{
int n = fputs("write by FILE*!", pFile);
fflush(pFile);
//执行后返回的结果 n为 0,没有写入文件
}

CloseHandle(hFile);
}

int main()
{
Test();
return 0;
}
dxj1234 2005-10-11
  • 打赏
  • 举报
回复
靠,连这种函数都有,什么情况下会用这些C函数啊
力为 2005-10-11
  • 打赏
  • 举报
回复
to Atry:
谢谢你的提示。
但我试了一下不行。测试代码如下.不知问题出在哪里?

void Test()
{
HANDLE hFile = CreateFile("c:\\test.dat",
GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

char szText[] = "Hello world!\n";
DWORD dwWritten;
WriteFile(hFile, szText, strlen(szText), &dwWritten, NULL);

FILE* pFile = NULL;
int nHandle = _open_osfhandle((long)hFile, _O_TEXT | _O_APPEND);
if (nHandle != -1)
pFile = _fdopen(nHandle, "wt");

if(pFile)
{
int n = fputs("write by FILE*!", pFile);
//执行后返回的结果 n为 0,没有写入文件
}

CloseHandle(hFile);
}
pomelowu 2005-10-10
  • 打赏
  • 举报
回复
同意Atry
djfu 2005-10-10
  • 打赏
  • 举报
回复
FILE* xxxx(HANDLE hFile)
{
int nHandle = _open_osfhandle((intptr_t)hFile, _O_TEXT);
if (nHandle != -1)
return _fdopen(nHandle, szMode);
else
return NULL;
}


just try
jiajie828 2005-10-10
  • 打赏
  • 举报
回复
呵呵...这个都知道, 厉害厉害!
Atry 2005-10-10
  • 打赏
  • 举报
回复
FILE* xxxx(HANDLE hFile)
{
int nHandle = _open_osfhandle((intptr_t)hFile, _O_TEXT);
if (nHandle != -1)
return _fdopen(nHandle, szMode);
else
return NULL;
}
Atry 2005-10-10
  • 打赏
  • 举报
回复
int nHandle = _open_osfhandle((intptr_t)hFile, _O_TEXT);
if (nHandle != -1)
m_pStream = _fdopen(nHandle, szMode);
Atry 2005-10-10
  • 打赏
  • 举报
回复
FILE* xxxx(HANDLE hFile)
{
return _open_osfhandle((intptr_t)hFile, _O_TEXT)
}
legendhui 2005-10-10
  • 打赏
  • 举报
回复
同意楼上
DentistryDoctor 2005-10-10
  • 打赏
  • 举报
回复
不行。

一个是WIN32的文件,一个是C运行时的文件指针。

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧