NOTICE: This version of the NSF Unidata web site (archive.unidata.ucar.edu) is no longer being updated.
Current content can be found at unidata.ucar.edu.

To learn about what's going on, see About the Archive Site.

Re: [netcdfgroup] NcFile close() member function?

Thank you (and thank you Lynton!) for your help. I have a slightly
different methodology (I've not included the class that I defined that make
this actually work, but you get the idea):

#include <iostream>
#include <ctime>
#include <string>
#include <sstream>
#include <netcdf>

using namespace std;
using namespace netCDF;
using namespace netCDF::exceptions;

int main()
{
  /* Instance of an OPeNDAP URL based on a class I've defined*/
   dap_url dapfile;

   for (int k = 0; k < 10; k++) {

     /* dap_url class contains a get function that returns a URL string*/
     NcFile dataFile(dapfile.get(), NcFile::read);

     /* Do something with the file */

     /* dap_url class contains a method to increment, so that the next
     call to the get() method returns a different file */
     dapfile.addtoday(1);
   }
   return 0;
}

...but it's nice to know that the destructor is called at the end of each
loop.
Taylor


On Wed, Jan 30, 2013 at 1:18 PM, Russ Rew <russ@xxxxxxxxxxxxxxxx> wrote:

> Taylor,
>
> > To finish up with my question, if I can not manually close a file, if I
> > open a NEW file with the same handle 'ncfile' (imagine the small code
> > snippet that I included earlier being inside a loop ), will this
> deallocate
> > the memory used by the earlier instance? I'd like to open (and close)
> > several files in sequence and would like to minimize memory usage.
>
> Here's the response from Lynton Appel:
>
> ... The following will loop through a a set of files. At each pass
> through the loop, the destructor functions for NcDim and NcFile are
> called; in particular the NcFile destructor will issue a call to
> nc_close. So, in my view the API is working correctly.
>
> #include <string>
> #include <iostream>
> #include <netcdf>
>
> using namespace std;
> using namespace netCDF;
>
>
> int main(int argc, char* argv[])
> {
>   char* fileName[] = {"efitOut.nc","efitOut2.nc"};
>  int i;
>   for(i=0;i<2;i++)
>     {
>       NcFile ncFile(fileName[i],NcFile::replace);
>       NcDim timeDim(ncFile.addDim("time",2));
>     }
>   return 1;
> }
>
> --Russ
>



-- 
Taylor Binnington
e. tbinnington@xxxxxxxxx
c. 647 926 4144
  • 2013 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdfgroup archives: