views:

125

answers:

3

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;
}

[edit] Xerces exception is:

Error Message: unable to open file 'C:\test.xml'

Windows exception is:

Access is denied

+1  A: 

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.

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");

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.


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;

Or use a scoped pointer instead:

boost::scoped_ptr<XMLFormatTarget> formatTarget(new LocalFileFormatTarget("C:\\test.xml"));

To avoid memory leaks.

ereOn
I don't knwo Xerxes either, but what's wrong with `LocalFileFormatTarget formatTarget("C:\\test.xml");`?
sbi
Thanks for your reply ereOn, i still get an exception with "C:\\test.xml". I am aware formatTarget needs to be freed, i just wanted to post the least possible amount of code to reproduce the problem. Ill post back with exception.
Gungho
It's not that "some libraries" don't care, WinAPI itself don't care and for the most part you can use \ and / interchangeably on Windows.
sbk
@sbi: There is nothing wrong with the double backslashes, that's my point. Perhaps you misread the question or my answer ? @sbk: I've seen many libraries that *do* care. Maybe those libraries are badly-designed, but that's another topic. Anyway, since I don't know Xerces, I can't tell for sure.
ereOn
Updated original question with exception.
Gungho
@Gungho: I already had updated my answer with another possibility ;) And it seems I was right: you probably have not enough permissions to write directly under `C:\`.
ereOn
Yes! it was a problem writing to C:/ under Windows Vista, no problems writing to other paths, or writing to C:/ under Windows XP. Thanks for your help.
Gungho
A: 

If you use only test.xml you specify a path relative to the current working directory (usually where the program was started from). So if your program is not directly on your C: drive, the two runs could point to different files. The C:\test.xml could have an error, but C:\Path\to\your\program\test.xml correct, so the latter gives you no exception.

Anyway, as ereOn said, it would help if we knew which exception is thrown.

king_nak
Thanks for your reply king_nak, the file test.xml does not exist for either the working or non working example. this bit of code is a part of a program that actually creates test.xml, and populates it.
Gungho
+1  A: 

Try transcoding the filename:

// Convert the path into Xerces compatible XMLCh*. 
XMLCh *tempFilePath = XMLString::transcode(filePath.c_str()); 

// Specify the target for the XML output. 
XMLFormatTarget *formatTarget = new LocalFileFormatTarget(tempFilePath);

as per this answer to a similar question.

jon hanson