I have data stored in a CString
and it needs to be parsed by an XML parser library. The problem is the XML parser takes in a CFile
. It's not ideal to write out the CString
to a text file and then reload it into a CFile
. Is there any way to directly send the CString
to the CFile
without making an intermediate output file?
views:
75answers:
1
+3
A:
You should be able to use CMemFile to accomplish this. It inherits from CFile
and allows you to specify an arbitrary buffer for data. The following sample code should work:
CString strData;
CMemFile memFile( (BYTE*)strData.GetBuffer() , (strData.GetLength() + 1) * sizeof(TCHAR) );
//Do something with memFile
strData.ReleaseBuffer();
flashk
2010-07-29 17:00:10
I tried your sample code, but the XML parser was unable to parse it due to a blank stream. Looking deeper into the XML parser it seems to be taking the `CFile` and putting it into a `CArchive`. I tried this: `CArchive archive( ` to make it an archive and sent the archive as a parameter since the function has an overloaded constructor that takes in a `CArchive`. Does this make a difference?
alex
2010-07-29 18:53:27
I tried putting the `CMemFile` object inside a `CArchive` object and reading data from the archive, and it worked fine. The `strData` object in my sample code is just a blank string, are you replacing it with a string that contains the actual XML data?
flashk
2010-07-29 19:16:17
Yes. I have `CString strData = blah.c_str();` with `blah` being the `<iostream>` string datatype containing the XML code.
alex
2010-07-29 19:19:32
That's strange. It's hard to tell what's going on without more information. Is the XML parser library freely available somewhere to look at?
flashk
2010-07-30 22:22:03
I'm actually editing the open-source program TinyCad ( http://sourceforge.net/projects/tinycad/ ). The XML parser doesn't look like a library available anywhere else. I tried their support forum, but haven't had much luck.
alex
2010-08-03 16:22:06