Reputation: 1044
I am trying to serialize a list of objects.Objects themselves contain only CString members. The serialization is done to a text file in order to create human-readable log file. The issue is that in order to write Unicode strings, the file is needed to be have BOM(byte order mark) for a Unicode encoding.
FILE *fStream = NULL;
VERIFY( _tfopen_s( &fStream, _T( "D:\\Test.txt" ), _T( "wt,ccs=UNICODE" ) ) == 0 );
CStdioFile theFile;
theFile.m_pStream = fStream;
CArchive archive( &theFile, CArchive::store );
ListContainingObject.Serialize( archive );
In Serialize()
I am trying to write the number of the elements so CArchive::WriteCount()
is called with the size of the list.
This messes up the BOM and if the file is opened in Notepad for example,
-
sign is showed at the beggining of the file.
In HEX viewer the file header looks like this: ff fe 06 00
.
I understand that 06 is the capacity of the list that i have written.I also understand that -
has probably the ASCII value of ff fe 06 00
The question is:
Is there any way in which i can write down an integer to the beginning of the file and not mess it up with the byte order mark so no symbol is shown in text editors?
Upvotes: 1
Views: 745
Reputation: 6040
Oh, it's all going to be messed up. If you serialize CString objects, they are also going to write length counts as is the list.
If all you have is a list of strings, then convert the count to a string usingw wsprintf() or CString::Format()... Write the count as a string and terminate with a newline ("\n"). Write all the remaining strings as strings terminated with a newline character. If your strings have newlines in them, well, you are going to have to escape it somehow. You could put your data in some XML format. You have lots of choices.
Upvotes: 2