I have a program which needs to take the logical drives returned from the GetLogicalDriveString function and store them in a string array for later use in a filepath. I tried doing this directly and it failed. Here's my code:

#include "stdafx.h"
#include <iostream>
#include <io.h>
#include <time.h>
#include <string>
#include <windows.h>
#include <direct.h>
#include <stdio.h>
#include <tchar.h>
#include <Lmcons.h>
#include <sstream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
CHAR name [ UNLEN + 1 ];
  DWORD size = UNLEN + 1;

  if (GetUserName( (CHAR*)name, &size ))
    wcout << L"Hello, " << name << L"!\n";
  else
    cout << "Hello, unnamed person!\n";

	TCHAR szBuffer[1024];
::GetLogicalDriveStrings(1024, szBuffer);
TCHAR *pch = szBuffer;
while (*pch) {
_tprintf(TEXT("%s\n"), pch);
pch = &pch[_tcslen(pch) + 1];
}
   int arraylength = sizeof( pch );
   cout << arraylength << endl;
   string drive[1025];

int i;
i = 0;

if (i < arraylength)
{
	i++;
	drive[i] = pch[i];
	cout << drive[i] << endl;
}
	return 0;
}

Recommended Answers

All 3 Replies

36-37 Are you forgetting something?

36-37 Are you forgetting something?

I don't think so

Like a for() loop, because it just boils down to drive[1] = pch[1]; Mentioned in your CodeGuru thread, mixing wides with ASCII can lead you into problems from messy code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.