【C++學習筆記】文件操作編程基礎

  • 需要用到的幾個函數
//*********************************************************************************************************************
HANDLE CreateFile(LPCTSTR lpFileName,                         // file name
				  DWORD dwDesiredAccess,                      // access mode
				  DWORD dwShareMode,                          // share mode
				  LPSECURITY_ATTRIBUTES lpSecurityAttributes, // SD
				  DWORD dwCreationDisposition,                // how to create
				  DWORD dwFlagsAndAttributes,                 // file attributes 一般爲FILE_ATTRIBUTE_NORMAL 默認屬性
				  HANDLE hTemplateFile                        // handle to template file 一般爲0
				  );
//參數信息
DWORD dwDesiredAccess:
	#define GENERIC_READ          (0x80000000L)
	#define GENERIC_WRITE         (0x40000000L)
DWORD dwShareMode:
	#define FILE_SHARE_READ                 0x00000001  
	#define FILE_SHARE_WRITE                0x00000002  
	#define FILE_SHARE_DELETE               0x00000004  
DWORD dwCreationDisposition:
	#define CREATE_NEW            1 //創建文件,如文件存在則會出錯
	#define CREATE_ALWAYS         2 //創建文件,會改寫前一個文件
	#define OPEN_EXISTING         3 //文件必須已經存在,由設備提出要求
	#define OPEN_ALWAYS           4 //如文件不存在則創建它
	#define TRUNCATE_EXISTING     5 //將現有文件縮短爲零長度
//返回值
	HANDLE//正常返回文件句柄
	INVALID_HANDLE_VALUE//出錯
//*********************************************************************************************************************
BOOL ReadFile(HANDLE hFile,                      // handle to file
			  LPVOID lpBuffer,                   // data buffer
		      DWORD nNumberOfBytesToRead,        // number of bytes to read
			  LPDWORD lpNumberOfBytesRead,       // number of bytes read
			  LPOVERLAPPED lpOverlapped          // overlapped buffer 一般設置爲NULL
			  );
//*********************************************************************************************************************
BOOL WriteFile(HANDLE hFile,                     // handle to file
			   LPCVOID lpBuffer,                 // data buffer
		   	   DWORD nNumberOfBytesToWrite,      // number of bytes to write
			   LPDWORD lpNumberOfBytesWritten,   // number of bytes written
			   LPOVERLAPPED lpOverlapped         // overlapped buffer 一般設置爲NULL
			   );
//*********************************************************************************************************************
DWORD SetFilePointer(HANDLE hFile,                // handle to file
					 LONG lDistanceToMove,        // bytes to move pointer
					 PLONG lpDistanceToMoveHigh,  // bytes to move pointer
					 DWORD dwMoveMethod           // starting point
					 );
//參數信息
DWORD dwMoveMethod
	#define FILE_BEGIN           0
	#define FILE_CURRENT         1
	#define FILE_END             2
//*********************************************************************************************************************
BOOL CloseHandle(HANDLE hObject);


發佈了64 篇原創文章 · 獲贊 6 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章