1

(To whom who may mark this as duplicate:

I have seen Convert CString to const char* which returns the sam error when I use this macro:

  CT2A ascii(m_InstrumentName, CP_UTF8);
  TRACE(_T("UTF8: %S\n"), ascii.m_psz);

that I don't know how this works

and Problem: How to convert CString into const char * in C++ MFC (it returns error C2440: 'type cast' : cannot convert from 'CString' to 'LPCSTR' ) )

I have a code that gives me this error:

Error 6 error C2664: 'void CElliotEngineDoc::ExportFile(CFile *,const char *)' : cannot convert argument 2 from 'CString' to 'const char *'

It's not my code, and it was written in VC 8.0 and I'm using VC 12.0

Here are the parts that the function is defined and is called.

Could you please tell me what would be the problem?

The function that is called:

void CElliotEngineDoc::ExportFile(CFile* fp, const char* isin)
{
  CString s = "time;isin;high;low;last;vol$;open\r\n";
  fp->Write(s, s.GetLength());

  char buf[100];
  PriceNodeVec::const_iterator it = m_PriceNodes.begin();
  while (it != m_PriceNodes.end())
  {
    const PriceNode& n = *it++;

    sprintf(buf, "%s;%s;%g;%g;%g;%g;%g\r\n",
      n.date.Format("%Y%m%d"),
      isin,
      n.high,
      n.low,
      n.price,
      n.volume,
      n.close );
    fp->Write(buf, strlen(buf));
  } // while
}

here is the definition of CString (if I'm not wrong):

typedef ATL::CStringT< wchar_t, StrTraitMFC_DLL< wchar_t > > CStringW;
typedef ATL::CStringT< char, StrTraitMFC_DLL< char > > CStringA;
typedef ATL::CStringT< TCHAR, StrTraitMFC_DLL< TCHAR > > CString;

And m_InstrumentName is defined like this:

  CString       m_InstrumentName;

And the error is in this line:

  ExportFile(ar.GetFile(), m_InstrumentName);
Community
  • 1
  • 1
Yousef
  • 393
  • 8
  • 23
  • Did you compile with Unicode enabled? Then your `TCHAR` would be a `wchar_t`, and `CString` would not cast to `char` but `wchar_t`. You could then use `CT2A`, [see here](http://stackoverflow.com/questions/859304/convert-cstring-to-const-char) – Karsten Koop Jun 14 '16 at 08:18
  • I added this project from http://sourceforge.net/projects/ewavetrade/ and it is written in VC 8.0 (according to the project file), and I use VC 12.0. When I import it in VS 2013, it returns an error. I don't remember the error, but it needed to set solution's properties to Unicode. – Yousef Jun 14 '16 at 09:16
  • @KarstenKoop I also said that I couldn't find a way to solve my problem in the my question by the link you provided. (last day my question, marked as duplicate, yet I couldn't find a solution) – Yousef Jun 14 '16 at 09:25
  • In what way does `CT2A` not work? Have you tried passing your `ascii.m_psz` to `ExportFile`? – Karsten Koop Jun 14 '16 at 09:29
  • Error came from this line ExportFile(ar.GetFile(), m_InstrumentName); so I added these two lines: CT2A ascii(m_InstrumentName, CP_UTF8); TRACE(_T("UTF8: %S\n"), ascii.m_psz); Did I do that right? – Yousef Jun 14 '16 at 09:32
  • 1
    Well, the first line gives you an ascii string in the variable `ascii`, the second line is just for testing. You have to then use the variable `ascii` in the next step, just adding these two lines doesn't change the rest of your program. Just pass `ascii.m_psz` to `ExportFile` instead of `m_InstrumentName`. – Karsten Koop Jun 14 '16 at 09:34

0 Answers0