CString to const char*
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: CString to const char*

  1. #1
    Join Date
    Feb 2005
    Posts
    325

    CString to const char*

    Hi,

    I'm trying to use fopen_s on a CString with the following code

    Code:
    CString filename(_T(""));
    FILE *fp;
    filename = someotherstring.GetString();
    fopen_s(&fp, filename, "w");
    However I get the following build error:

    Code:
    error C2664: 'fopen_s' : cannot convert parameter 2 from 'CString' to 'const char *'
    I've tried casting filename with (LPCTSTR), but then I get:

    Code:
    error C2664: 'fopen_s' : cannot convert parameter 2 from 'LPCTSTR' to 'const char *'
    I've also tried casting the filename with (const char *)(LPCTSTR) and it builds ok, but the result of that is simply the 1st character of the filename.

    How can I get fopen_s to use the CString properly?

  2. #2
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,051

    Re: CString to const char*

    You can call _tfopen_s macro instead of fopen_s which avoids headaches of ANSI/UNICODE conversions but once you are using MFC a better solution is the class CFile or CStdioFile.
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  3. #3
    Join Date
    Feb 2005
    Posts
    325

    Re: CString to const char*

    Hi,

    How would I use CFile to replace fopen, fprintf and fclose?

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: CString to const char*

    If you're building to UNICODE, then LPCTSTR resolves to const wchar_t*, so no matter how you cast, it won't work. In that case you can use CT2A class that converts a UNICODE or ANSII string to an ANSII string. Or you can manually convert from UNICODE to ANSI with WideCharToMultiByte().
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Jul 2005
    Location
    E: 120.6, N: 31.3′
    Posts
    795

    Re: CString to const char*

    Code:
    CFile file;
    file.Open("filename",CFile::modeRead);

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: CString to const char*

    Quote Originally Posted by galapogos View Post
    Hi,

    How would I use CFile to replace fopen, fprintf and fclose?
    There are examples in MSDN for using CFile and CStdioFile.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    Join Date
    May 2002
    Posts
    1,435

    Re: CString to const char*

    Code:
    CString filename(_T(""));
    FILE *fp;
    filename = someotherstring.GetString();
    _tfopen_s(&fp, filename, _T("w"));

  8. #8
    Join Date
    Feb 2005
    Posts
    325

    Re: CString to const char*

    Hi,

    I managed to use CStdioFile in the following manner:

    Code:
    CStdioFile file
    if (!file.Open(_T("file.txt"), CStdioFile::modeCreate|CStdioFile:: modeReadWrite))
    {
    	exit(1);
    }
    // ...some file access...
    // ...
    file.Close();
    This works fine. However, when I replace the hardcoded file.txt with a CString variable, it always fails during Open().

    What's wrong?

  9. #9
    Join Date
    Oct 2005
    Posts
    71

    Talking Re: CString to const char*

    #include <tchar.h>

    CString filename(_T("Hello_World.txt"));

    LPTSTR lpFileName = (LPTSTR ) malloc( (_tcslen(filename)+1)*sizeof(TCHAR) + sizeof(TCHAR));
    _tcscpy(lpFileName, filename) ;
    _tprintf(TEXT("FileName: &#37;s\n"), lpFileName);
    free(lpFileName );
    Last edited by quandary; March 17th, 2009 at 05:51 AM.

  10. #10
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: CString to const char*

    exit(1)? Wow, I haven't seen that since I was writing code in Borland C++ 3.1. Don't use exit(1) in windows applications. Return an error code, or through an exception that you catch somewhere else. Don't terminate abnormally.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,127

    Re: CString to const char*

    Quote Originally Posted by quandary View Post
    #include <tchar.h>

    CString filename(_T("Hello_World.txt"));

    LPTSTR lpFileName = (LPTSTR ) malloc( (_tcslen(filename)+1)*sizeof(TCHAR) + sizeof(TCHAR));
    _tcscpy(lpFileName, filename) ;
    _tprintf(TEXT("FileName: %s\n"), lpFileName);
    free(lpFileName );
    I hope you're not serious.

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,127

    Re: CString to const char*

    Quote Originally Posted by galapogos View Post
    Hi,

    I managed to use CStdioFile in the following manner:

    Code:
    CStdioFile file
    if (!file.Open(_T("file.txt"), CStdioFile::modeCreate|CStdioFile:: modeReadWrite))
    {
    	exit(1);
    }
    // ...some file access...
    // ...
    file.Close();
    This works fine. However, when I replace the hardcoded file.txt with a CString variable, it always fails during Open().

    What's wrong?
    Define "fail". You can pass a pointer to a CFileException to the open call and it will give you a detailed explanation.

    CString has an overloaded const char* operator. You should be able to just pass a CString anywhere that wants a const char*. I always use MBCS. Unicode may be different, I don't know.

  13. #13
    Join Date
    Oct 2005
    Posts
    71

    Arrow Re: CString to const char*

    Quote Originally Posted by GCDEF View Post
    I hope you're not serious.
    No, I'm not, since I cannot get CString to work with my compiler...

    Why use CString anyway?
    Use the normal std::string filename, then you can do filename.c_str().
    Then, your application is portable.

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,127

    Re: CString to const char*

    Quote Originally Posted by quandary View Post
    No, I'm not, since I cannot get CString to work with my compiler...

    Why use CString anyway?
    Use the normal std::string filename, then you can do filename.c_str().
    Then, your application is portable.
    I find CString more flexible, in part because of its const char* operator, and because it works with the MFC framework better. In the decades I've been doing this, I've never had to port code to another compiler or platform. I suppose it could happen for some people in some circumstances, but it's never been a consideration for me.

    Sometimes it's better to find out why something doesn't work than to spend a lot of time and effort coding around it.

  15. #15
    Join Date
    Feb 2005
    Posts
    325

    Re: CString to const char*

    By fail, meaning it goes into the exit(1) portion, i.e. file.Open returns 0.

    Yes I come from a unix/linux programming background, so I'm a relative noob in MSVC++.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center