NO

Author Topic: Unicode Read/Write File SOLVED  (Read 16897 times)

Juni

  • Guest
Re: Unicode Read/Write File
« Reply #15 on: August 28, 2010, 05:35:56 PM »
yes

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Unicode Read/Write File
« Reply #16 on: August 28, 2010, 06:07:51 PM »
OK.
So check this.
May the source be with you

Juni

  • Guest
Re: Unicode Read/Write File
« Reply #17 on: August 28, 2010, 06:11:47 PM »
results in this

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Unicode Read/Write File
« Reply #18 on: August 28, 2010, 06:21:10 PM »
Try this instead because of Intel:
TCHAR szTest[] = {0xFFFE, 0x4F60, 0x597D};

May the source be with you

Juni

  • Guest
Re: Unicode Read/Write File
« Reply #19 on: August 28, 2010, 06:27:28 PM »
That works,

thank you timovjl but i already have that in my demo,
the problem is the file i/o
and it seems Vista and up work different.

The load-function seems to work now (i updated demo.zip for download)
i just have to get rid of the BOM somehow wich seems to be different after reading ><

The save-function i don t get to work so far..

Thank you  :)
« Last Edit: August 30, 2010, 01:07:32 PM by Juni »

CommonTater

  • Guest
Re: Unicode Read/Write File
« Reply #20 on: August 28, 2010, 10:18:17 PM »
Ok... does the attached project work for you?

I just cobbled this together in a couple of minutes. The cut, copy, paste etc when you right click is built right into the EDIT control.  It may be a bit buggy.... but it does read and write unicode files correctly.



« Last Edit: August 28, 2010, 10:29:29 PM by CommonTater »

Juni

  • Guest
Re: Unicode Read/Write File
« Reply #21 on: August 28, 2010, 10:47:30 PM »
CommenTator thanks you really much  :)
Your example works perfectly.

I reached to handle the BOM a bit in meantime :
Code: [Select]
w1 = fgetwc( fIn );
w2 = fgetwc( fIn );

// Check for utf16 BOM
if ( w1 ==  0xFF && w2 == 0xFE )
{
fgetws( wsInBuffer ,nWords , fIn ); // terminates with 0

WideCharToMultiByte ( CP_UTF8, // UTF8 =>  lpDefaultChar and lpUsedDefaultChar must be set to NULL
0, // must be 0 for CP_UTF8
wsInBuffer, // Pointer to the Unicode string to convert
nWords, // can be set set to -1 if the string is null-terminated
wsMbBuffer, // Target
100,
NULL, NULL ); // For the CP_UTF8 setting for CodePage must be set to NULL

SetDlgItemText ( hwnd, IDC_TEXT, (LPCWSTR) wsMbBuffer );

I m not sure if to continue trying for BOM
or if to leave that part out
because Windows Editor itself uses it..

I think for compatibility (i.e. Linux)
it should be worth it ?

Scared a bit to produce tons of .txt
wich can t be handled later cause of missing file identifier ><

Again thank you all for the help !
« Last Edit: August 30, 2010, 01:08:19 PM by Juni »

CommonTater

  • Guest
Re: Unicode Read/Write File
« Reply #22 on: August 28, 2010, 11:22:20 PM »
CommenTator thanks you really much  :)
Your example works perfectly.

I reached to handle the BOM a bit in meantime :
That part's real easy...

In the file save function when you have the file open, just do two writes... write the identifier to the head of the file then write the text...  WriteFile (identifier);  WriteFile(text).  

In the file load section do two reads... The first to get the file id, the second to read the text.

Deal with the file ID and any required parsing on the memory buffer itself before you inject the text into the edit control.

In the mean time, have a close look at how I did things... there may be better ways, of course, but that's pretty much how windows programs are written.



« Last Edit: August 28, 2010, 11:25:33 PM by CommonTater »

CommonTater

  • Guest
Re: Unicode Read/Write File
« Reply #23 on: August 29, 2010, 12:03:50 AM »
Ok... last kick at the can then you're on your own...

Is the attached what you are looking for?

I've added a quicky code to flip the msB and lsB of each character in the file...
Look in the open and close functions, I spaced them out for you.

Be Warned though... if you do actually flip the bytes in a windows text file, it's going to display as garbage.

Juni

  • Guest
Re: Unicode Read/Write File
« Reply #24 on: August 29, 2010, 12:18:13 AM »
Quote
That part's real easy...

In the file save function when you have the file open, just do two writes... write the identifier to the head of the file then write the text...  WriteFile (identifier);  WriteFile(text).  

In the file load section do two reads... The first to get the file id, the second to read the text.

Yes now it seems really easy :)

Quote
In the mean time, have a close look at how I did things... there may be better ways, of course, but that's pretty much how windows programs are written.

I will thanks a lot for all the work !

Quote
I've added a quicky code to flip the msB and lsB of each character in the file...
Look in the open and close functions, I spaced them out for you.

That will help a lot to open different coded files,
you showed me the way to go thanks a lot :)




Updated demo.zip attached to first post
« Last Edit: August 30, 2010, 12:59:26 PM by Juni »