gpt4 book ai didi

c++ - 为什么我不能将字符串传递给函数 GetDriveTypesA()?

转载 作者:行者123 更新时间:2023-11-27 22:35:20 24 4
gpt4 key购买 nike

所以该程序的目的是找出可用的驱动器以及它们是什么。但是,我似乎无法将字符串变量直接传递给 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;
}
}

}

最佳答案

因为 GetDriveTypeA()需要 C string ,不是 C++ std::string

UINT GetDriveTypeA(
LPCSTR lpRootPathName
);

使用 std::string.c_str()方法:

driveType = GetDriveTypeA(path.c_str());

关于c++ - 为什么我不能将字符串传递给函数 GetDriveTypesA()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55152080/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com