[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

Formatting output in strings

Hi

I want to format a string so that the output is

Item1         : 0
Itemm2      : 0
Itemmm3   : 0

where the colons are vertically aligned.

I tried to make a string with white space and put inbetween the
actual string and the colon to pad it out but the formatting of the string
seems to strip this out and I get left with

Item1 : 0
Itemm2 : 0
Itemmm3 : 0


How can I get the desired effect?

Many thanks
anti
 
0
antinutrino
Asked:
antinutrino
4 Solutions
 
AxterCommented:
Hi antinutrino,
> >but the formatting of the string
> >seems to strip this out and I get left with
What do you mean by strips it out?
How do you know it's getting stripped out?

Can you post the code you're using for formattnig?
Are you displaying this string in a window?

David Maisonave :-)
Cheers!
0
 
AxterCommented:
If you're displaying this string in a window, it will not align in most fonts.
You have to use fonts that have fix width characters.
0
 
antinutrinoAuthor Commented:
Hi

This is the code

                CString sz(_T(""));
      CString szTmp(_T(""));

      sz = "Data";
      sz += "\r\n----------------------------------------------------";
      szTmp.Format("\r\nBytes Received\t: %d", pStat->m_dwBytesReceived);
      sz += szTmp;
      szTmp.Format("\r\nBytes Processed\t: %d", pStat->m_dwBytesProcessed);
      sz += szTmp;
      
      sz += "\r\n\r\n";
      sz += "Errors";
      sz += "\r\n----------------------------------------------------";


      for (int nIdx = 0; nIdx < MAX_ERROR_TYPE; ++nIdx) {
            CString szSpace(" ", (nMaxLen - strlen(m_szErrors[nIdx])));
            szTmp.Format("\r\n%s%s: %d", m_szErrors[nIdx], szSpace, pStat->m_dwErrors[nIdx]);
            sz += szTmp;
      }

      sz += "\r\n\r\n";
      sz += "Events";
      sz += "\r\n----------------------------------------------------";

      for (nIdx = 0; nIdx < MAX_EVENT_TYPE; ++nIdx) {
            //TRACE1("\nLen Event : %d", strlen(m_szEvents[nIdx]));
            CString szSpace(" ", (nMaxLen - strlen(m_szEvents[nIdx])));
            TRACE1("\nLen space : %d", szSpace.GetLength());      
            szTmp.Format("\r\n%s%s: %d", m_szEvents[nIdx], szSpace, pStat->m_dwEvents[nIdx]);
            sz += szTmp;
      }

      m_wndSummaryEdit.Clear();
      m_wndSummaryEdit.SetWindowText(sz);
      m_wndSummaryEdit.UpdateWindow();

This is the output

Data
----------------------------------------------------
Bytes Received      : 1827504
Bytes Processed      : 0

Errors
----------------------------------------------------
CE_BREAK : 0
CE_FRAME : 0
CE_IOE : 0
CE_MODE : 0
CE_OVERRUN : 0
CE_RXOVER : 0
CE_RXPARITY : 0
CE_TXFULL : 0

Events
----------------------------------------------------
EV_BREAK : 0
EV_CTS : 0
EV_DSR : 0
EV_ERR : 0
EV_RING : 0
EV_RLSD : 0
EV_RXCHAR : 398639
EV_RXFLAG : 0
EV_TXEMPTY : 1

Many thanks
anti
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
antinutrinoAuthor Commented:
missed abit

const int nMaxLen = 20;

.......


0
 
AxterCommented:
>>m_wndSummaryEdit.SetWindowText(sz);

Try adding a trace command after the above line of code, and see if the format looks good in the debug window.

If it looks good in the debug window, then the problem is with the font you're using.
0
 
antinutrinoAuthor Commented:
Hi

the format in the trace is just the same.

??



0
 
AxterCommented:
To format a string to a specific size, you can use fomat width and precesion specifiers.
Example:
szTmp.Format("\r\n%10.10s%10.10s: %d", m_szErrors[nIdx], szSpace, pStat->m_dwErrors[nIdx]);

0
 
AxterCommented:
continue ......

%[flags] [width] [.precision] [{h | l | I64 | L}]type


The number before the period is the width.  
The number after the period is the precision.

Use the width to specify the minimum size of the inserted string.
Use the precision to specify the maximum size of the inserted string.

%21.21s

0
 
antinutrinoAuthor Commented:
Hi

Thanks for the info.

Data
----------------------------------------------------
Bytes Received      : 174048
Bytes Processed      : 0

Errors
----------------------------------------------------
            CE_BREAK: 0
            CE_FRAME: 0
              CE_IOE: 0
             CE_MODE: 0
          CE_OVERRUN: 0
           CE_RXOVER: 0
         CE_RXPARITY: 0
           CE_TXFULL: 0

Events
----------------------------------------------------
            EV_BREAK: 0
              EV_CTS: 0
              EV_DSR: 0
              EV_ERR: 0
             EV_RING: 0
             EV_RLSD: 0
           EV_RXCHAR: 38640
           EV_RXFLAG: 0
          EV_TXEMPTY: 1

now this is the output.
0
 
AxterCommented:
>>now this is the output.

Is that the output with the string format using width and precision specifiers?

If so, can you post the new code?
0
 
AndyAinscowCommented:
A method that will work with ANY font is to create your own class based on CEdit, use this class as the object type (CMyEdit m_wndSummaryEdit in .h file).
In this class you create you handle the OnPaint yourself and draw the text exactly where you want it to appear.
0
 
NawalKishore1976Commented:
I can suggest you a very good way.

Set any MonoFont (fixed width) font like Courier, New Courier or System like that.
0
 
Knut HunstadCommented:
I think you might be looking for the following:

const CString sFormat("\r\n%-21s: %5d");

szTmp.Format(sFormat, m_szEvents[nIdx], pStat->m_dwEvents[nIdx]);

and to change the font in your OnDraw-routine you could use:

  CFont fCourier;
   fCourier.CreateFont(
      12,                        // nHeight
      0,                         // nWidth
      0,                         // nEscapement
      0,                         // nOrientation
      FW_NORMAL,                 // nWeight
      FALSE,                     // bItalic
      FALSE,                     // bUnderline
      0,                         // cStrikeOut
      ANSI_CHARSET,              // nCharSet
      OUT_DEFAULT_PRECIS,        // nOutPrecision
      CLIP_DEFAULT_PRECIS,       // nClipPrecision
      DEFAULT_QUALITY,           // nQuality
      FIXED_PITCH | FF_MODERN,  // nPitchAndFamily
      "Courier");                 // lpszFacename
   pDC->SelectObject(&fCourier);
   fCourier.DeleteObject();
 
to get a fixed-width font, which you also need.
0
 
antinutrinoAuthor Commented:
sorry for not returning to this question, i have been away for sometime.

Thanks for the help.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now