| Top |
| int | gretl_read_native_data () |
| int | gretl_write_native_data () |
| int | gretl_read_foreign_data () |
| GretlFileType | gretl_detect_filetype () |
Functionality for reading data from native-format gretl datafiles, and writing data to such files. Plus importation of data from various sorts of non-native data files.
Here is a simple but complete example of use of gretl_read_native_data()
to pull data into a program's workspace and print basic info
on the data that were read.
<informalexample><programlisting> #include <gretl/libgretl.h>
int main (int argc, char **argv) { char *fname; DATASET *dset; PRN *prn; int err;
if (argc >= 2) { fname = argv[1]; } else { exit(EXIT_FAILURE); }
libgretl_init();
prn = gretl_print_new(GRETL_PRINT_STDOUT, NULL);
dset = datainfo_new();
err = gretl_read_native_data(fname, dset);
if (err) {
pprintf(prn, "Got error d reading data from s\n", err, fname);
errmsg(err, prn);
} else {
pprintf(prn, "Read data from s OK\n", fname);
print_smpl(dset, 0, OPT_NONE, prn);
varlist(dset, prn);
}
destroy_dataset(dset);
gretl_print_destroy(prn);
libgretl_cleanup();
return 0; } </programlisting></informalexample>
int gretl_read_native_data (const char *fname,DATASET *dset);
Read data from file into gretl's work space, allocating memory as required.
The argument dset
represents a pointer-to-DATASET. It should
either be given as the address of a DATASET struct that exists
at the caller level, or it can be a pointer obtained via the
libgretl function datainfo_new().
int gretl_write_native_data (const char *fname,const int *list,const DATASET *dset);
Write out in native gretl (.gdt) format a data file containing the values of the given set of variables.
int gretl_read_foreign_data (const char *fname,GretlFileType file_type,DATASET *dset,PRN *prn);
Read data from a "foreign" format data file into gretl's work space,
allocating memory as required. For comments on the arguments pZ
and dset
,
see gretl_read_native_data().
file_type
must be one of GRETL_CSV, GRETL_OCTAVE,
GRETL_GNUMERIC, GRETL_XLS, GRETL_XLSX, GRETL_ODS, GRETL_WF1,
GRETL_DTA, GRETL_SAV, GRETL_SAS or GRETL_JMULTI. If you are
unsure of the type of the file you may call gretl_detect_filetype()
first. Note that the GRETL_CSV type is quite "permissive",
including plain text data files with the data values separated
in various ways, not just comma-separated values in the strict
sense. (The separation must, however, be consistent within the
given file.)
If prn
is non-NULL, diagnostic information will be printed.
This can be useful to gauge how successful the import was.
GretlFileType
gretl_detect_filetype (const char *fname);
Attempts to determine if the named file is of a type from which gretl can read data.
code representing the type of the data file, or
GRETL_UNRECOGNIZED if the file doesn't seem to be something
gretl can work with.