c說話法式設計文件操作辦法示例(CreateFile和fopen)。本站提示廣大學習愛好者:(c說話法式設計文件操作辦法示例(CreateFile和fopen))文章只能為提供參考,不一定能成為您想要的結果。以下是c說話法式設計文件操作辦法示例(CreateFile和fopen)正文
實例:
(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("沒法翻開充值卡文件");