学习vc++的第八天--遍历盘符
因为要进入win界面编程了.所以我把构造类和集合的方法在这里完整的实现的一次,以及令人难忘的auto和static的相对关系....
CharTool.h
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#pragma once #include "TCHAR.h" class CharTool { public: static char* TcharToChar(const TCHAR* tchar); static TCHAR* CharToTchar(const char* _char); };
CharTool.cpp
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include "CharTool.h" #include "atlbase.h" #include "atlstr.h" /// <summary> /// TCHAR*转char* /// </summary> /// <param name="tchar">The tchar.</param> /// <returns>char *.</returns> char* CharTool::TcharToChar(const TCHAR* tchar) { // _tcslen计算的是字符数,不是字节数,一个汉字也是一个字符长度 //获取字节长度 INT iLength = WideCharToMultiByte(CP_ACP, 0, tchar, -1, NULL, 0, NULL, NULL); char* _char = new char[iLength]; memset(_char, 0, iLength);//填充内存 //将tchar值赋给_char WideCharToMultiByte(CP_ACP, 0, tchar, -1, _char, iLength, NULL, NULL); return _char; } /// <summary> /// const char*转TCHAR* /// </summary> /// <param name="d2">The d2.</param> /// <returns>TCHAR *.</returns> TCHAR* CharTool::CharToTchar(const char* _char) { // _tcslen计算的是字符数,不是字节数,一个汉字也是一个字符长度 int iLength = MultiByteToWideChar(CP_ACP, //本机代码页,窄字节可变,宽字节不可变 0, //一般不用 _char, //窄字节指针 -1, //按照字符串处理 NULL, //目标空间缓冲区地址 0); //目标空间长度 if (iLength <= 0) return NULL; //返回需要的空间数 //申请大小,填充内存 TCHAR* tchar = new TCHAR[iLength]; wmemset(tchar, 0, iLength);//填充内存 //根据内存大小再次实现转换 MultiByteToWideChar(CP_ACP, 0, _char, -1, tchar, iLength); return tchar; }
JDrive.h
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#pragma once #include <Windows.h> #include <string> #include "atlbase.h" #include "atlstr.h" #include "TCHAR.h" using namespace std; //#define JStr PTCHAR //#define JStr const char* //#define JStr const CHAR* #define JStr TCHAR* /// <summary> /// 类:驱动器 /// </summary> class JDrive { public: //变量 JStr Drive; //驱动器盘符 UINT Type; //驱动器类型 JStr Name; //驱动器名称 INT Size; //容量 INT AlreadySize; //已用 //构造函数 JDrive(); JDrive(JStr _Drive, INT _Type, JStr _Name, INT _Size, INT _AlreadySize); ~JDrive(); void Exchange(JStr d1, JStr d2); //拷贝构造函数 JDrive(const JDrive& st); JDrive& operator= (const JDrive& st);//赋值运算符 //访问私有函数 INT GetSurplus(); JStr GetTypeName(); char* TcharToChar(const TCHAR* tchar); TCHAR* CharToTchar(const char* _char); private: };
JDrive.cpp
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include "JDrive.h" #include "CharTool.h" JDrive::JDrive() { // const JStr p = _T(""); // TCHAR szVer[1] = { 0 };//初始化 Drive; //驱动器盘符 Name; //驱动器名称 Type = 0; //驱动器类型 Size = 0; //容量 AlreadySize = 0; //已用 } /// <summary> /// 驱动器信息类 <see cref="JDrive"/> class. /// </summary> /// <param name="_Drive">盘符.</param> /// <param name="_Type">类型.</param> /// <param name="_Name">名称.</param> /// <param name="_Size">容量.</param> /// <param name="_AlreadySize">已用.</param> JDrive::JDrive(JStr _Drive, INT _Type, JStr _Name, INT _Size, INT _AlreadySize) :Drive(_Drive), Type(_Type), Name(_Name), Size(_Size), AlreadySize(_AlreadySize) { JDrive::JDrive(); } JDrive::~JDrive() { if (!Name) { delete Name; Name = NULL; } if (!Drive) { delete Drive; Drive = NULL; } } //拷贝构造函数 JDrive::JDrive(const JDrive& st) { if (this != &st) { Size = st.Size; AlreadySize = st.AlreadySize; Type = st.Type; //指针交换 /* Drive = st.Drive; Name = st.Name;*/ //如果以前有内容,就先删除,防止内存泄露 JDrive::~JDrive(); Exchange(Drive, st.Drive); Exchange(Name, st.Name); } } /// <summary> /// 交换两个TCHAR*内容 /// </summary> /// <param name="d1">目标</param> /// <param name="d2">来源</param> void Exchange(const CHAR* d1, const CHAR* d2) { if (d2)//指针为真 { int n_len = strlen(d2) + 1; char* ch = new char[n_len];//new申请堆内存不会在当前作用域不释放,要在析构函数上面释放 memset(ch, 0, n_len); strcpy_s(ch, n_len, d2); d1 = ch; } } /// <summary> /// 交换两个TCHAR*内容 /// </summary> /// <param name="d1">目标</param> /// <param name="d2">来源</param> void JDrive::Exchange(TCHAR* d1, TCHAR* d2) { //交换两个TCHAR*内容 if (d2)//指针为真 { INT n_len = lstrlen(d2) + 1; d1 = new TCHAR[n_len];//new申请堆内存不会在当前作用域不释放,要在析构函数上面释放 memset(d1, 0, n_len); lstrcpy(d1, d2); } } //赋值运算符 JDrive& JDrive::operator= (const JDrive& st) { JDrive::JDrive(st); return *this; } /// <summary> /// 求剩余容量大小 /// </summary> /// <returns>INT.</returns> INT JDrive::GetSurplus() //inline { return Size - AlreadySize; } /// <summary> /// 获取磁盘类型名称 /// </summary> /// <returns>string.</returns> JStr JDrive::GetTypeName() { const char* str = ""; switch (Type) { case DRIVE_UNKNOWN: str = "[未知类型磁盘]"; break; case DRIVE_NO_ROOT_DIR: str = "[路径错误]"; break; case DRIVE_REMOVABLE: str = "[可移动磁盘]"; break; case DRIVE_FIXED: str = "[固定磁盘]"; break; case DRIVE_REMOTE: str = "[网络磁盘]"; break; case DRIVE_CDROM: str = "[光驱]"; break; case DRIVE_RAMDISK: str = "[RAM磁盘]"; break; derault: break; } JStr js = CharTool::CharToTchar(str); return js; }
main:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// https://www.bilibili.com/video/av79302998?p=66 int main() { vector<JDrive*> ve; //获取所有驱动器 DWORD dwRet = GetLogicalDriveStrings(0, NULL); //拿到所有盘符字符大小 if (dwRet > 0) { INT big = dwRet; TCHAR* pBurrer = new TCHAR[big];//储存所有盘符 ZeroMemory(pBurrer, big * sizeof(TCHAR));//初始化内存.通常用来给new数组用的 dwRet = GetLogicalDriveStrings(big, pBurrer); TCHAR* pSignal = pBurrer; while (*pSignal) { JDrive* jDrive = new JDrive(); UINT nDriveType = GetDriveType(pSignal); jDrive->Type = nDriveType; jDrive->Drive = pSignal; //wcout << "驱动器:" << pSignal << endl; //cout会读到\0 两个\0才是真的结束 ve.push_back(jDrive); pSignal += _tcslen(pSignal) + 1;//+1是越过'\0' } for (size_t i = 0; i < ve.size(); i++) { wcout << "驱动器:" << ve[i]->Drive << endl; JStr str = ve[i]->GetTypeName(); char* ca = CharTool::TcharToChar(str); wcout << "类型:" << ca << endl; if (ve[i]->Name) { wcout << "名称:" << ve[i]->Name << endl; } else { wcout << "名称:无" << endl; } wcout << "总容量:" << ve[i]->Size << endl; wcout << "已用容量:" << ve[i]->AlreadySize << endl; wcout << "剩余容量:" << ve[i]->GetSurplus() << endl; wcout << endl; } } cin.get(); }