将无符号短数组作为文本写入文件 [英] Write unsigned short array as text to a file
本文介绍了将无符号短数组作为文本写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有无符号短* 数组,我需要将该数组中的值写入 xml 文件。
我不知道怎么.... :-(
请帮忙......
解决方案
Vineeth -
你应该可以使用类似的东西:// WriteXml.cpp:定义控制台应用程序的入口点。
//
// 来自http://msdn.microsoft.com/en-us/library/windows/desktop/ms757870(v = vs.85).aspx
< span class =code-keyword> #include stdafx.h
#include < objbase.h >
<跨越ss =code-keyword> #include < tchar.h >
#include < msxml6.h >
#include < sstream >
#include < string >
使用 namespace std;
// 调用COM方法返回HRESULT值的宏。
#define CHK_HR(stmt)do {hr =(stmt); if(FAILED(hr))goto CleanUp; } while(0)
// 验证内存分配的宏。
#define CHK_ALLOC(p)do {if(!(p)){hr = E_OUTOFMEMORY; goto CleanUp; while(0)
// 释放COM对象的宏如果不是NULL。
#define SAFE_RELEASE(p)do {if((p)){(p) - > Release(); (p)= NULL; while(0)
// 帮助创建DOM的函数实例。
HRESULT CreateAndInitDOM(IXMLDOMDocument ** ppDoc)
{
HRESULT hr = CoCreateInstance( __ uuidof (DOMDocument60),NULL ,CLSCTX_INPROC_SERVER,IID_PPV_ARGS(ppDoc));
if (SUCCEEDED(hr))
{
// 这些方法不应该失败,所以不要检查结果
(* ppDoc) - > put_async(VARIANT_FALSE);
(* ppDoc) - > put_validateOnParse(VARIANT_FALSE);
(* ppDoc) - > put_resolveExternals(VARIANT_FALSE);
}
return hr;
}
// 从null终止创建VT_BSTR变体的Helper函数串。
HRESULT VariantFromString(PCWSTR wszValue,VARIANT& Variant)
{
HRESULT hr = S_OK;
BSTR bstrString = SysAllocString(wszValue);
CHK_ALLOC(bstrString);
V_VT(& Variant)= VT_BSTR;
V_BSTR(& Variant)= bstrString;
CleanUp:
return hr;
}
// 来自http://stackoverflow.com/questions/ 6284524 / bstr-to-stdstring-stdwstring-anti-versa
BSTR ConvertMBSToBSTR( const std :: string& str)
{
int wslen = :: MultiByteToWideChar(CP_ACP, 0 / * 无标记* / ,
str.data(),str.length(),
NULL, 0 );
BSTR wsdata = :: SysAllocStringLen(NULL,wslen);
:: MultiByteToWideChar(CP_ACP, 0 / * 没有标志* / ,
str.data(),str.length(),
wsdata,wslen);
return wsdata;
}
void saveDOM()
{
HRESULT hr = S_OK;
IXMLDOMDocument * pXMLDom = NULL;
IXMLDOMParseError * pXMLErr = NULL;
BSTR bstrXML = NULL;
BSTR bstrErr = NULL;
VARIANT_BOOL varStatus;
VARIANT varFileName;
string xml = < r> \ n;
VariantInit(& varFileName);
CHK_HR(CreateAndInitDOM(& pXMLDom));
// 创建一个10字节的数组
BYTE短裤[ 10 ];
for ( int i = 0 ; i< 10 ; i ++){
shorts [i] =(BYTE)i;
}
// 我们是什么用于将我们的字节转换为字符串
// 来自http://www.cplusplus。 com / articles / D9j2Nwbp /
// 我们只需翻译我们的无符号短裤数组发短信
for ( int i = 0 ; i< 10 ; i ++){
ostringstream convert;
xml + = < a> \ n;
convert<< ( int )shorts [i];
xml + = convert.str();
xml + = < / a> \ n;
}
// 关闭我们的xml strring
xml + = < / r>;
bstrXML = ConvertMBSTBSBS(xml);
CHK_ALLOC(bstrXML);
CHK_HR(pXMLDom-> loadXML(bstrXML,& varStatus));
if (varStatus == VARIANT_TRUE)
{
CHK_HR(pXMLDom-> get_xml(& bstrXML) );
printf( 从应用程序加载的XML DOM:\ n%S \ n ,bstrXML);
VariantFromString(L myData.xml,varFileName);
CHK_HR(pXMLDom-> save(varFileName));
printf( XML DOM保存到myData.xml \ n);
}
其他
{
// < span class =code-comment>无法加载xml,得到最后一次解析错误
CHK_HR(pXMLDom-> get_parseError(& pXMLErr));
CHK_HR(pXMLErr-> get_reason(& bstrErr));
printf( 无法从xml字符串加载DOM。%S \ n, bstrErr);
}
清理:
SAFE_RELEASE(pXMLDom);
SAFE_RELEASE(pXMLErr);
SysFreeString(bstrXML);
SysFreeString(bstrErr);
VariantClear(& varFileName);
}
int _tmain( int argc,_TCHAR * argv [])
{
HRESULT hr = CoInitialize(NULL);
if (SUCCEEDED(hr))
{
saveDOM();
CoUninitialize();
}
return 0 ;
}
< / string>< / sstream>< /msxml6.h>< /tchar.h>< /objbase.h>
Hi,
I have a unsigned short* array and i need to write the values inside that array to an xml file.
I don''t know how....:-(
Please help...
解决方案
Vineeth -
You should be able to use something like:// WriteXml.cpp : Defines the entry point for the console application. // // from http://msdn.microsoft.com/en-us/library/windows/desktop/ms757870(v=vs.85).aspx #include "stdafx.h" #include <objbase.h> #include <tchar.h> #include <msxml6.h> #include <sstream> #include <string> using namespace std; // Macro that calls a COM method returning HRESULT value. #define CHK_HR(stmt) do{ hr=(stmt); if (FAILED(hr)) goto CleanUp; } while(0) // Macro to verify memory allcation. #define CHK_ALLOC(p) do { if (!(p)) { hr = E_OUTOFMEMORY; goto CleanUp; } } while(0) // Macro that releases a COM object if not NULL. #define SAFE_RELEASE(p) do { if ((p)) { (p)->Release(); (p) = NULL; } } while(0) // Helper function to create a DOM instance. HRESULT CreateAndInitDOM(IXMLDOMDocument **ppDoc) { HRESULT hr = CoCreateInstance(__uuidof(DOMDocument60), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(ppDoc)); if (SUCCEEDED(hr)) { // these methods should not fail so don't inspect result (*ppDoc)->put_async(VARIANT_FALSE); (*ppDoc)->put_validateOnParse(VARIANT_FALSE); (*ppDoc)->put_resolveExternals(VARIANT_FALSE); } return hr; } // Helper function to create a VT_BSTR variant from a null terminated string. HRESULT VariantFromString(PCWSTR wszValue, VARIANT &Variant) { HRESULT hr = S_OK; BSTR bstrString = SysAllocString(wszValue); CHK_ALLOC(bstrString); V_VT(&Variant) = VT_BSTR; V_BSTR(&Variant) = bstrString; CleanUp: return hr; } //from http://stackoverflow.com/questions/6284524/bstr-to-stdstring-stdwstring-and-vice-versa BSTR ConvertMBSToBSTR(const std::string& str) { int wslen = ::MultiByteToWideChar(CP_ACP, 0 /* no flags */, str.data(), str.length(), NULL, 0); BSTR wsdata = ::SysAllocStringLen(NULL, wslen); ::MultiByteToWideChar(CP_ACP, 0 /* no flags */, str.data(), str.length(), wsdata, wslen); return wsdata; } void saveDOM() { HRESULT hr = S_OK; IXMLDOMDocument *pXMLDom=NULL; IXMLDOMParseError *pXMLErr = NULL; BSTR bstrXML = NULL; BSTR bstrErr = NULL; VARIANT_BOOL varStatus; VARIANT varFileName; string xml = "<r>\n"; VariantInit(&varFileName); CHK_HR(CreateAndInitDOM(&pXMLDom)); // create an array of 10 bytes BYTE shorts[10]; for(int i = 0; i < 10; i++) { shorts[i] = (BYTE)i; } // what we are using to convert our bytes to string // from http://www.cplusplus.com/articles/D9j2Nwbp/ // we just have to translate our array of unsigned shorts to text for(int i = 0; i < 10; i++) { ostringstream convert; xml += "<a>\n"; convert << (int)shorts[i]; xml += convert.str(); xml += "</a>\n"; } // close our xml strring xml += "</r>"; bstrXML = ConvertMBSToBSTR(xml); CHK_ALLOC(bstrXML); CHK_HR(pXMLDom->loadXML(bstrXML, &varStatus)); if (varStatus == VARIANT_TRUE) { CHK_HR(pXMLDom->get_xml(&bstrXML)); printf("XML DOM loaded from app:\n%S\n", bstrXML); VariantFromString(L"myData.xml", varFileName); CHK_HR(pXMLDom->save(varFileName)); printf("XML DOM saved to myData.xml\n"); } else { // Failed to load xml, get last parsing error CHK_HR(pXMLDom->get_parseError(&pXMLErr)); CHK_HR(pXMLErr->get_reason(&bstrErr)); printf("Failed to load DOM from xml string. %S\n", bstrErr); } CleanUp: SAFE_RELEASE(pXMLDom); SAFE_RELEASE(pXMLErr); SysFreeString(bstrXML); SysFreeString(bstrErr); VariantClear(&varFileName); } int _tmain(int argc, _TCHAR* argv[]) { HRESULT hr = CoInitialize(NULL); if(SUCCEEDED(hr)) { saveDOM(); CoUninitialize(); } return 0; } </string></sstream></msxml6.h></tchar.h></objbase.h>
这篇关于将无符号短数组作为文本写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文