win32SDK中操作文件用HANDLE,如何通过HANDLE得到文件的FILE指针呢?
HANDLE hFile = CreateFile(...);
FILE* pFile = xxxx(hFile);这个xxxx函数如何写呢?
FILE* xxxx(HANDLE hFile)
{
.....
}
HANDLE hFile = CreateFile(...);
FILE* pFile = xxxx(hFile);这个xxxx函数如何写呢?
FILE* xxxx(HANDLE hFile)
{
.....
}
解决方案 »
- VC2010类列表里的两个前面带大括号的是一个类吗?还是什么别的东西,本人新手,请高人指点
- button控件控制picture control显示图片
- 内存拷贝
- 用WinSock编写一个服务器和客户端,服务器能创建成功,但是连结不上?
- 后缀为def的文件是什么文件?
- access中Note和Demo類型在xp下不能用destinct關鍵字查詢,而varchar的最大長度只有255.
- 字符串相加问题
- 为什么不可以用ADO打开相对路径的数据库
- 操作系统难道被我修改了?大虾来看看这到底是怎么一回事.
- 我定义了一个全局的const char[100],请问如何设置初值?
- 我想请问一下各位高手一个简单的问题.
- COM Attribute的问题
{
return _open_osfhandle((intptr_t)hFile, _O_TEXT)
}
if (nHandle != -1)
m_pStream = _fdopen(nHandle, szMode);
{
int nHandle = _open_osfhandle((intptr_t)hFile, _O_TEXT);
if (nHandle != -1)
return _fdopen(nHandle, szMode);
else
return NULL;
}
{
int nHandle = _open_osfhandle((intptr_t)hFile, _O_TEXT);
if (nHandle != -1)
return _fdopen(nHandle, szMode);
else
return NULL;
}
just try
谢谢你的提示。
但我试了一下不行。测试代码如下.不知问题出在哪里?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);
}
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;
}
--------------------------------------------------------------------------------
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 );
}