windows api 每日一练(3)文件操作
2009-11-17 11:10 Clingingboy 阅读(1293) 评论(1) 编辑 收藏 举报参考:http://www.yesky.com/99/1824599.shtml
1.OpenFile 全功能函数,可以打开,创建,删除文件
HFILE WINAPI OpenFile( __in LPCSTR lpFileName, __out LPOFSTRUCT lpReOpenBuff, __in UINT uStyle );
2.获取文件长度
DWORD WINAPI GetFileSize( __in HANDLE hFile, __out_opt LPDWORD lpFileSizeHigh );
3.获取文件类型
DWORD WINAPI GetFileType( __in HANDLE hFile );
BOOL WINAPI GetFileTime( __in HANDLE hFile, __out_opt LPFILETIME lpCreationTime, __out_opt LPFILETIME lpLastAccessTime, __out_opt LPFILETIME lpLastWriteTime );
5.获取文件名
(1)全名
DWORD WINAPI GetFullPathName( __in LPCTSTR lpFileName, __in DWORD nBufferLength, __out LPTSTR lpBuffer, __out LPTSTR* lpFilePart );
DWORD WINAPI GetLongPathName( __in LPCTSTR lpszShortPath, __out LPTSTR lpszLongPath, __in DWORD cchBuffer );
(3)短路径名
DWORD WINAPI GetShortPathName( __in LPCTSTR lpszLongPath, __out LPTSTR lpszShortPath, __in DWORD cchBuffer );
Test
#include <windows.h> #include <stdio.h> #include <iostream> int main() { std::wstring originalStrFile(L"./test.txt"); HANDLE hFile; hFile = CreateFile(originalStrFile.c_str(), // file to open GENERIC_READ, // open for reading FILE_SHARE_READ, // share for reading NULL, // default security OPEN_EXISTING, // existing file only FILE_ATTRIBUTE_NORMAL, // normal file NULL); //获?取?文?件?大?小? DWORD dwSize =GetFileSize(hFile,NULL); //获?取?文?件?类?型? DWORD fileType=GetFileType(hFile); //获?取?文?件?时?间?信?息? FILETIME time1; FILETIME time2; FILETIME time3; GetFileTime(hFile,&time1,&time2,&time3); //获?取?文?件?名? DWORD bufferLength=100; TCHAR buffer[100]=TEXT(""); TCHAR* lpPart[100]={NULL}; //获?取?全?名? GetFullPathName(originalStrFile.c_str(),bufferLength,buffer,lpPart); //获?取?长?路?径?名? GetLongPathName(originalStrFile.c_str(),buffer,bufferLength); //获?取?短?路?径?名? GetShortPathName(originalStrFile.c_str(),buffer,bufferLength); //关?闭?句?柄?对?象? CloseHandle(hFile); }