How the drive letter in a string

Hi,
I need to copy the list of drives present in the computer to an array e.g
string drives[i] ;
Where i is the list of drives How can I do it?

What I did until now is I have referred Google and found that GetlogicaldriveStrings but it is quite difficult to copy it into my string I know strcpy will not help here but How can I do it? Kindly help me with this
Thank you
You need to use a char pointer to parse zero terminated sub strings.

#include <iostream>
#include <string>
#include <string.h>
#include <Windows.h>

using namespace std;

string GetDriveNames ()
{
char buffer[256] = { 0 };

::GetLogicalDriveStringsA (255, buffer);
char *cp = buffer;

string retval;

while (*cp)
{
retval += cp;
cp += strlen (cp)+1;
}
return retval;
}

int main ()
{
cout << GetDriveNames () << "\n\n";
system ("pause");
return 0;
}
Hi Thank you for your answer.But how can I use it because the output is like this C :\D:\E:\ If I want to use the C drive for some iteration how can I use this? could you please teach me to do this using an string array so that I use it. It is quiet difficult to figure out your solution because I don't know why you have used 256 as a buffer value could you please explain this too

Thank you once again
Well the bufsize of 256 of just to be on the save side. You can have max 28 drives and each would be 4 chars so you can use a smaller one if you want.

What do you want to do with the drive names? Would a vector<string> more useful - like:
void GetDriveNames (vector<string> &drives)
Last edited on
I want to do some iteration with the directories so to add further locations (more specifically paths in my computer) To my work string is good since exact drive is unknown my loop will recursively add things to each drive... How can I do this I referred Google and found that there is no standard way of doing this since C ++ lacks its concept in directories the best way is to use boost library
Yes I installed boost on my Visual studio 2015 but boost is quiet like a new language to be learnt
Could you please teach me to get the drives in an array like this

This may look that I have posted in the wrong forum but I am unsure that GetLogicalDriveStrings is related to Windows so I posted here. Sorry if I am wrong

string drive[i]; i is an integer value so that I can increment the value and do my iteration manually.

Thank you for all your help to me
Topic archived. No new replies allowed.