c++字符串类型转换相关总结
===========************************************************************************==============
CString szTipText;
pTTT->lpszText =(LPTSTR) (LPCTSTR) szTipText;
long CChappyView::AddTab(CString & szTitle_p)
{
m_iLastTab++;//insert a new tab
TC_ITEM tcItem;
tcItem.mask = TCIF_TEXT;
char szTabTitle[256];
sprintf(szTabTitle, " %s ",(LPCTSTR) szTitle_p);//e.g." Rooms "
tcItem.pszText = szTabTitle;
int ret = m_ctrlTabWnd.InsertItem(m_iLastTab ,&tcItem);
ASSERT (m_iLastTab==ret);
return m_iLastTab;
}
--------------------------------------------------------------------------------------------------
// example for CString::CString
CString s1; // Empty string
CString s2( "cat" ); // From a C string literal
CString s3 = s2; // Copy constructor
CString s4( s2 + " " + s3 ); // From a string expression
CString s5( 'x' ); // s5 = "x"
CString s6( 'x', 6 ); // s6 = "xxxxxx"
CString s7((LPCTSTR)ID_FILE_NEW); // s7 = "Create a new document"
CString city = "Philadelphia"; // NOT the assignment operator
-------------------------------------------------------------------------------------------------
// crt_itoa_s.c
#include <stdlib.h>
int main( void )
{
char buffer[65];
int r;
for( r=10; r>=2; --r )
{
_itoa_s( -1, buffer, 65, r );
printf( "base %d: %s (%d chars)\n", r, buffer, strlen(buffer) );
}
printf( "\n" );
for( r=10; r>=2; --r )
{
_i64toa_s( -1L, buffer, 65, r );
printf( "base %d: %s (%d chars)\n", r, buffer, strlen(buffer) );
}
printf( "\n" );
for( r=10; r>=2; --r )
{
_ui64toa_s( 0xffffffffffffffffL, buffer, 65, r );
printf( "base %d: %s (%d chars)\n", r, buffer, strlen(buffer) );
}
}
base 10: -1 (2 chars)
base 9: 12068657453 (11 chars)
base 8: 37777777777 (11 chars)
base 7: 211301422353 (12 chars)
base 6: 1550104015503 (13 chars)
base 5: 32244002423140 (14 chars)
base 4: 3333333333333333 (16 chars)
base 3: 102002022201221111210 (21 chars)
base 2: 11111111111111111111111111111111 (32 chars)
base 10: -1 (2 chars)
base 9: 145808576354216723756 (21 chars)
base 8: 1777777777777777777777 (22 chars)
base 7: 45012021522523134134601 (23 chars)
base 6: 3520522010102100444244423 (25 chars)
base 5: 2214220303114400424121122430 (28 chars)
base 4: 33333333333333333333333333333333 (32 chars)
base 3: 11112220022122120101211020120210210211220 (41 chars)
base 2: 1111111111111111111111111111111111111111111111111111111111111111 (64 chars)
base 10: 18446744073709551615 (20 chars)
base 9: 145808576354216723756 (21 chars)
base 8: 1777777777777777777777 (22 chars)
base 7: 45012021522523134134601 (23 chars)
base 6: 3520522010102100444244423 (25 chars)
base 5: 2214220303114400424121122430 (28 chars)
base 4: 33333333333333333333333333333333 (32 chars)
base 3: 11112220022122120101211020120210210211220 (41 chars)
base 2: 1111111111111111111111111111111111111111111111111111111111111111 (64 chars)
-----------------------------------------------------------------------------------
// crt_itoa.c
#include <stdlib.h>
int main( void )
{
char buffer[65];
int r;
for( r=10; r>=2; --r )
{
_itoa( -1, buffer, r );
printf( "base %d: %s (%d chars)\n", r, buffer, strlen(buffer) );
}
printf( "\n" );
for( r=10; r>=2; --r )
{
_i64toa( -1L, buffer, r );
printf( "base %d: %s (%d chars)\n", r, buffer, strlen(buffer) );
}
printf( "\n" );
for( r=10; r>=2; --r )
{
_ui64toa( 0xffffffffffffffffL, buffer, r );
printf( "base %d: %s (%d chars)\n", r, buffer, strlen(buffer) );
}
}
-----------------------------------------------------------------------------------
Convert from string to wstring:
std::wstring stows(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
----------
Convert from wstring to string:
std::string wstos(const std::wstring& s)
{
int len;
int slength = (int)s.length() + 1;
len = WideCharToMultiByte(CP_ACP, 0, s.c_str(), slength, 0, 0, 0, 0);
char* buf = new char[len];
WideCharToMultiByte(CP_ACP, 0, s.c_str(), slength, buf, len, 0, 0);
std::string r(buf);
delete[] buf;
return r;
}
-----------
std::string s;
#ifdef UNICODE
std::wstring stemp = stows(s); // Temporary buffer is required
LPCWSTR result = stemp.c_str();
#else
LPCWSTR result = s.c_str();
#endif
---------------------------------------------------------------------------------------------------------------------------
This topic demonstrates how to convert various Visual C++ string types into other strings. The strings types that are covered include char *, wchar_t*, _bstr_t, CComBSTR, CString, basic_string, and System.String. In all cases, a copy of the string is made when converted to the new type. Any changes made to the new string will not affect the original string, and vice versa.
Converting from char *
Example
This example demonstrates how to convert from a char * to the other string types listed above.
Copy Code
// convert_from_char.cpp
// compile with: /clr /link comsuppw.lib
#include <iostream>
#include <stdlib.h>
#include <string>
#include "atlbase.h"
#include "atlstr.h"
#include "comutil.h"
using namespace std;
using namespace System;
int main()
{
char *orig = "Hello, World!";
cout << orig << " (char *)" << endl;
// Convert to a wchar_t*
size_t origsize = strlen(orig) + 1;
const size_t newsize = 100;
size_t convertedChars = 0;
wchar_t wcstring[newsize];
mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE);
wcscat_s(wcstring, L" (wchar_t *)");
wcout << wcstring << endl;
// Convert to a _bstr_t
_bstr_t bstrt(orig);
bstrt += " (_bstr_t)";
cout << bstrt << endl;
// Convert to a CComBSTR
CComBSTR ccombstr(orig);
if (ccombstr.Append(L" (CComBSTR)") == S_OK)
{
CW2A printstr(ccombstr);
cout << printstr << endl;
}
// Convert to a CString
CString cstring(orig);
cstring += " (CString)";
cout << cstring << endl;
// Convert to a basic_string
string basicstring(orig);
basicstring += " (basic_string)";
cout << basicstring << endl;
// Convert to a System::String
String ^systemstring = gcnew String(orig);
systemstring += " (System::String)";
Console::WriteLine("{0}", systemstring);
delete systemstring;
}
Output
Hello, World! (char *)
Hello, World! (wchar_t *)
Hello, World! (_bstr_t)
Hello, World! (CComBSTR)
Hello, World! (CString)
Hello, World! (basic_string)
Hello, World! (System::String)
------------------------------------------
Converting from wchar_t *
Example
This example demonstrates how to convert from a wchar_t * to the other string types listed above.
Copy Code
// convert_from_wchar_t.cpp
// compile with: /clr /link comsuppw.lib
#include <iostream>
#include <stdlib.h>
#include <string>
#include "atlbase.h"
#include "atlstr.h"
#include "comutil.h"
using namespace std;
using namespace System;
int main()
{
wchar_t *orig = L"Hello, World!";
wcout << orig << L" (wchar_t *)" << endl;
// Convert to a char*
size_t origsize = wcslen(orig) + 1;
const size_t newsize = 100;
size_t convertedChars = 0;
char nstring[newsize];
wcstombs_s(&convertedChars, nstring, origsize, orig, _TRUNCATE);
strcat_s(nstring, " (char *)");
cout << nstring << endl;
// Convert to a _bstr_t
_bstr_t bstrt(orig);
bstrt += " (_bstr_t)";
cout << bstrt << endl;
// Convert to a CComBSTR
CComBSTR ccombstr(orig);
if (ccombstr.Append(L" (CComBSTR)") == S_OK)
{
CW2A printstr(ccombstr);
cout << printstr << endl;
}
// Convert to a CString
CString cstring(orig);
cstring += " (CString)";
cout << cstring << endl;
// Convert to a basic_string
wstring basicstring(orig);
basicstring += L" (basic_string)";
wcout << basicstring << endl;
// Convert to a System::String
String ^systemstring = gcnew String(orig);
systemstring += " (System::String)";
Console::WriteLine("{0}", systemstring);
delete systemstring;
}
Output
Hello, World! (wchar_t *)
Hello, World! (char *)
Hello, World! (_bstr_t)
Hello, World! (CComBSTR)
Hello, World! (CString)
Hello, World! (basic_string)
Hello, World! (System::String)
----------------------------------------
Converting from _bstr_t
Example
This example demonstrates how to convert from a _bstr_t to the other string types listed above.
Copy Code
// convert_from_bstr_t.cpp
// compile with: /clr /link comsuppw.lib
#include <iostream>
#include <stdlib.h>
#include <string>
#include "atlbase.h"
#include "atlstr.h"
#include "comutil.h"
using namespace std;
using namespace System;
int main()
{
_bstr_t orig("Hello, World!");
wcout << orig << " (_bstr_t)" << endl;
// Convert to a char*
const size_t newsize = 100;
char nstring[newsize];
strcpy_s(nstring, (char *)orig);
strcat_s(nstring, " (char *)");
cout << nstring << endl;
// Convert to a wchar_t*
wchar_t wcstring[newsize];
wcscpy_s(wcstring, (wchar_t *)orig);
wcscat_s(wcstring, L" (wchar_t *)");
wcout << wcstring << endl;
// Convert to a CComBSTR
CComBSTR ccombstr((char *)orig);
if (ccombstr.Append(L" (CComBSTR)") == S_OK)
{
CW2A printstr(ccombstr);
cout << printstr << endl;
}
// Convert to a CString
CString cstring((char *)orig);
cstring += " (CString)";
cout << cstring << endl;
// Convert to a basic_string
string basicstring((char *)orig);
basicstring += " (basic_string)";
cout << basicstring << endl;
// Convert to a System::String
String ^systemstring = gcnew String((char *)orig);
systemstring += " (System::String)";
Console::WriteLine("{0}", systemstring);
delete systemstring;
}
Output
Hello, World! (_bstr_t)
Hello, World! (char *)
Hello, World! (wchar_t *)
Hello, World! (CComBSTR)
Hello, World! (CString)
Hello, World! (basic_string)
Hello, World! (System::String)
--------------------------------------------------
Converting from CComBSTR
Example
This example demonstrates how to convert from a CComBSTR to the other string types listed above.
Copy Code
// convert_from_ccombstr.cpp
// compile with: /clr /link comsuppw.lib
#include <iostream>
#include <stdlib.h>
#include <string>
#include "atlbase.h"
#include "atlstr.h"
#include "comutil.h"
#include "vcclr.h"
using namespace std;
using namespace System;
using namespace System::Runtime::InteropServices;
int main()
{
CComBSTR orig("Hello, World!");
CW2A printstr(orig);
cout << printstr << " (CComBSTR)" << endl;
// Convert to a char*
const size_t newsize = 100;
char nstring[newsize];
CW2A tmpstr1(orig);
strcpy_s(nstring, tmpstr1);
strcat_s(nstring, " (char *)");
cout << nstring << endl;
// Convert to a wchar_t*
wchar_t wcstring[newsize];
wcscpy_s(wcstring, orig);
wcscat_s(wcstring, L" (wchar_t *)");
wcout << wcstring << endl;
// Convert to a _bstr_t
_bstr_t bstrt(orig);
bstrt += " (_bstr_t)";
cout << bstrt << endl;
// Convert to a CString
CString cstring(orig);
cstring += " (CString)";
cout << cstring << endl;
// Convert to a basic_string
wstring basicstring(orig);
basicstring += L" (basic_string)";
wcout << basicstring << endl;
// Convert to a System::String
String ^systemstring = gcnew String(orig);
systemstring += " (System::String)";
Console::WriteLine("{0}", systemstring);
delete systemstring;
}
Output
Hello, World! (CComBSTR)
Hello, World! (char *)
Hello, World! (wchar_t *)
Hello, World! (_bstr_t)
Hello, World! (CString)
Hello, World! (basic_string)
Hello, World! (System::String)
--------------------------------------
Converting from CString
Example
This example demonstrates how to convert from a CString to the other string types listed above.
Copy Code
// convert_from_cstring.cpp
// compile with: /clr /link comsuppw.lib
#include <iostream>
#include <stdlib.h>
#include <string>
#include "atlbase.h"
#include "atlstr.h"
#include "comutil.h"
using namespace std;
using namespace System;
int main()
{
CString orig("Hello, World!");
wcout << orig << " (CString)" << endl;
// Convert to a char*
const size_t newsize = 100;
char nstring[newsize];
strcpy_s(nstring, orig);
strcat_s(nstring, " (char *)");
cout << nstring << endl;
// Convert to a wchar_t*
// You must first convert to a char * for this to work.
size_t origsize = strlen(orig) + 1;
size_t convertedChars = 0;
wchar_t wcstring[newsize];
mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE);
wcscat_s(wcstring, L" (wchar_t *)");
wcout << wcstring << endl;
// Convert to a _bstr_t
_bstr_t bstrt(orig);
bstrt += " (_bstr_t)";
cout << bstrt << endl;
// Convert to a CComBSTR
CComBSTR ccombstr(orig);
if (ccombstr.Append(L" (CComBSTR)") == S_OK)
{
CW2A printstr(ccombstr);
cout << printstr << endl;
}
// Convert to a basic_string
string basicstring(orig);
basicstring += " (basic_string)";
cout << basicstring << endl;
// Convert to a System::String
String ^systemstring = gcnew String(orig);
systemstring += " (System::String)";
Console::WriteLine("{0}", systemstring);
delete systemstring;
}
Output
Hello, World! (CString)
Hello, World! (char *)
Hello, World! (wchar_t *)
Hello, World! (_bstr_t)
Hello, World! (CComBSTR)
Hello, World! (basic_string)
Hello, World! (System::String)
--------------------------------------------
Converting from basic_string
Example
This example demonstrates how to convert from a basic_string to the other string types listed above.
Copy Code
// convert_from_basic_string.cpp
// compile with: /clr /link comsuppw.lib
#include <iostream>
#include <stdlib.h>
#include <string>
#include "atlbase.h"
#include "atlstr.h"
#include "comutil.h"
using namespace std;
using namespace System;
int main()
{
string orig("Hello, World!");
cout << orig << " (basic_string)" << endl;
// Convert to a char*
const size_t newsize = 100;
char nstring[newsize];
strcpy_s(nstring, orig.c_str());
strcat_s(nstring, " (char *)");
cout << nstring << endl;
// Convert to a wchar_t*
// You must first convert to a char * for this to work.
size_t origsize = strlen(orig.c_str()) + 1;
size_t convertedChars = 0;
wchar_t wcstring[newsize];
mbstowcs_s(&convertedChars, wcstring, origsize, orig.c_str(), _TRUNCATE);
wcscat_s(wcstring, L" (wchar_t *)");
wcout << wcstring << endl;
// Convert to a _bstr_t
_bstr_t bstrt(orig.c_str());
bstrt += " (_bstr_t)";
cout << bstrt << endl;
// Convert to a CComBSTR
CComBSTR ccombstr(orig.c_str());
if (ccombstr.Append(L" (CComBSTR)") == S_OK)
{
CW2A printstr(ccombstr);
cout << printstr << endl;
}
// Convert to a CString
CString cstring(orig.c_str());
cstring += " (CString)";
cout << cstring << endl;
// Convert to a System::String
String ^systemstring = gcnew String(orig.c_str());
systemstring += " (System::String)";
Console::WriteLine("{0}", systemstring);
delete systemstring;
}
Output
Hello, World! (basic_string)
Hello, World! (char *)
Hello, World! (wchar_t *)
Hello, World! (_bstr_t)
Hello, World! (CComBSTR)
Hello, World! (CString)
Hello, World! (System::String)
---------------------------------------------
Converting from System::String
Example
This example demonstrates how to convert from a System.String to the other string types listed above.
Copy Code
// convert_from_system_string.cpp
// compile with: /clr /link comsuppw.lib
#include <iostream>
#include <stdlib.h>
#include <string>
#include "atlbase.h"
#include "atlstr.h"
#include "comutil.h"
#include "vcclr.h"
using namespace std;
using namespace System;
using namespace System::Runtime::InteropServices;
int main()
{
String ^orig = gcnew String("Hello, World!");
Console::WriteLine("{0} (System::String)", orig);
pin_ptr<const wchar_t> wch = PtrToStringChars(orig);
// Convert to a char*
size_t origsize = wcslen(wch) + 1;
const size_t newsize = 100;
size_t convertedChars = 0;
char nstring[newsize];
wcstombs_s(&convertedChars, nstring, origsize, wch, _TRUNCATE);
strcat_s(nstring, " (char *)");
cout << nstring << endl;
// Convert to a wchar_t*
wchar_t wcstring[newsize];
wcscpy_s(wcstring, wch);
wcscat_s(wcstring, L" (wchar_t *)");
wcout << wcstring << endl;
// Convert to a _bstr_t
_bstr_t bstrt(wch);
bstrt += " (_bstr_t)";
cout << bstrt << endl;
// Convert to a CComBSTR
CComBSTR ccombstr(wch);
if (ccombstr.Append(L" (CComBSTR)") == S_OK)
{
CW2A printstr(ccombstr);
cout << printstr << endl;
}
// Convert to a CString
CString cstring(wch);
cstring += " (CString)";
cout << cstring << endl;
// Convert to a basic_string
wstring basicstring(wch);
basicstring += L" (basic_string)";
wcout << basicstring << endl;
delete orig;
}
Output
Hello, World! (System::String)
Hello, World! (char *)
Hello, World! (wchar_t *)
Hello, World! (_bstr_t)
Hello, World! (CComBSTR)
Hello, World! (CString)
Hello, World! (basic_string)
-----------------------------------------------------------------------------
// crt_memset.c
/* This program uses memset to
* set the first four chars of buffer to "*".
*/
#include <memory.h>
#include <stdio.h>
int main( void )
{
char buffer[] = "This is a test of the memset function";
printf( "Before: %s\n", buffer );
memset( buffer, '*', 4 );
printf( "After: %s\n", buffer );
}
-----------------------------------------------------------------------------
CString cstrTrace;
......
CString cstrTrace.Format( _T( "[IDEA REQUESTER] CoCreateInstanceEx() error. ERROR NO = 0x%x\n" ), e_ErrID );
-----------------------------------------------------------------------------
#include "IMTRACE.h"
......
CString showMsg(LogfileInfo->pfilepath);
showMsg = "FilePath:"+showMsg;
IM_TRACE(_T(showMsg));
-----------------------------------------------------------------------------
oracle develop:
void COle_databaseView::OnExecutesql()
{
///DeleteFromListBox();
CString m_sSql,m_tmp,m_tmp2;
int m_fieldcount,m_recordcount,m_i;
m_sqledit.GetWindowText(m_sSql);
odyn.Open(odb,m_sSql);
m_fieldcount=odyn.GetFieldCount();
//シモネ・ミテ・
m_flexgrid.SetCols(m_fieldcount+1);
m_flexgrid.SetTextMatrix(0,0,"ミナ");
for(int i=0;i<m_fieldcount;i++)
{
m_flexgrid.SetTextMatrix(0,i+1,odyn.GetFieldOriginalName(i));
}
//シモネ・セン
int i=1;
m_recordcount=odyn.GetRecordCount();
m_flexgrid.SetRows(m_recordcount+1);
while(!odyn.IsEOF())
{
char x[10];
::itoa(i,x,10);
m_flexgrid.SetTextMatrix(i,0,x);
for(m_i=0;m_i<m_fieldcount;m_i++)
{
odyn.GetFieldValue(m_i,&ovalue);
m_flexgrid.SetTextMatrix(i,m_i+1,&(*ovalue));
}
odyn.MoveNext();
i++;
}
}
--------------------------------------------------------------------------------
Driver={Oracle in OraDb10g_home1};DSN=cqmorcl;UID=system;PWD=cqm
---------------------------------------------------------------------------------
// exception_specification.cpp
// compile with: /EHs
#include <stdio.h>
void handler() {
printf_s("in handler\n");
}
void f1(void) throw(int) {
printf_s("About to throw 1\n");
if (1)
throw 1;
}
void f5(void) throw() {
try {
f1();
}
catch(...) {
handler();
}
}
// invalid, doesn't handle the int exception thrown from f1()
// void f3(void) throw() {
// f1();
// }
void __declspec(nothrow) f2(void) {
try {
f1();
}
catch(int) {
handler();
}
}
// only valid if compiled without /EHc
// /EHc means assume extern "C" functions don't throw exceptions
extern "C" void f4(void);
void f4(void) {
f1();
}
int main() {
f2();
try {
f4();
}
catch(...) {
printf_s("Caught exception from f4\n");
}
f5();
}
Output
About to throw 1
in handler
About to throw 1
Caught exception from f4
About to throw 1
in handler
--------------------------------------------------------------------------------
bool JIDEA_SqlDatabase::AddDocument(wchar_t* strTitile,wchar_t* pText,IDEA_DOCID nDocId);
bool JIDEA_SqlDatabase::Commit(void);
------------------------------------------------------------------------------------------------------------------------
// RegDocInfo.cpp : begin
#include "stdafx.h"
#include "RegDoc.h"
#include "RegDocInfo.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNAMIC(CRegDocInfo, CRecordset)
CRegDocInfo::CRegDocInfo(CDatabase* pdb) : CRecordset(pdb)
{
//{{AFX_FIELD_INIT(CRegDocInfo)
m_ri_docid = 0;
m_ri_filename = _T("");
m_ri_title = _T("");
m_nFields = 3;
//}}AFX_FIELD_INIT
m_nDefaultType = dynaset;
}
CString CRegDocInfo::GetDefaultConnect()
{
return _T("ODBC;DSN=IDEA-TESTAPP");
}
CString CRegDocInfo::GetDefaultSQL()
{
return _T("[dbo].[RegDocInfo]");
}
void CRegDocInfo::DoFieldExchange(CFieldExchange* pFX)
{
//{{AFX_FIELD_MAP(CRegDocInfo)
pFX->SetFieldType(CFieldExchange::outputColumn);
RFX_Long(pFX, _T("[ri_docid]"), m_ri_docid);
RFX_Text(pFX, _T("[ri_filename]"), m_ri_filename);
RFX_Text(pFX, _T("[ri_title]"), m_ri_title);
//}}AFX_FIELD_MAP
}
/////////////////////////////////////////////////////////////////////////////
#ifdef _DEBUG
void CRegDocInfo::AssertValid() const
{
CRecordset::AssertValid();
}
void CRegDocInfo::Dump(CDumpContext& dc) const
{
CRecordset::Dump(dc);
}
#endif //_DEBUG
//RegDocInfo.cpp : end
--------------------------
//call CRegDocInfo class:
unsigned int CRegDocDlg::GetRegDocCount()
{
CRegDocInfo rs( &( ( CRegDocApp* )AfxGetApp() )->m_db );
try
{
rs.Open( CRecordset::snapshot, _T( "SELECT COUNT(*) FROM RegDocInfo" ), CRecordset::readOnly );
}
catch( CDBException* e )
{
e->ReportError();
e->Delete();
}
catch( CMemoryException* e )
{
e->ReportError();
e->Delete();
}
return rs.m_ri_docid;
}
-------------------------
try {
throw CSomeOtherException();
}
catch(...) { // Handle all exceptions
// Respond (perhaps only partially) to exception
throw; // Pass exception to some other handler
}
-------------------------
// exceptions_trycatchandthrowstatements.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
int main() {
char *buf;
try {
buf = new char[512];
if( buf == 0 )
throw "Memory allocation failure!";
}
catch( char * str ) {
cout << "Exception raised: " << str << '\n';
}
}
-------------------------------------------------------------------------------------------------------------------------
int nUid = cstrConnectWord.Find( _T( "UID=" ) );
CString cstrUidPass = cstrConnectWord.GetBuffer( cstrConnectWord.GetLength() ) + nUid;
-------------------------------------------------------------------------------------------------------------------------
convert from string to int
::_ttoi( cstrReadText );
-------------------------------------------------------------------------------------------------------------------------
L("");
_T("");
-------------------------------------------------------------------------------------------------------------------------
ヤレエーフ袒ミム。ヨミcheckBoxカヤサーソコ
void CRegDocDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CRegDocDlg)
//IDC_MAIN_IDEACHECK ハヌcheckboxオトid
DDX_Check(pDX, IDC_MAIN_IDEACHECK, m_bIndexCreate);
DDX_Check(pDX, IDC_MAIN_TOOLCHECK, m_bToolDbCreate);
//}}AFX_DATA_MAP
}
------------
GetDlgItem( IDC_MAIN_COMMITCOUNT )->GetWindowText( cstrCommitCnt );
-------------------------------------------------------------------------------------------------------------------------
strcpy(m_pSql, szSelectStatement);
-------------------------------------------------------------------------------------------------------------------------
#include <exception>
#include <cstdio>
int main(void)
{
try
{
throw new std::exception("bork");
}
catch (std::exception const&)
{
printf("std::exception\n");
}
catch(...)
{
printf("unknown exception\n");
}
return 0;
}
-------------------------------------------------------------------------------------------------------------------------
/* FLOOR.C: This example displays the largest integers
* less than or equal to the floating-point values 2.8
* and -2.8. It then shows the smallest integers greater
* than or equal to 2.8 and -2.8.
*/
#include <math.h>
#include <stdio.h>
void main( void )
{
double y;
y = floor( 2.8 );
printf( "The floor of 2.8 is %f\n", y );
y = floor( -2.8 );
printf( "The floor of -2.8 is %f\n", y );
y = ceil( 2.8 );
printf( "The ceil of 2.8 is %f\n", y );
y = ceil( -2.8 );
printf( "The ceil of -2.8 is %f\n", y );
}
Output
The floor of 2.8 is 2.000000
The floor of -2.8 is -3.000000
The ceil of 2.8 is 3.000000
The ceil of -2.8 is -2.000000
---------------------------------------------------------------------------------------------------------------------------
#include "stdafx.h"
#include <stdio.h>
#include <iostream.h>
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
void HandleError(SQLHANDLE hHandle, SQLSMALLINT hType, RETCODE RetCode)
{
SQLSMALLINT iRec = 0;
SQLINTEGER iError;
TCHAR szMessage[1000];
TCHAR szState[SQL_SQLSTATE_SIZE];
if (RetCode == SQL_INVALID_HANDLE)
{
fprintf(stderr,"Invalid handle!\n");
return;
}
while (SQLGetDiagRec(hType,
hHandle,
++iRec,
(SQLCHAR *)szState,
&iError,
(SQLCHAR *)szMessage,
(SQLSMALLINT)(sizeof(szMessage) /
sizeof(TCHAR)),
(SQLSMALLINT *)NULL) == SQL_SUCCESS)
{
fprintf(stderr,TEXT("[%5.5s] %s (%d)\n"),szState,szMessage,iError);
}
}
class ODBC_Class
{
//attributes
public:
SQLHANDLE henv;
SQLHANDLE hdbc1;
SQLHANDLE hstmt;
SQLRETURN rc;
//methods
public:
ODBC_Class();
~ODBC_Class();
SQLRETURN SendLongData();
};
ODBC_Class::ODBC_Class()
{
rc = SQL_SUCCESS; //init the return code variable
//Allocate an environment handle.
rc = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv); //Allocate the env handle.
//Set the ODBC application version to 3.x.
if (rc ==SQL_SUCCESS)
rc = SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION, (SQLPOINTER)
SQL_OV_ODBC3, SQL_IS_UINTEGER);
//Allocate a connection handle.
if (rc == SQL_SUCCESS)
rc = SQLAllocHandle(SQL_HANDLE_DBC,henv, &hdbc1);
}
ODBC_Class::~ODBC_Class()
{
//Free the connection handle.
if (hdbc1 != NULL)
SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
//Free the environment handle.
if (henv != NULL)
SQLFreeHandle(SQL_HANDLE_ENV, henv);
}
int main(int argc, char* argv[])
{
SQLRETURN rc = SQL_SUCCESS;
CHAR DBName[100] = "YourDataBaseName";
CHAR User[100] = "YourUID";
CHAR Password[100] = "YourPassword";
CHAR SQLstmt[255];
CHAR* m_id = ("1234");
CHAR* m_name;
OLECHAR* pSrc;
SQLINTEGER bufsize = 204801;
SQLINTEGER ValSizeName = 5;
SQLINTEGER ValSizeID = SQL_DATA_AT_EXEC;
SQLINTEGER paramMarker = 1;
ODBC_Class Example; //Create an instance of the ODBC_Class to use.
if (Example.hdbc1 != NULL)
{
//Connect to the database.
rc = SQLConnect(Example.hdbc1, (SQLCHAR*) DBName, SQL_NTS,
(SQLCHAR*)User, SQL_NTS, (SQLCHAR*) Password,
SQL_NTS);
if (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
cout << "Connected to " << DBName<< " \n\n" << endl;
//Allocate stmt handle.
rc = SQLAllocStmt(Example.hdbc1, &Example.hstmt);
if ( rc == SQL_SUCCESS)
{
UINT info2 = 60; //Set the timeout value in seconds.
//Fill in the data "abcd...."
m_name = new char[bufsize];
for (int i = 0; i < bufsize; i++)
m_name[i] = (i % 26) + 65;
m_name[bufsize-1] = '#';
OLECHAR* m_olechar = new OLECHAR[bufsize*2];
int result = MultiByteToWideChar(CP_ACP, 0, m_name, -1,
m_olechar, bufsize*2);
//Bind the parameters.
rc = SQLBindParameter(Example.hstmt,1,SQL_PARAM_INPUT,
SQL_C_WCHAR, SQL_LONGVARCHAR,
bufsize*2, 0, (SQLPOINTER)
paramMarker, 0, &ValSizeName);
if (rc != SQL_SUCCESS)
{
HandleError(Example.hstmt,SQL_HANDLE_STMT, rc);
}
rc = SQLBindParameter(Example.hstmt,2,SQL_PARAM_INPUT,
SQL_C_DEFAULT, SQL_CHAR, 4, 0, (SQLPOINTER) m_id, 4, NULL);
strcpy(SQLstmt,"UPDATE repro SET name=? WHERE id=?");
ValSizeName = SQL_LEN_DATA_AT_EXEC(bufsize*2);
rc = SQLExecDirect(Example.hstmt, (SQLCHAR*) SQLstmt,
SQL_NTS);
paramMarker = -1;
rc = SQLParamData(Example.hstmt,
(SQLPOINTER*)¶mMarker);
//Loop through and insert the data.
int j = 2*bufsize/8188;
int remains = bufsize*2;
pSrc = m_olechar;
for (int k=0; k<j; k++)
{
remains -= 8188;
rc =SQLPutData(Example.hstmt,(SQLPOINTER)pSrc , 8188);
if (rc != SQL_SUCCESS)
HandleError(Example.hstmt,SQL_HANDLE_STMT, rc);
pSrc += 4094; //Increment widechar pointer by 4094 = 8188
//bytes
cout << rc << "\n";
}
rc =SQLPutData(Example.hstmt,(SQLPOINTER)pSrc , remains);
rc = SQLParamData(Example.hstmt, (SQLPOINTER*)paramMarker);
if (rc != SQL_SUCCESS)
HandleError(Example.hstmt, SQL_HANDLE_STMT, rc);
}
rc = SQLDisconnect(Example.hdbc1);
}
return (rc);
}