1。OpenFile function is provided for compatibility with 16-bit versions of Windows!2。Win32-based applications should use the CreateFile function. 3。The SetFilePointer function moves the file pointer of an open file. 4。use WriteFile、ReadFile to Write/Read.
HANDLE hFile;
//初始文件句柄为空
hFile=NULL;//创建文件句柄
hFile=CreateFile("text.txt",GENERIC_WRITE|GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile==NULL)
{
AfxMessageBox("创建文件句柄失败!");
}//将文件指针指到文件末尾
SetFilePointer(hFile,0,NULL,FILE_END);
//将数据写入文件
CString outputText;
DWORD nLen;
WriteFile(hFile,outputText,nLen, (ULONG *)&nLen, NULL);
char buffer[]="Test for Visual C++"
DOWORD nWrite;
hFile=CreateFile((LPCSTR)"c:\\test.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(INVALID_HANDLE_VALUE!=hFile)
{
SetFilePointer(hFile,0,NULL,FILE_END);
WriteFile(hFile,buffer,sizeof(buffer),&nWrite,NULL);
}
else
MessageBox(NULL,"Open File Failed!","Failed:",MB_OK);
break;