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.
>From: "Timothy E. Dowling" <address@hidden> >Subject: netcdf nc_put_att_text bug? >Organization: ? >Keywords: 200007101335.e6ADZPT10374 netCDF nc_put_att_text > I notice that nc_put_att_text(...,len,input string) will > read past the end of the input string if it is shorter than > the given len, picking up garbage that it then sends as part > of the text attribute. > > I think the correct behavior is that it should stop at the > first '\0' in the input string, and leave the rest of the > text attribute blank or full of '\0's. You are making the (reasonable) assumption that netCDF deals with character array attributes in the form of C strings, properly terminated with a null '\0' character. However netCDF files are language-neutral, so Fortran programs can read the files too. There is only a null character terminating a string attribute if you put it there when you wrote it, and specified the attribute length (number of characters) to include the extra null character. NetCDF permits creating character attributes containing zero or more null bytes, since some of the languages that read and write netCDF files have no special interpretation for a null character (e.g. Fortran). Hence it's necessary to use something like nc_put_att_text(..., strlen(input_string)+1 ,input_string) if you want to include the null byte to terminate the string, or nc_put_att_text(..., strlen(input_string) ,input_string) if you want to not include the null byte, since the string length is available through the netCDF interface without it. --Russ _____________________________________________________________________ Russ Rew UCAR Unidata Program address@hidden http://www.unidata.ucar.edu