Unable to write in file the \n in unicode

I am trying to convert from ansi to unicode using the MultibyteToWideChar.
The problem is that when a new line comes the (function or the program) is unable to translate the 0d0a into the 0d00 0a00.
So i tried to manually add the new line but again deadin. The compiler was translating 0a as 0d0ad so i couldn't just write 0d00 0a00.(And there i lost every hope)

The code:

1
2
3
4
5
6
7
8
9

void writeUnicode(FILE *pFile, char *buffer)
{  
   int x=MultiByteToWideChar(0, 0, buffer, -1, NULL, 0);
   wchar_t uniBuff[x];
   MultiByteToWideChar(0, 0, buffer, x-1, uniBuff, x-1); //x-1 because i don't want the null
   fwrite (uniBuff , 2, x-1, pFile );
}
Last edited on
The compiler was translating 0a as 0d0ad
That's not the compiler, it's the runtime. It's a language feature. When you open a file for text output (i.e. the default mode), 10 is translated to the operating system's default newline sequence, regardless of context. While this does have its uses, more often than not it's just annoying. If you need to conserve exact binary images, you'll have to open the file as binary.
http://www.cplusplus.com/reference/clibrary/cstdio/fopen/

I myself wasted ten minutes yesterday debugging a file that was three kilobytes longer than it should until I realized I was opening the file as text output. If you ask me, it was a mistake on the part of K&R to make text the default mode.
Last edited on
are you on windows or linux/unix?
are you on windows or linux/unix?
MultiByteToWideChar()

Gee, I wonder.
i dont know if this exist on unix. :P

if thats windows, making the application unicode will do everything. Instead of MBCS, use UNICODE and _UNICODE.
Sigh... No it won't. That just makes TCHAR the same as WCHAR, rather than CHAR. In other words, it controls the type functions that take strings will receive.
what about this:


1
2
3
4
5
6
	FILE *fout = _wfopen(L"out.txt", L"w");
	wchar_t		wchar[100];
	memset(wchar, 0, 100);
	wcscpy(wchar,L"This is unicode\n");
	fwrite(wchar, sizeof(wchar_t), wcslen(wchar),fout);	
	fclose(fout);
The file is still being opened as text. Didn't you read what I said before?
Besides, the wide versions of file operations aren't really that useful. Also, I'm pretty sure you're suppposed to use wfwrite() and wfclose().
solved, opening file in binary worked.
Thanks a lot
Topic archived. No new replies allowed.