Menu

Closing a File

Developer
Anonymous
2007-07-05
2013-05-20
  • Anonymous

    Anonymous - 2007-07-05

    I do not see any method related to Closing XML files. The only way I can see how is to open a new file the old file is then closed. I shouldn't have to do this.
    How to close?

     
    • Ellers

      Ellers - 2007-07-07

      Hmm, if I am not mistaken, TinyXml expects file opening/closing to be done by the caller.

      Or I may be misunderstanding you?

       
    • Anonymous

      Anonymous - 2007-07-08

      LoadFile(const char*....

      There is no CloseFile()... In fact LoadFile opens the file using fopen and a FILE* pointer and it's never kept in for later use. So why have that function at all if it's only goign to cause a file to remain open the entire duration of the program?

      I have now been attempting to open my own file my own way (CreateFile()) and the only bad part is trying to get output text for the XML. TiXmlPrinter seems to cause issues, so until I fix saving to files, this isn't going to work.

       
      • Nicola Civran

        Nicola Civran - 2007-07-10

        I'm sorry, but I don't understand: the FILE* created by LoadFile(const char*) is closed three line after...
        So, why do you state that it "cause a file to remain open the entire duration of the program"?

         
    • Anonymous

      Anonymous - 2007-07-08

      TiXmlPrinter seems to be ok, I think.
      The data in the file gets duplicated when the file is opened while notepad also has it open. It's sort of strange, I think notepad somehow causes the file pointer to think the end of the file is the beginning.

       
    • Anonymous

      Anonymous - 2007-07-08

      Fixed all of that.

      I use platform SDK, ::CreateFile then I read all of the text using ::ReadFile (You have to ensure there is a \n character at the end of the file or it wont parse right.) then Parse with TiXml. I keep the file open if I'm using it as a project document so other applications can't mess around with it. I use ::CloseHandle when the file is closed.

      I did run into a problem with duplicated data when a 2nd, 3rd, + file was opened. This is because TiXmlDocument::Clear() doesn't actually clear the document! What's it do? I dont know. I allocate a document when opening a file using new then I deallocate that when the file is closed. That seems to sovle all of those problems I had.

      I wrote a wrapper around TinyXML that's a lot easier to code. Instead of using trees of code to find your elements & attributes its more like:

      string strValue = GetElementAttribute("ROOTELEMENT\\MYELEMENT", 0 /*Index*/);

      It's easier and faster to write out. Only one downside that hardly effects me much is that root elements specified in the path there are always goign to be the first ones if there are other elements with the same name. The target element u can pick a duplicate index.

       
    • Anonymous

      Anonymous - 2007-07-11

      Sorry, when I originally looked at the source it led me to
      bool TiXmlDocument::LoadFile( FILE* file, TiXmlEncoding encoding )
      which doesn't close the file.

      Fortunatly I'm happy with the change in the way I deal with the file. So not all of my time was lost. :-)

       

Log in to post a comment.