-
January 20th, 2014, 04:06 AM
#1
Saving unicode strings to .txt files ..
Hi ..
In my project , we need to create an Array of Unicode Strings . The Array will contain 5000 Strings.
I need to write those strings to a text file which can be opened or edited with NotePad.
Normal _tfopen and fwrite are not able to create notepad compatible .txt file .. I mean the file I created is not
readable with Notepad though file open mode is "w+t"
How can I save my unicode strings to a text file
-
January 20th, 2014, 04:37 AM
#2
Re: Saving unicode strings to .txt files ..
You can use CStdioFile Class
or Win32 API functions CreateFile + WriteFile
Also consider inserting BOM in the beginning of the file.
Victor Nijegorodov
-
January 20th, 2014, 05:16 AM
#3
Re: Saving unicode strings to .txt files ..
Like Victor already did, I also suggest to use CStdioFile MFC class, that can make your life easier.
However, if really-really need using CRT functions and no MFC...
Notepad, as well as other text viewers/editors, uses BOM (byte order mark) to get the file text encoding.
You can write it yourself or, better add ccs flag to fopen/_wfopen.
Example
Code:
wchar_t* wchText = L"Ala bala portocala";
FILE* f = _wfopen(L"c:\\test\\mytest.txt", L"w+t,ccs=UNICODE");
if(NULL != f)
{
size_t size = fwrite(wchText, sizeof(wchar_t), wcslen(wchText), f);
// ...
fclose(f);
}
For more details, see Remarks under fopen, _wfopen MSDN topic.
-
January 20th, 2014, 07:42 AM
#4
Re: Saving unicode strings to .txt files ..
CStdioFile is a bad idea if you need to create a file with UNICODE contents because it will always enforce ansi-mode. You could find yourself in a situation working around MFC more than working with MFC.
either use CFile and do your own buffering and wide-string IO.
or use _wfopen as Ovidu suggests
-
January 20th, 2014, 09:08 AM
#5
Re: Saving unicode strings to .txt files ..
Victor Nijegorodov
-
January 20th, 2014, 10:48 AM
#6
Re: Saving unicode strings to .txt files ..
Originally Posted by VictorN
I found this issue has been resolved in VS2012.
CFile::OpenFlags enum contains a new value, typeUnicode. So, to save unicode, you can do the following:
Code:
// ...
UINT nOpenFlags = CFile::modeCreate | CFile::modeWrite;
#ifdef _UNICODE
nOpenFlags |= CFile::typeUnicode;
#endif
CStdioFile file(_T("d:\\test\\mytest.txt"), nOpenFlags);
file.WriteString(_T("Ala bala portocala\r\n"));
// ...
-
January 21st, 2014, 12:15 AM
#7
Re: Saving unicode strings to .txt files ..
Thanks thanks thanks to all of you ..
This site has supported me many times ..
I will read every thing carefully and will write down the program.
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
|