下面的代码给了我一个例外XMLFormatTarget
行,但如果我更改字符串"C:/test.xml"
to "test.xml"
效果很好。
// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/framework/LocalFileFormatTarget.hpp>
using namespace xercesc;
int main()
{
XMLPlatformUtils::Initialize();
XMLFormatTarget *formatTarget = new LocalFileFormatTarget("C:/test.xml");
return 0;
}
[编辑]
Xerces 例外是:
错误消息:无法打开文件
'C:\test.xml'
Windows 例外是:
访问被拒绝
可能是您没有足够的权限来写入C:\
。在这种情况下,Xerces 可能会报告错误并引发异常。
An Access Denied
如果您尝试在没有管理员凭据的情况下写入系统目录,则通常会出现异常。
也许它也与目录分隔符有关:
XMLFormatTarget *formatTarget = new LocalFileFormatTarget("C:\\test.xml");
在 Windows 上,目录分隔符是反斜杠“\”。有些库不在乎(而且我从未使用过 Xerces,所以我不知道)。在C
and C++
, 反斜杠也是一个转义字符所以你必须double如果您想在字符串中使用文字“\”,请使用它。
另外,告诉我们您遇到的例外情况会对我们有更多帮助。
没有直接关系,但从你的代码来看,你似乎从来没有delete
formatTarget
。我假设这是示例代码,但如果不是,您应该将以下行添加到您的代码中:
delete formatTarget;
或者使用一个作用域指针反而:
boost::scoped_ptr<XMLFormatTarget> formatTarget(new LocalFileFormatTarget("C:\\test.xml"));
以避免内存泄漏。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)