Hi folks
I want to convert TCHAR[][] to char* and TCHAR[][][] to char*. help me.
Printable View
Hi folks
I want to convert TCHAR[][] to char* and TCHAR[][][] to char*. help me.
That do you mean? char[][] and char* are not the same, less char[][][] and char*.
Code:TCHAR tc[2][2];
char* pChar;
pChar = (char*)&tc;
sorry cilu i didn't get u.Quote:
Originally Posted by cilu
i try this code but it onlt take first char only.Quote:
Originally Posted by dave2k
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?Quote:
Originally Posted by luckykiran
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?Quote:
Originally Posted by Gunaamirthavelu
Why do you feel the need to perform this conversion?Quote:
Originally Posted by Gunaamirthavelu
Please post the code where (you think) such conversions are necessary.
We here might have another opinion... ;)
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.
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;
}