为啥我不能将字符串传递给函数 GetDriveTypesA()?
Posted
技术标签:
【中文标题】为啥我不能将字符串传递给函数 GetDriveTypesA()?【英文标题】:Why can't I pass a string into the function GetDriveTypesA()?为什么我不能将字符串传递给函数 GetDriveTypesA()? 【发布时间】:2019-03-13 22:25:13 【问题描述】:因此,该计划的目的是找出可用的驱动器以及它们是什么。但是,我似乎无法将字符串变量直接传递给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()。 【参考方案1】:因为GetDriveTypeA()
采用C string,而不是C++ std::string
。
UINT GetDriveTypeA(
LPCSTR lpRootPathName
);
使用std::string.c_str()
方法:
driveType = GetDriveTypeA(path.c_str());
【讨论】:
以上是关于为啥我不能将字符串传递给函数 GetDriveTypesA()?的主要内容,如果未能解决你的问题,请参考以下文章
为啥我不能像传递其他变量一样将函数从 Express.js 传递给 EJS?
为啥我不能将 lambda 传递给这个需要 std::function 的函数? [复制]