Ivan
Ivan

Reputation: 85

c++ pugiXML save_file not working as expected

I'm using pugiXML to write to XML.

This is demo:

//--open file
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(filePath.c_str());

//--add node
pugi::xml_node nodeEventLogger = doc.first_child();
pugi::xml_node nodeEvents = nodeEventLogger.first_child();
pugi::xml_node nodeEvent = nodeEvents.append_child("Event");
pugi::xml_node nodeEventIndex = nodeEvent.append_child("EventIndex");
int currentIndex = 1;
currentIndex = currentIndex + 1;
nodeEventIndex.append_child(pugi::node_pcdata).set_value(std::to_string(currentIndex).c_str());

//--save file
doc.save_file(filePath.c_str()); 

doc.save_file method deletes data from file before saving new one:

PUGI__FN bool xml_document::save_file(const char* path_, const char_t* indent, unsigned int flags, xml_encoding encoding) const
{
    using impl::auto_deleter; // MSVC7 workaround
    auto_deleter<FILE> file(fopen(path_, (flags & format_save_file_text) ? "w" : "wb"), impl::close_file);

    return impl::save_file_impl(*this, file.data, indent, flags, encoding);
}

At some moments file is empty (size 0kB). Another process is reading file periodically and sometimes it reads empty file.

How to prevent this? Am I using pugiXML open and save in the right way? Can I do some kind of lock on file to prevent file reading from another process? Another process is copiing file using SFTP on another machine.

Upvotes: 0

Views: 501

Answers (0)

Related Questions