[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
961002: How to write multplie strings using C++ interface
- Subject: 961002: How to write multplie strings using C++ interface
- Date: Wed, 02 Oct 96 08:57:07 -0600
Miki,
> To: address@hidden
> From: "R. Miki Moore 423-9712" <address@hidden>
> Subject: C++ and putting more than one element
> Organization: LLNL
> Keywords: 199610020509.AA09727
In the above message you wrote:
> I am a newbie with netcdf and am grappling with writing a list of
> names to a netcdf file. I can't seem to get anything but the first
> name into the file. I'd like to write name1, then name2; rather
> than concatenate all the names into one lonnnng string and
> write the whole thing in one blast..is this possible? Am I somehow
> making an error in my parameters to the var->put call?
> //
> // simple.cc
> // miki moore
> // 9/96
> // writes string variables
> //
>
> #include <iostream.h>
> #include <fstream.h>
> #include <stdlib.h>
> #include <NetCDF/netcdf.hh>
> int main(int argc,char **argv)
> {
> // create output file
> NcFile outfile(argv[1],NcFile::Replace);
>
> if (! outfile.is_valid()) {
> cout << "file not valid: " << argv[1] << endl;
> ::exit(1);
> }
>
> NcDim* numObs = outfile.add_dim("numObs",30);
> NcDim* strLength = outfile.add_dim("strLen",32);
>
> NcVar* var = outfile.add_var("stnName",
> ncChar,
> numObs,
> strLength);
>
> if (var == NULL) {
> cout << "failed to add var" << argv[2] << endl;
> ::exit(1);
> }
>
> // first string
> int length = 30;
> char* data = new char[length+1];
> strcpy(data,argv[2]);
>
> NcBool ncError;
> ncError = var->put(data,0,length);
You should use an edge-length of `1' in the above rather than `0' for
the most slowly varying dimention.
> if (! ncError) { cout << "error putting " << endl; ::exit(1); }
>
> delete data;
>
> // second string
> char* data2 = new char[length+1];
> strcpy(data2,argv[3]);
> int record = atoi(argv[4]);
At this point, you should use the NcVar member function `set_cur()' to
set the start corner for the next string; otherwise, you'll just rewrite
the first string. E.g.
var->set_cur(1, 0);
I'm unsure what the `record' variable is. You use it in the next call
as (basically) the number of strings (which I find hard to believe
becuse the string buffer is dimensioned only for one).
Also, don't you mean to write `data2' rather than `data'.
>
> ncError = var->put(data,record,length);
> if (! ncError) { cout << "error putting " << endl; ::exit(1); }
>
> delete data2;
> }
Please let me know if this helps.
--------
Steve Emmerson <address@hidden>