This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
Hi Camilla, > I am writing a C++ program to read the data in a netcdf file. In the > script, I use 'Ncfile' to read the .nc file. However, the compiler said > that the function 'Ncfile' was not found. Would you please give me some > advice? > > ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; > > using namespace std; > #include <iostream> // This is a key C++ library > #include "netcdfcpp.h" > > string dir ("G:/result/"); > string code ("us0000"); > const string path = dir + code; > const string fil = path + "/" + "wrfout_" + code + ".nc"; > > int main () > { > NcFile file(fil, NcFile::ReadOnly); > if ( !file->is_valid() ) { > delete fil; > exit(1); > } > NcVar* var = fil->get_var("P"); > if(var) > printf("variable's name is %s/n", var->name()); > system("pause"); > return 0; > } Yes, the netCDF C++ library was developed in the mid-1990's and is showing its age (no use of Exceptions, Templates, or namespaces, for example). The netCDF Java API is much more up-to-date. An experimental new C++ API for netCDF-4, contributed by Lynton Appel, is available in the 4.1.1 release and is built if you configure with --enable-cxx4, but it still needs a little more work to support netCDF classic files. The current C++ API is just a thin wrapper layer on top of the C library, so in particular, it takes C char* arguments instead of C++ string arguments where character strings are required. So if you change all the string declarations to the corresponding char* or char[] declarations, things should work: using namespace std; #include <iostream> // This is a key C++ library #include "netcdfcpp.h" #include <strings.h> char dir[] = "G:/result/"; char code[] = "us0000"; const char *path = strcat(dir, code); char fil[NC_MAX_NAME]; int main () { sprintf(fil, "%s/wrfout_%s.nc", path, code); NcFile file(fil, NcFile::ReadOnly); if ( !file.is_valid() ) { delete fil; exit(1); } NcVar* var = file.get_var("P"); if(var) printf("variable's name is %s/n", var->name()); system("pause"); return 0; } --Russ Russ Rew UCAR Unidata Program address@hidden http://www.unidata.ucar.edu Ticket Details =================== Ticket ID: ZDR-128646 Department: Support netCDF Priority: Normal Status: Closed