Serialization & Reading/Write Multiple Lines :: MFC

Status
Not open for further replies.
Hello.

My intention is to read each line from a file and save it into a STL list of CString. So if there are 10 lines in the file, then the linked list of CString will be of size ten. Prosise demonstrates a very effective way to read and write from and to a file linewise using the CStdioFile class. However, in doc/view, MFC turns to Serialization. The problem, to me, with Serialization is I do not know what is going on in the background. I do not know what the framework does to the data in terms of CArchive.

When writing to CArchive, I use this:

for (iter = list.begin(); iter != list.end(); ++iter)
ar << *iter;

I am not sure how to read data from CArchive back into the linked list. First, I do not know whether data is stored similar to a file, i.e. if you write line1 and line2, then you can read back line1 and line2 in its original form.

How should I go about read data from CArchive back into the linked list if I have no knowledge of how CArchive manages the orientation of the data?

For example let say that the linked list of CString has this:

-----
a
b
c
d
e
-----

Okay. When it stores the data into CArchive, I assumpt it stores it like this:

<- a <- b <- c <- d <-e

However, if I have no prior knowledge of what was in the CArchive, how do I go about saving the data from CArchive back into the linked list?

Thanks,
Kuphryn

Note: If nothing works, I think the technique Prosise shows is best since class CStdioFile features ReadString() and WriteString().
 
Thanks.

I am considering going with going the manual route and use CStdioFile class. I thought since the program is an doc/view architecture, I should implement it with serialization.

AlexMarbus of Code Project brought up a very good point. He mentioned the CStringList class. I will looking into that class. Maybe I do not need a linked list of CString. The program can use a CStringList instead since it has its own serialization.

Kuphryn
 
Wheter or not you can use serialization depends on one thing: do you have complete control of the file?

If you create the file, are the only one to use it, and its internal format doesn't matter, then serialization is your friend.

Otherwise, go the manual way. Keep in mind that even serializing a string list will probably not result in a string on every line like you seem to want. The string class might support multiline strings, which will require a different encoding.
 
Status
Not open for further replies.