windows 文件,文件夹操作

时间:2021-05-24 08:18:38   收藏:0   阅读:0

1.C标准的文件读写

技术图片
 1             FILE* pFile = nullptr;
 2             const char* pcFilePath = ".//1.text";
 3 
 4             //文件写
 5             errno_t err = fopen_s(&pFile, pcFilePath, "ab");
 6             if ((0 == err) && (nullptr != pFile))
 7             {
 8                 const char* pcData = "我们我们我们我们";
 9                 UINT uiFileLen = strlen(pcData) + 1;
10 
11                 UINT uiWriteLen = fwrite(pcData, sizeof (char), uiFileLen, pFile);
12                 if (uiFileLen == uiWriteLen)
13                 {
14                     printf("file write successfully\n");
15                 }
16                 fclose(pFile);
17             }
View Code

2.MFC封装的文件读写

技术图片
 1             CFile objFile;
 2 
 3             //文件写
 4             BOOL bOpen = objFile.Open(_T(".//2.text"), CFile::modeCreate | CFile::modeWrite);
 5             if (TRUE == bOpen)
 6             {
 7                 const PCHAR pcData = "我们会不会有明天";
 8                 objFile.Write(pcData, (strlen(pcData) + 1) * sizeof (char));
 9                 objFile.Close();
10             }
11 
12             //文件读
13             BOOL bOpen1 = objFile.Open(_T(".//2.text"), CFile::modeRead);
14             if (TRUE == bOpen1)
15             {
16                 CHAR acBuf[1024] = { 0 };
17                 UINT uiFileLen = (UINT)objFile.GetLength();
18 
19                 UINT uiReadLen = objFile.Read(acBuf, uiFileLen);
20                 if (uiReadLen == uiFileLen)
21                 {
22                     printf("MFC: file read: %s \n", acBuf);
23                 }
24                 objFile.Close();
25             }
View Code

3.win32下的文件及文件夹操作

技术图片
 1 #ifndef _FILEUTIL_H__
 2 #define _FILEUTIL_H__
 3 
 4 class ICallBackFile
 5 {
 6 public:
 7     virtual void CallBackEnumFiles(LPCTSTR lpFileName) = 0;
 8     virtual void CallBackOpenDlgFile(LPCTSTR lpFileName) = 0;
 9     virtual void CallBackOpenDlgDir(LPCTSTR lpDirName) = 0;
10 public:
11     ICallBackFile() = default;
12     virtual ~ICallBackFile() = default;
13 };
14 
15 class CFileUtil
16 {
17 public:
18     /*文件读写操作都是原子操作,使用单例来降低被重复并行操作的风险*/
19     static CFileUtil& GetInstance() { static CFileUtil objFileUtil; return objFileUtil; }
20 
21     /*创建文件*/
22     HANDLE CreateFile(LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwCreationDisposition,
23         DWORD dwShareMode = 0, LPSECURITY_ATTRIBUTES lpSecurityAttributes = nullptr,
24         DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL, HANDLE hTemplateFile = nullptr);
25 
26     /*写文件*/
27     BOOL WriteFile(HANDLE hFile,LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPDWORD lpNumberOfBytesWritten,
28         LPOVERLAPPED lpOverlapped = nullptr);
29 
30     /*读文件*/
31     BOOL ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead,
32         LPOVERLAPPED lpOverlapped = nullptr);
33 
34     /*关闭文件句柄*/
35     BOOL CloseHandle( HANDLE hObject );
36 
37     /*获取文件大小*/
38     DWORD GetFileSize(HANDLE  hFile, LPDWORD lpFileSizeHigh = nullptr);
39 
40     /*设置文件指针位置*/
41     BOOL SetFilePointer(HANDLE hFile, LONGLONG llOffset, DWORD dwMoveMethod, PLARGE_INTEGER lpNewFilePointer = nullptr);
42 
43     /*删除文件*/
44     BOOL DeleteFile(LPCTSTR lpFileName);
45 
46     /*文件遍历:遍历指定目录下所有指定类型的文件(包含子目录的遍历)*/
47     void EnumFile(TCHAR* pcDirPath, TCHAR* pcFileExt);
48 
49     /*获取文件名:从完整的文件名路径中分离出文件名*/
50     PTSTR PathFindFileName(PTSTR ptFilePath);
51 
52     /*获取文件后缀名:从完整的文件名路径中分离出后缀名*/
53     BOOL  PathFindFileExt(PTSTR ptFilePath, PTSTR ptExtBuf, UINT uiBufSize);
54 
55     /*文件夹的创建*/
56     BOOL CreateDirectory(LPCTSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes = nullptr);
57 
58     /*文件夹的删除*/
59     BOOL RemoveDirectory(LPCTSTR lpPathName);
60 
61     /*选择文件:打开文件的对话框*/
62     void OpenDlgFile(HWND hWnd = nullptr, DWORD dwFlags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST, LPCTSTR lpFileType = _T("*.*"));
63 
64     /*选择文件夹:打开文件夹的对话框*/
65     void OpenDlgDir(LPCTSTR lpTitle = _T("选择文件夹"), UINT uiFlags = BIF_RETURNONLYFSDIRS | BIF_DONTGOBELOWDOMAIN | BIF_USENEWUI);
66 
67 public:
68     ICallBackFile* GetCallbackFile() const;
69     void SetCallbackFile(ICallBackFile* pCallbackFile);
70 
71 public:
72     CFileUtil() : m_pCallBackFile(nullptr) {};
73     CFileUtil(const CFileUtil&) = delete;//禁类赋值
74     CFileUtil& operator=(const CFileUtil&) = delete;//禁类拷贝
75 
76     ~CFileUtil() = default;
77 
78 private:
79     ICallBackFile* m_pCallBackFile;
80 };
81 
82 #endif /*_FILEUTIL_H__*/
View Code

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!