error

programming MFC...Keep getting this error for these lines of code

1
2
3
4
5
6
7
8
9
10
11
12
13
FILE * fstPayroll;

		UpdateData(TRUE);

	
		fstPayroll = _wfopen(m_filename + L".txt","w+");

	

		fwprintf(fstPayroll, L"%d\n", m_enumber);
		fwprintf(fstPayroll, L"%s\n", m_name);
	
		fclose(fstPayroll);



error C2664: '_wfopen' : cannot convert parameter 1 from 'class CString' to 'const unsigned short *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called


if i use fstPayroll = fopen(m_filename + L".txt","w+"); insted of _wfopen..it just takes the decimals values and not strings..such as CString
Last edited on
So you don't know basic things as CString concatenation ... use += operator overloading such as:
1
2
m_filename += _T(".txt");
fstPayroll = _tfopen(m_filename, _T("w+"));


Use tchar.h routines, don't you read MSDN documentation first ??
i used _tfopen, but it still does not get the STRING..

my program has a INT value edit box
my program has a CString value edit box



the CSTRING is not getting logged to the txt file
Are you compiling for Unicode?

If I fix the last param (add a L to "w+") you code compiles ok for me.

I do notice the warning about 'unsigned short' rather than wchar_t, which make me wonder about the age of your compiler?

But as mordoran suggests, you should use the tchar.h macros, etc. Consistently!

Andy

PS Out of interest, where are you calling this fstream-based code from? MFC provides CStdioFile and CArchive which offer a more "MFC idomatic" way of writing and readin date. This hold especially if this code is in class derived from CDocument.

Last edited on
1
2
3
4
5
6
7
CString path = m_filename + _T(".txt");
fstPayroll = _wfopen((LPCTSTR)path,_T("w+"));

or

CString path = m_filename + _T(".txt");
fstPayroll = _wfopen((LPCTSTR)path,_T("w+"));

using these codes adove still does not work and gives this error



error C2664: '_wfopen' : cannot convert parameter 1 from 'const char *' to 'const unsigned short *'


im going to use unicode to see if that works
Last edited on
Everything points to a mixture of Ansi and Unicode.

Check your project setting and make sure they are set to build for Unicode

(If you're using VC++, that's in the projects General setting:

should be: "Character Set" = "Use Unicode Character Set")

But this code code (tweaked for tchar.h) should build as is for both Unicode and Multi-Byte (Ansi)

1
2
3
4
5
6
7
8
FILE * fstPayroll;

fstPayroll = _tfopen(m_filename + _T(".txt"), _T("w+"));

_ftprintf(fstPayroll, _T("%d\n"), m_enumber);
_ftprintf(fstPayroll, _T("%s\n"), m_name); // also a CString?

fclose(fstPayroll);


Andy
Last edited on
You insists on using _wfopen ... Then convert from ansi to unicode first ...

1
2
3
4
#include <atlbase.h>
USES_CONVERSION; // only needed for ATL < 7, as it seems the case here
CString path = m_filename + _T(".txt");
fstPayroll = _wfopen(CT2W(path) , L"w+");



Don't know if CT2W works for MFC too.
Last edited on
@modoran

If you just want to steal ATL's conversion macros, you can include MFC's afxconv.h

This defines a few things that ATL assumes to be around and then includes atlconv.h directly.
got it to work with andys soultion...thx
re tchar.h stuff.

Actually, I was just re-restating modoran's suggestion that bit more completely...
Topic archived. No new replies allowed.