Link to home
Start Free TrialLog in
Avatar of BrianM_AZ
BrianM_AZ

asked on

Create UNICODE file using MFC/C++

How do I create a UNICODE encoded file using MFC? It appears that CStdioFile only encodes in ANSI. When I try to "WriteString" it fails.

FILE *fStream;
errno_t err;
err = _tfopen_s( &fStream, _T("test.txt"), _T("w+ ,ccs=UNICODE") );
if( err == 0 )
{        
     CStdioFile f(fStream);
     f.WriteString( _T("TestLine") );
     f.Close();
}

Open in new window

Thanks for the help
Avatar of jkr
jkr
Flag of Germany image

If your project is set to compile as UNICODE, that should work. But, why are you using a CStdioFile at all since you are already using a file descriptor? You could just use

FILE *fStream;
errno_t err;
err = _tfopen_s( &fStream, _T("test.txt"), _T("w+ ,ccs=UNICODE") );
if( err == 0 )
{        
     _ftprintf(_T("TestLine"));
}
                                  

Open in new window

Ooops, sorry, that should have been

FILE *fStream;
errno_t err;
err = _tfopen_s( &fStream, _T("test.txt"), _T("w+ ,ccs=UNICODE") );
if( err == 0 )
{        
     _ftprintf(fStream,_T("TestLine"));
}
                                  
                                            

Open in new window

Avatar of BrianM_AZ
BrianM_AZ

ASKER

Unfortunately my project is not setup to compile as UNICODE. All I’m trying to do is create a UNICODE text file to be used by another process.

I used CStdioFile because I’m more familiar with its usage.  I tried your idea but even fprintf validates that the stream is ANSI so it also asserts.
>>Unfortunately my project is not setup to compile as UNICODE

Hm, then the above should not even compile, but that explains why you won't get a UNICODE text file. But, you could just use STL classes or the CRT to write such a file, e.g.

// STL
#include <fstream>
using namespace std;

// ...

wofstream wos("test.txt");

wos << L"UNICODE text";

Open in new window


// CRT

#include <stdio.h>
#include <wchar.h>

FILE* p = _wfopen(L"test.txt".L"wt+,ccs=UNICODE");

fwprintf(p,L"%s",L"UNICODE text");

fclose(p);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Using CFile seems to be the ticket! I have one more question I hope you don't mind answering...
Here's my code -
CFile file("test.txt", CFile::modeCreate | CFile::modeWrite);
CStringW sOutStr = "Test output string";
file.Write( sOutStr, wcslen(sOutStr.GetBuffer()) * 2 );

Open in new window

Why do I have to double the string length? I would think that because I'm using CStringW it would be correct using file.Write( sOutStr, wcslen(sOutStr.GetBuffer()) );

Thanks!
Thanks for the help!
Aw, my fault - 'wcslen()' returns the amount of characters in a UNICODE string, yet a single character is two bytes, that's why.

The 'pedantically' correct way would be to

file.Write( sOutStr, wcslen(sOutStr.GetBuffer()) * sizeof(wchar_t) );

Open in new window