[英]Why can't I pass a string into the function GetDriveTypesA()?
So the aim of the program is to find out what drives are available and what they are. 因此,该计划的目的是找出可用的驱动器和可用的驱动器。 However, I cannot seem to pass a string variable straight into
GetDriveTypesA()
. 但是,我似乎无法直接将字符串变量传递给
GetDriveTypesA()
。 But, if I just do GetDriveTypesA("C:\\\\")
it works successfully. 但是,如果我只是执行
GetDriveTypesA("C:\\\\")
它将成功运行。
Please can someone help me find a solution to this problem the program is below. 请有人帮助我找到解决该问题的方法,程序如下。 Thank you :)
谢谢 :)
#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;
}
}
}
Because GetDriveTypeA()
takes a C string , not a C++ std::string
. 因为
GetDriveTypeA()
接受C字符串 ,而不是C ++ std::string
。
UINT GetDriveTypeA(
LPCSTR lpRootPathName
);
Use the std::string.c_str()
method: 使用
std::string.c_str()
方法:
driveType = GetDriveTypeA(path.c_str());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.