Menu

SaveFile erases file. Why?

Developer
2007-10-20
2013-05-20
  • maxinuruguay

    maxinuruguay - 2007-10-20

    Why does SaveFile erase the current XML file? Shouldn't it update the existing XML file instead totally rewriting it? If my program crashes, the xml file sometimes is completely blank. Is there any way of asking tinyxml to just update the file (avoiding fopen(w))? -max

     
    • Ellers

      Ellers - 2007-10-20

      *hmmmm*

      There's a few things to think about here.

      So, your first concern is that if, while saving the file, your application crashes, that the file will be blank. Well, I don't want to suggest anything too obvious, but... don't crash! Then the file won't be blank. If your app is crashing then the blank file should be the least of your worries.

      But lets consider the idea of "updating" a file. What exactly would you do? If all you did was to add nodes to the end of an XML file, then leaving the first part of the file would be possible. Jump to (say) byte 10000, overwrite from there, closing the XML again at the end. You can't do this with TinyXml, of course, but then you can't do it with any public XML toolkit that I know of (they may exist, but I've never heard of it).

      This kind of thing might be useful say, if your data is 100Mb long and you're always adding to the end and always at the end. But it would mean adapting the XML library of your choice to know which nodes have changed, and adding fancy writing logic, but it could be done.

      Now, as for "updating" a file in a more general sense, do you mean, say, jumping to byte 10000, inserting say 100  or 200 bytes (or whatever), then jumping ahead another (say) 15000 bytes and updating another node?

      File access, at least as far as I know it, doesn't operate that way. You can jump to a point and start writing, but nothing that I know of allows *inserting* at a point. And would you really truly want that? Again, for applications writing 100s of Mbs, maybe it might be worth it, but I don't think so in general.

      For normal applications - and TinyXml is fairly well targeted at "normal" applications - just write the whole file every time.

      A more reasonable alternative is a temp-write-swap approach, i.e. your normal file is called "config.xml", but when you save it you save to "tmp.config.xml"; at the successful completion of writing tmp.config.xml, you remove config.xml and rename tmp.config.xml=config.xml.

      HTH
      Ellers

       

Log in to post a comment.