I am trying to develop a class for reading and writing files. There are two ways for strings: ANSI and Unicode. ANSI is working fine, but something is wrong with my Unicode.
it's a bit connected and I can read Unicode files just fine, I mean, without checking or skipping "0xFEFF". it works no matter what language i am in (i have tried english, chinese and japanese). is there anything i should know about?
then the biggest problem popped up: write Unicode lines to a file. At first I tried plain English as an alphabet without the "\ n" character, it worked perfectly. then I press "\ n" and everything starts to go wrong: the output is inserted with many spaces like "abcdefg \ nh i jklmn \ nopqrst \ nuvwxyz" ('\ n' works, but there are so many spaces) and the file is ANSI again . don't ask characters in other languages, I can't even read them.
So, here is the question: what should I do to correctly write Unicode lines to a file and how? do not mention the function "_wopen", the file is already open with the function "fopen".
Answers and recommendations will be so appreciated.
I am using windows 7 and visual studio.
Edit: it works for non-English characters with the following code, but still wrong with '\ n'.
char* cStart = "\xff\xfe"; if (::ftell(m_pFile) == 0) ::fwrite(cStart, sizeof(wchar_t), 1, m_pFile);
but how does it work? I mean, I did not see this while I was reading the file.
Edit: part of my code.
void File::ReadWText(wchar_t* pString, uint32 uLength) { wchar_t cLetter = L'\0'; uint32 uIndex = 0; do { cLetter = L'\0'; ::fread(&cLetter, sizeof(wchar_t), 1, m_pFile); pString[uIndex] = cLetter; }while (cLetter != L'\0' && !::feof(m_pFile) && uIndex++ < uLength); pString[uIndex] = L'\0'; } void File::WriteWText(wchar_t* pString, uint32 uLength) { char* pStart = "\xff\xfe"; if (::ftell(m_pFile) == 0) ::fwrite(pStart, sizeof(wchar_t), 1, m_pFile); m_uSize += sizeof(wchar_t) * ::fwrite(pString, sizeof(wchar_t), uLength, m_pFile); } void main() { ::File* pFile = new File(); wchar_t* pWString = L"abcdefg\nhijklmn\nopqrst\nuvwxyz"; pFile->Open("TextW.txt", File::Output);
Output file content: "abcdefg 栀 椀 樀 欀 氀 洀 渀 渀 ഀ ഀ 甀 甀 瘀 眀 砀 礀 稀", Unicode file.
I don’t know if this is the correct expression "L" \ n ", I have never worked with Unicode before. Thanks for helping me :)