By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,512 Members | 1,769 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,512 IT Pros & Developers. It's quick & easy.

Difference between widechartomultibyte and wcstombs

P: 2
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:
Expand|Select|Wrap|Line Numbers
  1. #define _UNICODE
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <sql.h>
  7. #include <sqlext.h>
  8. #include <sqlucode.h>
  9. //#include <odbcss.h>
  10. #include<iostream.h>
  11.  
  12. #include<fstream.h>
  13.  
  14. void main()
  15. {    
  16.     void WriteXMLFile (int *,char *);
  17.  
  18.     SQLHENV            hEnv=NULL;
  19.     SQLHDBC            hDBC=NULL;
  20.     SQLHSTMT    hStmt = NULL;// Statement handle
  21.     SWORD        retcode;    
  22.     WCHAR wszDSN[] = L"myDataSource";
  23.     WCHAR wszUID[] = L"sa";
  24.     WCHAR wszPWD[] = L"bmcAdm1n";
  25.     WCHAR wszSqlState[SQL_SQLSTATE_SIZEW] = L"";
  26.     WCHAR wszMsg[200] = L"";
  27.     WCHAR        name[55];
  28.     //wchar_t        name[55]= {L'\0'};
  29.     SDWORD      cbname;
  30.  
  31.     WCHAR        szSqlStr[150]= L"select name  from sys.sql_logins" ;
  32.     char strBuffer[255];    
  33.     int flag =0,count=1;            
  34.  
  35.  
  36.     retcode=  SQLAllocEnv (&hEnv);
  37.     cout<<"retcode= "<<retcode<<endl;
  38.     cout<<"hEnv after SQLAllocEnv = "<<hEnv<<endl;
  39.     cout<<"\n***********************\n\n";                
  40.  
  41.     // Allocate memory for the connection handle
  42.     retcode= SQLAllocConnect (hEnv, &hDBC);
  43.     cout<<"retcode= "<<retcode<<endl;
  44.     cout<<"hDBC after SQLAllocConnect = "<<hDBC<<endl;
  45.     cout<<"\n***********************\n\n";    
  46.  
  47.     SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION,(SQLPOINTER) SQL_OV_ODBC3,SQL_IS_UINTEGER);
  48.     //    SQLSetEnvAttr(hEnv, SQL_ATTR_DRIVER_UNICODE_TYPE ,SQL_CP_UTF8 ,SQL_NTS);
  49.  
  50.     retcode = SQLConnectW(hDBC, wszDSN, SQL_NTS,wszUID, SQL_NTS, wszPWD, SQL_NTS);
  51.     if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
  52.     {    
  53.         printf("\n------------Connected---------------\n");        
  54.  
  55.         // Allocate memory for the statement handle
  56.         retcode = SQLAllocStmt (hDBC, &hStmt);
  57.         cout<<"hStmt after SQLAllocStmt = "<<hStmt<<endl;
  58.         cout<<"\n***********************\n\n";
  59.  
  60.         retcode = SQLExecDirectW(hStmt,szSqlStr, SQL_NTS);
  61.         if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
  62.         {
  63.             retcode = SQLBindCol(hStmt, 1, SQL_C_WCHAR, name, 55, &cbname);
  64.             //retcode = SQLBindCol(hStmt, 1, SQL_C_CHAR, name, 55, &cbname);
  65.         }
  66.         cout <<"\n****************************************\n";
  67.  
  68.         while (TRUE)
  69.         {
  70.             memset(name, 0, sizeof(name));    
  71.             retcode = SQLFetch(hStmt);
  72.             if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
  73.             {
  74.                 if (cbname == SQL_NULL_DATA)               /* check null data */
  75.                     printf("\nname: NULL\n");
  76.                 else
  77.                 {
  78.                     //printf("\nName: %s\n", name);
  79.                     //wprintf(L"\n1Name: %S", name);            
  80.                     memset (strBuffer, 0,sizeof(strBuffer));
  81.                     WideCharToMultiByte(CP_UTF8, 0, name,-1, strBuffer, 255, NULL, NULL);                    
  82.  
  83.                     //wcstombs(strBuffer,name,sizeof(name));
  84.                     printf("\nBuffer: %s\n", strBuffer);    
  85.                         WriteXMLFile(&flag,strBuffer);
  86.                     //    WriteXMLFile(&flag,(char*)name);
  87.                 }
  88.             }                                    
  89.             else /* if no more data or errors returned */
  90.             {
  91.                 flag =2;
  92.                 WriteXMLFile(&flag,strBuffer);
  93.                 break;  
  94.             }
  95.         }                
  96.         // Free the allocated statement handle
  97.         SQLFreeStmt (hStmt, SQL_DROP);
  98.  
  99.         // Disconnect from datasource
  100.         SQLDisconnect (hDBC);
  101.     }     
  102.     // Free the allocated connection handle
  103.     SQLFreeConnect (hDBC);
  104.  
  105.     // Free the allocated ODBC environment handle
  106.     SQLFreeEnv (hEnv);           
  107. }
  108. void WriteXMLFile (int *flag, char * buffer)
  109. {
  110.     ofstream myfile;
  111.     char * buffer2;
  112.     myfile.open ("output.xml", ios::app);
  113.  
  114.     if (*flag==2)
  115.     {
  116.         buffer2="</string>";
  117.         myfile<<buffer2;    
  118.         myfile.close();
  119.     }
  120.  
  121.     if (*flag ==0)
  122.     {
  123.         buffer2 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
  124.         myfile<<"\n";
  125.         myfile<<buffer2;
  126.         buffer2="<string>";
  127.         myfile<<buffer2;
  128.         myfile<<"\n";
  129.         *flag = 1;
  130.     }
  131.     myfile<<buffer;
  132.     myfile<<"\n";
  133. }
May 25 '07 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
wcstombs converts whar_t values to multibyte values but I don't believe it uses Unicode UTF-16.

Your Unicode results should be valid using WideCharToMultiByte and MultiByteToWideChar.

Also, wcstombs is deprecated in favor of wcstombs_s.

Lastly, I see a char array in your code. You should be using char anything in a Unicode function.
May 25 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.