[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
20031203: How to create an array of string?
- Subject: 20031203: How to create an array of string?
- Date: Wed, 03 Dec 2003 10:25:19 -0700
David,
>Date: Wed, 3 Dec 2003 12:12:32 -0500
>From: David Han <address@hidden>
>Organization: NASA
>To: Steve Emmerson <address@hidden>
>Subject: Re: 20031203: How to create an array of string?
The above message contained the following:
> I tried exactly what you suggested below prior to sending my
> question, but it didn't work. Can you send a little snippet of code
> that creates a variable, writes and reads an array of string. Thanks.
You can do that yourself using the ncgen(1) utility and the "test.cdl"
file from my previous email:
$ ncgen -c test.cdl
#include <stdio.h>
#include <stdlib.h>
#include <netcdf.h>
void
check_err(const int stat, const int line, const char *file) {
if (stat != NC_NOERR) {
(void) fprintf(stderr, "line %d of %s: %s\n", line, file,
nc_strerror(stat));
exit(1);
}
}
int
main() { /* create test.nc */
int ncid; /* netCDF id */
/* dimension ids */
int len_dim;
int n1_dim;
/* dimension lengths */
size_t len_len = 15;
size_t n1_len = 2;
/* variable ids */
int strings_id;
/* rank (number of dimensions) for each variable */
# define RANK_strings 2
/* variable shapes */
int strings_dims[RANK_strings];
/* enter define mode */
int stat = nc_create("test.nc", NC_CLOBBER, &ncid);
check_err(stat,__LINE__,__FILE__);
/* define dimensions */
stat = nc_def_dim(ncid, "len", len_len, &len_dim);
check_err(stat,__LINE__,__FILE__);
stat = nc_def_dim(ncid, "n1", n1_len, &n1_dim);
check_err(stat,__LINE__,__FILE__);
/* define variables */
strings_dims[0] = n1_dim;
strings_dims[1] = len_dim;
stat = nc_def_var(ncid, "strings", NC_CHAR, RANK_strings, strings_dims,
&strings_id);
check_err(stat,__LINE__,__FILE__);
/* leave define mode */
stat = nc_enddef (ncid);
check_err(stat,__LINE__,__FILE__);
{ /* store strings */
static char strings[] = {"a string\000\000\000\000\000\000\000another
string\000"};
stat = nc_put_var_text(ncid, strings_id, strings);
check_err(stat,__LINE__,__FILE__);
}
stat = nc_close(ncid);
check_err(stat,__LINE__,__FILE__);
return 0;
}
Regards,
Steve Emmerson