Instance:
(1) The first method of CreateFile
Copy Code code as follows:
#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); Write buffer data to disk
LONG idistance = 0;
DWORD dwptr = SetFilePointer (Hdevice,idistance,null,file_begin); Adjust file pointer to start of file
DWORD dwreadsize = 0;
BRet = ReadFile (hdevice,chbuffer,10,&dwreadsize,null);
if (BRet)
{
printf ("Chbuffer is%s/n", chbuffer);
}
CloseHandle (Hdevice);
return;
}
(2) The second method of streaming file operation files
Copy Code code as follows:
Char datain[101];
FILE *fp_sys;
Fp_sys = fopen ("FileName to open", "RB"); The second argument is open method, R is read, B represents binary
if (Fp_sys = = NULL) {
AfxMessageBox ("Unable to open the Recharge card file");