CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jun 2005
    Location
    Tirunelveli-Tamil Nadu-India
    Posts
    354

    Smile Convert TCHAR[][] to char*?

    Hi folks

    I want to convert TCHAR[][] to char* and TCHAR[][][] to char*. help me.
    If I Helped You, "Rate This Post"

    Thanks
    Guna

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Convert TCHAR[][] to char*?

    That do you mean? char[][] and char* are not the same, less char[][][] and char*.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Aug 2005
    Location
    southampton, UK
    Posts
    1,320

    Re: Convert TCHAR[][] to char*?

    Code:
    TCHAR tc[2][2];
    char* pChar;
    pChar = (char*)&tc;

  4. #4
    Join Date
    Jun 2005
    Location
    Tirunelveli-Tamil Nadu-India
    Posts
    354

    Re: Convert TCHAR[][] to char*?

    Quote Originally Posted by cilu
    That do you mean? char[][] and char* are not the same, less char[][][] and char*.
    sorry cilu i didn't get u.
    If I Helped You, "Rate This Post"

    Thanks
    Guna

  5. #5
    Join Date
    Jun 2005
    Location
    Tirunelveli-Tamil Nadu-India
    Posts
    354

    Re: Convert TCHAR[][] to char*?

    Quote Originally Posted by dave2k
    Code:
    TCHAR tc[2][2];
    char* pChar;
    pChar = (char*)&tc;
    i try this code but it onlt take first char only.
    If I Helped You, "Rate This Post"

    Thanks
    Guna

  6. #6
    Join Date
    Aug 2005
    Location
    pune
    Posts
    72

    Thumbs up Re: Convert TCHAR[][] to char*?

    Hi

    I have solution for this problem ,

    VOID ChartoTChar(TCHAR *CBuff,CHAR *Char)
    {
    while((*CBuff++ = (TCHAR) *Char++) != '\0');
    }

    void WcharToString(char * str,TCHAR* pTchar)
    {

    while( (*str++ = (char)*pTchar++) != '\0') ;
    }


    It will usefull for u.

  7. #7
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Convert TCHAR[][] to char*?

    Quote Originally Posted by luckykiran
    Hi

    I have solution for this problem ,

    VOID ChartoTChar(TCHAR *CBuff,CHAR *Char)
    {
    while((*CBuff++ = (TCHAR) *Char++) != '\0');
    }

    void WcharToString(char * str,TCHAR* pTchar)
    {

    while( (*str++ = (char)*pTchar++) != '\0') ;
    }


    It will usefull for u.
    And what does this have to do with the question?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  8. #8
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Convert TCHAR[][] to char*?

    Quote Originally Posted by Gunaamirthavelu
    sorry cilu i didn't get u.
    Well, I didn't get you either. Perhaps you should explain what are you actually trying to do. You want to convert and array or arrays of chars to a pointer to char and an array of arrays of arrays of char to pointer to char. What do you want to do?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  9. #9
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Convert TCHAR[][] to char*?

    Quote Originally Posted by Gunaamirthavelu
    I want to convert TCHAR[][] to char* and TCHAR[][][] to char*. help me.
    Why do you feel the need to perform this conversion?

    Please post the code where (you think) such conversions are necessary.
    We here might have another opinion...

  10. #10
    Join Date
    Jun 2005
    Location
    Tirunelveli-Tamil Nadu-India
    Posts
    354

    Smile Re: Convert TCHAR[][] to char*?

    Code:
    #define _UNICODE    // Enables Unicode character handling for log
    #define UNICODE     // files using Unicode instead of ANSI encoding.
    
    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>  // Includes generic text handling functions.
    
    #define MAX_LINE		1026
    #define MAX_FIELDS		256
    #define MAX_LOGLINES	1000
    #define MAX_FIELDLENGTH	1000
    // Declare variables.
    FILE    *pLogFile;
    FILE    *pLogFile1;
    TCHAR   ptcLogLine[MAX_LINE];
    //char   ptcLogLine[MAX_LINE];
    TCHAR   ptcFieldNames[MAX_FIELDS][MAX_FIELDLENGTH];
    TCHAR   ptcLogValues[MAX_LOGLINES][MAX_FIELDS][MAX_FIELDLENGTH];
    TCHAR   *ptcToken;
    UINT    uiIndex;
    UINT    uiInput;
    UINT    uiLineCount = 0;
    UINT    uiFieldCount = 0;
    
    void main()
    {
    	// Open the log file for parsing.
    	pLogFile = _tfopen(TEXT("c:\\WMS_20060823.log"), TEXT("r"));
    	pLogFile1 = _tfopen(TEXT("c:\\write.txt"), TEXT("w+"));
    	// Read a line at a time from the log file and process it.
    	while(_fgetts(ptcLogLine, MAX_LINE, pLogFile) != NULL)
    	{
    		// Remove the trailing newline character from the log line.
    		ptcLogLine[_tcsclen(ptcLogLine)-1] = '\0';
    
    		// If the field names have been processed,
    		// then this line can be read in as log data.
    		if(uiFieldCount > 0)
    		{
    			uiIndex = 0;
    			uiLineCount++;
    			ptcToken = _tcstok(ptcLogLine, TEXT(" "));
    
    			// Store the log data from each field.
    			while(ptcToken != NULL)
    			{
    				uiIndex++;
    				_tcsncpy(ptcLogValues[uiLineCount - 1][uiIndex - 1],
    						 ptcToken, MAX_FIELDLENGTH);
    				ptcLogValues[uiLineCount - 1][uiIndex - 1]
    										 [MAX_FIELDLENGTH-1] = '\0';
    				ptcToken = _tcstok(NULL, TEXT(" "));
    			}
    		}
    
    		// Process all the field names in the log.
    		if(_tcsstr(ptcLogLine, TEXT("#Fields:")) != NULL)
    		{
    			ptcToken = _tcstok(ptcLogLine, TEXT(" "));
    			ptcToken = _tcstok(NULL, TEXT(" "));
    
    			// Store each field name.
    			while(ptcToken != NULL)
    			{
    				uiFieldCount++;
    				_tcsncpy(ptcFieldNames[uiFieldCount - 1], ptcToken,
    						 MAX_FIELDLENGTH);
    				ptcFieldNames[uiFieldCount - 1][MAX_FIELDLENGTH-1] = '\0';
    				char *temp1;
    				TCHAR *temp2;
    				temp1=(char*)&ptcFieldNames;//[0][0];
    				fprintf(pLogFile1,temp1);
    				fprintf(pLogFile1,"\n");
    				temp1="";
    				ptcToken = _tcstok(NULL, TEXT(" "));
    			}
    		}
    	}
    	// Close the log file.
    	fclose(pLogFile);
    	fclose(pLogFile1);
    	return;
    }
     i want to read file from WMS_20060823.log and write to write.txt file. u can use this code only but u can convert the datatype for writing the file.
    If I Helped You, "Rate This Post"

    Thanks
    Guna

  11. #11
    Join Date
    Oct 2010
    Posts
    1

    Re: Convert TCHAR[][] to char*?

    Yes, it is real pain in the back, especially when you adopting old code with long main function.
    The simplest way is to allocate new space for strings and copy characters skipping each second character. Here is the CPP code that duplicates _TCHAR * argv[] to char * argn[].

    http://www.wincli.com/?p=72

    If you adopting old code to Windows, simple use define mentioned in the code as optional.

    int _tmain(int argc, _TCHAR* argv[])
    {
    char ** argn = allocate_argn(argc, argv);
    #define argv argn
    // Here is your old code working with arguments
    if (argc>1) {
    printf(“Arg 1 = ‘%s’\n”, argv[1]); // Just argn intead of argv
    }
    #undefine argv
    release_argn(argc, argn);
    return 0;
    }

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Convert TCHAR[][] to char*?

    Quote Originally Posted by trasser View Post
    Yes, it is real pain in the back, especially when you adopting old code with long main function....
    Why do you revive this more than four years old thread?
    Only to post a piece of wrong code to it?
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured