what is the Difference between widechartomultibyte and wcstombs

Joined
May 25, 2007
Messages
1
Reaction score
0
Hi All
I have russian characters in SQL database. I am fetching data using C++ code and storing it in xml file.
I get right outout with widechartomultibyte.
But it's not able to fetch unicode data when I use wcstombs instead of widechartomultibyte function.
Why is it so?
Can anybody please help?

following is my code:


#define _UNICODE
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sql.h>
#include <sqlext.h>
#include <sqlucode.h>
//#include <odbcss.h>
#include<iostream.h>

#include<fstream.h>

void main()
{
void WriteXMLFile (int *,char *);

SQLHENV hEnv=NULL;
SQLHDBC hDBC=NULL;
SQLHSTMT hStmt = NULL;// Statement handle
SWORD retcode;
WCHAR wszDSN[] = L"myDataSource";
WCHAR wszUID[] = L"sa";
WCHAR wszPWD[] = L"bmcAdm1n";
WCHAR wszSqlState[SQL_SQLSTATE_SIZEW] = L"";
WCHAR wszMsg[200] = L"";
WCHAR name[55];
//wchar_t name[55]= {L'\0'};
SDWORD cbname;

WCHAR szSqlStr[150]= L"select name from sys.sql_logins" ;
char strBuffer[255];
int flag =0,count=1;


retcode= SQLAllocEnv (&hEnv);
cout<<"retcode= "<<retcode<<endl;
cout<<"hEnv after SQLAllocEnv = "<<hEnv<<endl;
cout<<"\n***********************\n\n";

// Allocate memory for the connection handle
retcode= SQLAllocConnect (hEnv, &hDBC);
cout<<"retcode= "<<retcode<<endl;
cout<<"hDBC after SQLAllocConnect = "<<hDBC<<endl;
cout<<"\n***********************\n\n";

SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION,(SQLPOINTER) SQL_OV_ODBC3,SQL_IS_UINTEGER);
// SQLSetEnvAttr(hEnv, SQL_ATTR_DRIVER_UNICODE_TYPE ,SQL_CP_UTF8 ,SQL_NTS);

retcode = SQLConnectW(hDBC, wszDSN, SQL_NTS,wszUID, SQL_NTS, wszPWD, SQL_NTS);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
printf("\n------------Connected---------------\n");

// Allocate memory for the statement handle
retcode = SQLAllocStmt (hDBC, &hStmt);
cout<<"hStmt after SQLAllocStmt = "<<hStmt<<endl;
cout<<"\n***********************\n\n";

retcode = SQLExecDirectW(hStmt,szSqlStr, SQL_NTS);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
retcode = SQLBindCol(hStmt, 1, SQL_C_WCHAR, name, 55, &cbname);
//retcode = SQLBindCol(hStmt, 1, SQL_C_CHAR, name, 55, &cbname);
}
cout <<"\n****************************************\n";

while (TRUE)
{
memset(name, 0, sizeof(name));
retcode = SQLFetch(hStmt);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
if (cbname == SQL_NULL_DATA) /* check null data */
printf("\nname: NULL\n");
else
{
//printf("\nName: %s\n", name);
//wprintf(L"\n1Name: %S", name);
memset (strBuffer, 0,sizeof(strBuffer));
WideCharToMultiByte(CP_UTF8, 0, name,-1, strBuffer, 255, NULL, NULL);

//wcstombs(strBuffer,name,sizeof(name));
printf("\nBuffer: %s\n", strBuffer);
WriteXMLFile(&flag,strBuffer);
// WriteXMLFile(&flag,(char*)name);
}
}
else /* if no more data or errors returned */
{
flag =2;
WriteXMLFile(&flag,strBuffer);
break;
}
}
// Free the allocated statement handle
SQLFreeStmt (hStmt, SQL_DROP);

// Disconnect from datasource
SQLDisconnect (hDBC);
}
// Free the allocated connection handle
SQLFreeConnect (hDBC);

// Free the allocated ODBC environment handle
SQLFreeEnv (hEnv);
}
void WriteXMLFile (int *flag, char * buffer)
{
ofstream myfile;
char * buffer2;
myfile.open ("output.xml", ios::app);

if (*flag==2)
{
buffer2="</string>";
myfile<<buffer2;
myfile.close();
}

if (*flag ==0)
{
buffer2 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
myfile<<"\n";
myfile<<buffer2;
buffer2="<string>";
myfile<<buffer2;
myfile<<"\n";
*flag = 1;
}
myfile<<buffer;
myfile<<"\n";
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top