Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use CString with sprintf

I have some C++ code where I need to use CString with sprintf. In this code I'm creating file names that are CStrings that are defined by sprintf. The code is below.

double Number;     
Number = 0.25; 

char buffer [50];

CString sFile;
sFile = sprintf(buffer,"TRJFPICD(%3.3f).txt",Number);

CString SFFile;
SFFile = sprintf(buffer,"TRJFPICV(%3.3f).txt",Number);

CString SFFFile;
SFFFile = sprintf(buffer,"TRJFPICA(%3.3f).txt",Number);

The desired file names are TRJFPICD(0.25).txt, TRJFPICV(0.25).txt, and TRJFPICA(0.25).txt. I have to use CStrings for my code.

The error I get is 'operator =' is ambiguous.

like image 520
Grady F. Mathews Iv Avatar asked Apr 16 '26 05:04

Grady F. Mathews Iv


1 Answers

Take a look at CString::Format (ignore the CStringT part - CString is derived from CStringT). It does what you want and allows you to rewrite your code cleanly:

double Number = 0.25; 

CString sFile;
sFile.Format(_T("TRJFPICD(%3.3f).txt"), Number);

CString SFFile;
SFFile.Format(_T("TRJFPICV(%3.3f).txt"),Number);

CString SFFFile;
SFFFile.Format(_T("TRJFPICA(%3.3f).txt"),Number);
like image 65
Nik Bougalis Avatar answered Apr 18 '26 20:04

Nik Bougalis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!