views:

75

answers:

1

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?

+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
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
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
Yes. I have `CString strData = blah.c_str();` with `blah` being the `<iostream>` string datatype containing the XML code.
alex
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
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