0

I want to write the C++ code that can help me to give the drive letter which contains the given folder. I am writing the given code and getting the error while adding the character variable to a string variable at line 11. Can anyone help me out in rectifying the below code.

#include "stdafx.h"
#include <string>
#include <windows.h>
#include <iostream>
#include "Shlwapi.h"
int main()
{
    char var;
    for (var = 'A'; var <= 'Z'; ++var)
    {
        char buffer_1[] = var +":\\PerfLogs";      ------->>>> line where i am getting the error
        char *lpStr1;
        lpStr1 = buffer_1;
        int retval;
        retval = PathFileExists(lpStr1);
        if (retval == 1)
        {
            std :: cout << "Search for the file path of : " << lpStr1;
            system("PAUSE");
        }
    }
}
9
  • 1
    use std::string Commented Jun 18, 2018 at 11:49
  • 4
    retval = PathFileExists((var + std::string(":\\PerfLogs")).c_str());. Alternatively, char buffer[] = "*:\\PerLogs"; buffer[0] = var; retval = PathFileExists(buffer); Commented Jun 18, 2018 at 11:49
  • 3
    Also note that your loop is not portable. Not all systems have character encodings where all letters are contiguous. Commented Jun 18, 2018 at 11:50
  • 1
    @Bathsheba: Let me rephrase it in a different way,then. Drive letters are already not portable. std::file_system has an abstract notion of root names, but their exact syntax is wholly unspecified by C++. The only thing you know is that they're represented as strings, and there's no way you can portably enumerate these strings. Commented Jun 18, 2018 at 12:08
  • 1
    @Bathsheba: #include <windows.h> Commented Jun 18, 2018 at 12:34

3 Answers 3

3

You should use the string library:

std::string str1="Str 1";
std::string str2=" Str 2";
str1.append(str2);      //str1 = "Str 1 Str 2"
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the suggestion, its working fine over there but then I am not able to convert the same to char* type and i need to do that for accessing PathFileExists(lpStr1) function, any idea about that
@PuneetGarg PathFileExists(your_string.c_str())
2

The specific compiler error you get is due to your attempting to add a const char* type (as a result of a string literal decayed to a pointer type) to a char. Let's not worry too much about that; rather, let's put the C++ standard library to good use:

A portable solution would be as follows:

#include <iostream>
#include <string>
// ToDo - include the header for PathFileExists
using namespace std::string_literals; // Bring in the std::string user defined literal.

int main() {
    for (auto c : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"s){ // Note the user defined literal.
        std::string path = c + ":\\PerfLogs"s; // And again. This calls an overloaded `+`.
        int retval = PathFileExists(path.c_str()); // Pass the char buffer.
        if (retval == 1){
            std::cout << "Search for the file path of : " << path;
            system("PAUSE");
        }
    }
}

5 Comments

You're missing using namespace std::string_literals; and PathFileExists is declared in Shlwapi.h
@O'Neil: On your first point, doesn't the string header bring that in? On the second point, no matter as there's nothing in the question to really point out which file system library they are using.
1) No it doesn't. 2) Except the #include of that file.
@Bathsheba thanks, It worked, and yes there is the need to use the namespace to define user define literals... thanks a lot everyone for your help.
@O'Neil: Checked the first point, and yes, as you knew all along, you're correct.
0

You can use std::string for this as others have suggested. But since it's just 1 character it wouldn't be too hard to do this:

const char buffer_1[] = { var, ':', '\\', 'P', 'e', 'r', 'f', 'L', 'o', 'g', 's', '\0' };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.