patrickm
asked on
CString.Format()
When using a CString, it appears to truncate a 255 chars. How do you control this?
TIA
Pat...
TIA
Pat...
Can you show me the code in which the truncating takes place. CString doesn't truncate. I was able to use a string of over 300 characters without any problems using the CString::Format method.
Trace output certainly has this problem
Also output in the debug window has similar limitations.
Perhap-s this is what you are observing?
Also output in the debug window has similar limitations.
Perhap-s this is what you are observing?
According to the MSDN: "A CString object can store up to INT_MAX (2,147,483,647) characters.". Your problem must be elsewhere. Where you try to see the string with the trace output/watch utility try to issue a command:
AfxMessageBox(strYourStrin g);
This will display a message box that can accomodate far bigger strings that 255 chars.
AfxMessageBox(strYourStrin
This will display a message box that can accomodate far bigger strings that 255 chars.
You may have a "\0" character in there. What are you trying to do? Sometimes you need a string with embedded "\0"s, but then you need to treat it differently if you want to print it out.
khyatt has a good point. If you need the buffer for the CString and you suspect there may be \0 in there, you can "manually" check by calling GetBuffer("some large number");
Simply analyze the returned buffer and check for \0. Don't forget to call ReleaseBuffer after you're done.
Glenn
Simply analyze the returned buffer and check for \0. Don't forget to call ReleaseBuffer after you're done.
Glenn
from MSDN:
CString::Format
void Format( LPCTSTR lpszFormat, ... );
void Format( UINT nFormatID, ... );
Parameters
lpszFormat
A format-control string.
nFormatID
The string resource identifier that contains the format-control string.
Remarks
Call this member function to write formatted data to a CString in the same way that sprintf formats data into a C-style character array. This function formats and stores a series of characters and values in the CString. Each optional argument (if any) is converted and output according to the corresponding format specification in lpszFormat or from the string resource identified by nFormatID.
The call will fail if the string object itself is offered as a parameter to Format. For example, the following code:
CString str = "Some Data";
str.Format("%s%d", str, 123); // Attention: str is also used in the parameter list.
will cause unpredictable results.
When you pass a character string as an optional argument, you must cast it explicitly as LPCTSTR. The format has the same form and function as the format argument for the printf function. (For a description of the format and arguments, seeprintf in the Run-Time Library Reference.) A null character is appended to the end of the characters written.
For more information, seesprintf in the Run-Time Library Reference.
Example
CString str;
str.Format(_T("Floating point: %.2f\n"), 12345.12345);
_tprintf("%s", (LPCTSTR) str);
str.Format(_T("Left-justif ied integer: %.6d\n"), 35);
_tprintf("%s", (LPCTSTR) str);
str.Format(IDS_SCORE, 5, 3);
_tprintf("%s", (LPCTSTR) str);
Output
If the application has a string resource with the identifier IDS_SCORE that contains the string "Penguins: %d\nFlyers : %d\n", the above code fragment produces this output:
Floating point: 12345.12
Left-justified integer: 000035
Penguins: 5
Flyers : 3
CString::Format
void Format( LPCTSTR lpszFormat, ... );
void Format( UINT nFormatID, ... );
Parameters
lpszFormat
A format-control string.
nFormatID
The string resource identifier that contains the format-control string.
Remarks
Call this member function to write formatted data to a CString in the same way that sprintf formats data into a C-style character array. This function formats and stores a series of characters and values in the CString. Each optional argument (if any) is converted and output according to the corresponding format specification in lpszFormat or from the string resource identified by nFormatID.
The call will fail if the string object itself is offered as a parameter to Format. For example, the following code:
CString str = "Some Data";
str.Format("%s%d", str, 123); // Attention: str is also used in the parameter list.
will cause unpredictable results.
When you pass a character string as an optional argument, you must cast it explicitly as LPCTSTR. The format has the same form and function as the format argument for the printf function. (For a description of the format and arguments, seeprintf in the Run-Time Library Reference.) A null character is appended to the end of the characters written.
For more information, seesprintf in the Run-Time Library Reference.
Example
CString str;
str.Format(_T("Floating point: %.2f\n"), 12345.12345);
_tprintf("%s", (LPCTSTR) str);
str.Format(_T("Left-justif
_tprintf("%s", (LPCTSTR) str);
str.Format(IDS_SCORE, 5, 3);
_tprintf("%s", (LPCTSTR) str);
Output
If the application has a string resource with the identifier IDS_SCORE that contains the string "Penguins: %d\nFlyers : %d\n", the above code fragment produces this output:
Floating point: 12345.12
Left-justified integer: 000035
Penguins: 5
Flyers : 3
ASKER
Thank you all for your suggestions.
szQuery is truncated to 255 chars after assignment when viewd in the debugger, so RONSLOW may be correct, except for the fact I receive an error "error retrieving record" when executed
Here's a snippet of the offending code:
CString strFromDate,strToDate;
CString strOut("");
strFromDate = dlg.m_pszLowRange;
strToDate = dlg.m_pszHighRange;
DWORD dwERROR = GetLastError();
// Get Recordset
CItemSet m_pItemSet;
CInvoiceDetailSet m_pSet;
CString szQuery("");
CString szQuery1("");
CString szQuery2("");
szQuery.Format("SELECT D.INVOICE_NO,D.LINE_SEQ,D. LINE_TYPE, D.ITEM_NUM BER,D.WARE HOUSE_CODE ,D.PRODUCT _LINE,D.IT EM_DESC,D. COGS_ACCOU NT,D.Origi nalPrice,D .QTY_SHIPP ED,D.UNIT_ PRICE,D.EX TENSION,D. UNIT_COST, D.GL_SALES _ACCT,H.IN VOICE_NO,H .INVOICE_D ATE from InvoiceDetail D,InvoiceHeader H WHERE H.INVOICE_NO = D.INVOICE_NO AND INVOICE_DATE BETWEEN #%s# AND #%s# ORDER BY H.INVOICE_NO,D.LINE_SEQ",s trFromDate ,strToDate ) ;
m_pSet.Open(AFX_DB_USE_DEF AULT_TYPE, szQuery);
if( m_pSet.IsBOF()) // Test for empty record set
{
strOut.Format("There are No Sales on File for %s",dlg.m_pszRange);
AfxMessageBox(strOut,MB_OK );
return; // The recordset is empty
}
szQuery is truncated to 255 chars after assignment when viewd in the debugger, so RONSLOW may be correct, except for the fact I receive an error "error retrieving record" when executed
Here's a snippet of the offending code:
CString strFromDate,strToDate;
CString strOut("");
strFromDate = dlg.m_pszLowRange;
strToDate = dlg.m_pszHighRange;
DWORD dwERROR = GetLastError();
// Get Recordset
CItemSet m_pItemSet;
CInvoiceDetailSet m_pSet;
CString szQuery("");
CString szQuery1("");
CString szQuery2("");
szQuery.Format("SELECT D.INVOICE_NO,D.LINE_SEQ,D.
m_pSet.Open(AFX_DB_USE_DEF
if( m_pSet.IsBOF()) // Test for empty record set
{
strOut.Format("There are No Sales on File for %s",dlg.m_pszRange);
AfxMessageBox(strOut,MB_OK
return; // The recordset is empty
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.