【发布时间】:2023-03-16 01:52:01
【问题描述】:
因此,该计划的目的是找出可用的驱动器以及它们是什么。但是,我似乎无法将字符串变量直接传递给GetDriveTypesA()。但是,如果我只是做GetDriveTypesA("C:\\"),它就会成功。
请有人帮我找到解决这个问题的方法,程序如下。谢谢你:)
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
int main() {
int i;
int driveType;
std::string path;
int binary[26] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //To save the binary number of the GetLogicalDrives
std::string actualDrives[26];
std::string letters[26] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}; //letters of the alphabet
DWORD drives = GetLogicalDrives(); //finds the bit mask of logical drives
for(i=0; drives>0; i++) { //changes the integer into a binary number
binary[i]=drives%2;
drives= drives/2;
}
for (i=0; i<26; i++){ //finds out what drive letters are available
if (binary[i]==1){
actualDrives[i] = letters[i];
}
}
for (i=0; i<26; i++){ \trying to find the drive type
if (actualDrives[i] != ""){
cout << "Actual drive:";
cout<< actualDrives[i] <<endl;
path = actualDrives[i] + ":\\";
cout << path <<endl;
driveType = GetDriveTypeA(path); //the part of the code that does not work but does work if a string is just entered
cout<< driveType << endl;
}
}
}
【问题讨论】:
-
您的编译器对此有何评论?
-
改用 path.data()。