• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 482
  • Last Modified:

MFC: How to populate CListView with std::string

Dear Experts

I have a problem populating a CListView with std::string. Besides the string it I get a "square" (I guess it means empty space), then the string and then lots of strange letters such as ýýýý and stuff like that. I guess it has to do with the way I convert the std::string to lpstr, but I have tried many many things, also followed some advices here on EE. Below I have posted two functions. First the one filling up the ListView, second the one doing the conversion between std::string and lpstr. I hope you can help me with this one.

Thank you very much in advance, Peter

void CSeriesView::InsertCols()
{
      CSeriesDoc *iDoc = GetDocument();
      CListCtrl& m_List = GetListCtrl();
      LVCOLUMN lvColumn;
      lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
      lvColumn.fmt = LVCFMT_LEFT;
      lvColumn.cx = 120;

      lvColumn.pszText = "Time";
      m_List.InsertColumn(0, &lvColumn);

      int i = 1;
      for (timeseries<double>::iteratorS _S = iDoc->iSeries.begin_s(); _S != iDoc->iSeries.end_s(); _S++)
      {
            std::string st = _S->first;
            lvColumn.pszText = _T(str2lpstr(st));
            m_List.InsertColumn(i, &lvColumn);
            i++;
      }
}

LPSTR CLASS_DECLSPEC inline str2lpstr(std::string aS)
{
      int iLen = aS.length();
      LPSTR iOut = new char[iLen+1];
      aS._Copy_s(iOut,iLen,iLen);
      strncpy(iOut + 1, aS.c_str(), iLen);
      iOut[0]= (BYTE) iLen;
      return iOut;
}
0
PeterTokeHedenAhlgren
Asked:
PeterTokeHedenAhlgren
  • 3
  • 2
  • 2
  • +1
2 Solutions
 
LordOfPortsCommented:
If you go to Project -> Properties on the menu, what is the value for the "Character Set" attribute under the "General" node of "Configuration Properties"?
0
 
AndyAinscowCommented:
Does changing
 lvColumn.pszText = _T(str2lpstr(st));
to
 lvColumn.pszText = str2lpstr(st);

cure the problem?
0
 
alb66Commented:
LPSTR iOut is declared local  to str2lpstr() function. Outside that function it is no more valid.
Try the following:

vColumn.pszText = st.c_str();
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
PeterTokeHedenAhlgrenAuthor Commented:
Dear Experts

I'm sorry to keep you waiting.

Quick answers to your question are:

Lord of Ports: The Character Set is Not Set. I prefer this setting, At least I'm certain that unicode won't work with other parts of the project.

Andy: Changing vColumn.pszText = _T(str2lpstr(st));
to
lvColumn.pszText = str2lpstr(st);
does not help either.

Alb66: Your suggestion (simply writing vColumn.pszText = st.c_str();) gives me an error message during compilation saying:
C2440: '=' cannot convert from 'const char *' to 'LPSTR'.

I hope this made you just a little closer to what is wrong.

Thanks, Peter
0
 
alb66Commented:
vColumn.pszText = (LPSTR)st.c_str();
0
 
AndyAinscowCommented:
change
      strncpy(iOut + 1, aS.c_str(), iLen);
      iOut[0]= (BYTE) iLen;
to
      strncpy(iOut, aS.c_str(), iLen);
      //iOut[0]= (BYTE) iLen;

0
 
alb66Commented:
Warning: iOut buffer is never deallocated
0
 
PeterTokeHedenAhlgrenAuthor Commented:
Thanks!!!

Embarrassing with the final LPSTR cast. I've tried so many things but not this one. It works. GREAT!!!

I think alb66 has a very valid point about my str2lpstr function. What you actually say is that I should always use a procedure like(?):

std::string st;
LPSTR lp_st;
lp_st = (LPSTR)st.c_str();

instead of the str2lpstr function.

Thansk again to all of you
0

Featured Post

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.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now