執行個體:
(1)第一種方法CreateFile
複製代碼 代碼如下:
#include "stdafx.h"
#include <windows.h>
void main(int argc, char* argv[])
{
HANDLE hDevice = CreateFile("C://S.txt",
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hDevice == INVALID_HANDLE_VALUE)
{
printf("Failed to obtain file with %d error code !/n",GetLastError());
return;
}
DWORD dwSize = GetFileSize(hDevice,NULL);
printf("%d /n",dwSize);
char chBuffer[10] = "5469";
DWORD dwWriteSize = 0;
BOOL bRet = WriteFile(hDevice,chBuffer,4,&dwWriteSize,NULL);
if(bRet)
{
printf("write file success /n");
}
FlushFileBuffers(hDevice); //將緩衝區資料寫入磁碟
LONG IDistance = 0;
DWORD dwPtr = SetFilePointer(hDevice,IDistance,NULL,FILE_BEGIN); //調整檔案指標到檔案開頭
DWORD dwReadSize = 0;
bRet = ReadFile(hDevice,chBuffer,10,&dwReadSize,NULL);
if (bRet)
{
printf("chbuffer is %s /n",chBuffer);
}
CloseHandle(hDevice);
return ;
}
(2)第二種方法流檔案操作FILE
複製代碼 代碼如下:
char datain[101];
FILE *fp_sys;
fp_sys = fopen("要開啟的檔案名稱", "rb"); //第二個參數為開啟方法,r代表讀,b代表二進位方式
if(fp_sys == NULL) {
AfxMessageBox("無法開啟儲值卡檔案");