简体   繁体   English

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

[英]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.

相关问题 为什么我不能在这个专门的 function 模板中将字符串文字传递给 const char* const& - Why I can't pass string literals to const char* const& in this specialized function template 为什么不能传递string *而不是string []作为参数? - Why can't I pass string* instead of string[] as argument? 为什么我不能将this指针显式传递给成员函数? - Why can't I pass the this pointer explicitly to a member function? 为什么我不能通过值将rvalue std :: stringstream传递给函数? - Why can't I pass an rvalue std::stringstream by value to a function? 为什么我不能将字符串文字传递给这个模板? - Why I can't pass string literal to this template? 为什么我不能从字符串函数返回字符串数组? - Why can't I return the string array from the string function? 为什么不能将绑定的成员函数作为可调用函数传递? - Why can't I pass a bound member function as a callable? 为什么我不能将模板函数指针传递给varargs函数? - Why can't I pass template function pointers to varargs functions? 为什么我不能在 function 声明中将全局变量作为参数传递 - Why can't I pass global Variables as parameters in function declaration 为什么我不能将函数指针作为模板参数传递给地图? - Why can't I pass a function pointer as a template parameter to a map?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM