Thread: Converting tchar array to string array

  1. #1
    Registered User
    Join Date
    Jan 2011

    Exclamation Converting tchar array to string array

    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";
        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)
    	drive[i] = pch[i];
    	cout << drive[i] << endl;
    	return 0;

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    int arraylength = sizeof( pch );
    sizeof returns the size of a data type. Here, pch is a pointer, so it returns the size of a pointer (4 bytes on a 32-bit machine). Judging by the name of the variable this is not what you wanted.

    string drive[1025];
    This is declaring an array of 1025 strings. I think perhaps what you were going for was a string with 1025 characters reserved? C++ string memory is automatically handled so there's no need for it, just declare one string, though if it makes you feel better you can use drive.reserve(1025);
    Consider this post signed

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    More cross-posting

    Didn't learn from your previous thread huh?

    Here, try this on for size
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Converting to expression to string of array elements
    By Sailors in forum C Programming
    Replies: 12
    Last Post: 07-26-2007, 03:01 PM
  4. error converting string to char array
    By COBOL2C++ in forum C++ Programming
    Replies: 6
    Last Post: 07-11-2003, 10:59 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM