文件的HANDLE轉化為FILE*
#include <iostream>
#include <fcntl.h>
#include <io.h>
#include <afxwin.h>
using namespace std;
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);
fflush(pFile);//立即寫入文件
}
CloseHandle(hFile);
}
int main()
{
Test();
return 0;
}
#include <fcntl.h>
#include <io.h>
#include <afxwin.h>
using namespace std;
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);
fflush(pFile);//立即寫入文件
}
CloseHandle(hFile);
}
int main()
{
Test();
return 0;
}
posted on 2010-12-25 15:19 天下 閱讀(423) 評論(0) 編輯 收藏 引用 所屬分類: Win32

