-
March 17th, 2009, 02:21 AM
#1
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?
-
March 17th, 2009, 02:36 AM
#2
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.
-
March 17th, 2009, 02:39 AM
#3
Re: CString to const char*
Hi,
How would I use CFile to replace fopen, fprintf and fclose?
-
March 17th, 2009, 02:43 AM
#4
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().
-
March 17th, 2009, 02:45 AM
#5
Re: CString to const char*
Code:
CFile file;
file.Open("filename",CFile::modeRead);
-
March 17th, 2009, 02:49 AM
#6
Re: CString to const char*
Originally Posted by galapogos
Hi,
How would I use CFile to replace fopen, fprintf and fclose?
There are examples in MSDN for using CFile and CStdioFile.
-
March 17th, 2009, 02:50 AM
#7
Re: CString to const char*
Code:
CString filename(_T(""));
FILE *fp;
filename = someotherstring.GetString();
_tfopen_s(&fp, filename, _T("w"));
-
March 17th, 2009, 05:27 AM
#8
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?
-
March 17th, 2009, 05:37 AM
#9
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: %s\n"), lpFileName);
free(lpFileName );
Last edited by quandary; March 17th, 2009 at 05:51 AM.
-
March 17th, 2009, 05:47 AM
#10
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.
-
March 17th, 2009, 07:09 AM
#11
Re: CString to const char*
Originally Posted by quandary
#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.
-
March 17th, 2009, 07:10 AM
#12
Re: CString to const char*
Originally Posted by galapogos
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.
-
March 17th, 2009, 09:11 AM
#13
Re: CString to const char*
Originally Posted by GCDEF
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.
-
March 17th, 2009, 09:25 AM
#14
Re: CString to const char*
Originally Posted by quandary
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.
-
March 17th, 2009, 09:42 AM
#15
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++.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|