windows 文件,文件夹操作

2021-05-28 01:03

阅读:325

标签:bsp   选择   word   targe   type   磁盘   存储   ati   读写   

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封装的文件读写

  • 简洁方便,缺点也明显,只限于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下的文件及文件夹操作

  • MFC封装的CFile类本质是对CreateFile,WriteFile,ReadFile,CloseHandle接口的封装;
  • CreateFile除了可以打开文件外,还可以打开磁盘,驱动等,所以封装时作了兼容性处理;
  • wprintf汉字时出现"??",跟大小端的存储格式有关,加上setlocale()可解决;当使用unicode输入汉字到文本后,使用记事本打开呈乱码,原因是系统自动识别为ANSI,可以在写入汉字之前,先写入“FEFF”
  • 打开文件后,直接进行读写操作时,一定要注意文件指针的位置,适当地使用SetFilePointer;
技术图片技术图片
 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
  • 完整代码地址:https://files-cdn.cnblogs.com/files/blogs/666666/string_util.zip

 

windows 文件,文件夹操作

标签:bsp   选择   word   targe   type   磁盘   存储   ati   读写   

原文地址:https://www.cnblogs.com/zhoushen4576/p/14765918.html


评论


亲,登录后才可以留言!