两行Xerces程序中的异常 [英] Exception in two line Xerces program
问题描述
以下代码在 XMLFormatTarget
行上给了我一个例外,但是如果我将字符串从"C:/test.xml"
更改为"test.xml"
正常工作.
The following code gives me an exception on the XMLFormatTarget
line, but if I change the string from "C:/test.xml"
to "test.xml"
it works fine.
// 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例外是:
[edit] Xerces exception is:
错误信息:无法打开文件'C:\ test.xml'
Error Message: unable to open file 'C:\test.xml'
Windows例外是:
Windows exception is:
访问被拒绝
推荐答案
可能是您没有足够的权限写入 C:\
.在这种情况下,Xerces可能会报告引发异常的错误.
It could be that you don't have sufficient permissions to write to C:\
. In such a case, Xerces might report the error throwing an exception.
如果您尝试在没有管理员凭据的情况下写入系统目录,通常会发生 Access Denied
例外.
An Access Denied
exception is typically what we could expect if you try to write to a system directory without administrator credentials.
也许与目录分隔符也有关系:
Maybe it has also something to do with the directory separators:
XMLFormatTarget *formatTarget = new LocalFileFormatTarget("C:\\test.xml");
在Windows上,目录分隔符为反斜杠"\".有些库不在乎(而且我从没使用过Xerces,所以我不知道).在 C
和 C ++
中,反斜杠也是转义字符,因此,如果您想要乱扔垃圾,则必须 double 字符串中的"\".
On Windows, directory separators are backslashes "\". Some libraries don't care (and I never used Xerces, so I can't tell). In C
and C++
, backslash is also an escape character and so you must double it if you want a litteral "\" in your string.
另外,告诉我们您所获得的例外情况将对我们有更大的帮助.
Also, telling us what was the exception you got would help us even more.
没有直接关系,但是从您的代码看来,您从未删除
formatTarget
.我假设这是示例代码,但如果不是,则应在代码中添加以下行:
Not directly related, but from your code, it seems you never delete
formatTarget
. I assume this is sample code, but if it is not, you should add the following line to your code:
delete formatTarget;
或改用作用域指针:
boost::scoped_ptr<XMLFormatTarget> formatTarget(new LocalFileFormatTarget("C:\\test.xml"));
为避免内存泄漏.
这篇关于两行Xerces程序中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!