---
name: "gd_get_string"
section: "3"
platform: "linux"
package: "libgetdata-doc"
version: "0.11.0-15"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/gd_get_string"
---

# gd\_get\_string(3)

retrieve STRING or SARRAY data from a Dirfile database

## NAME

gd\_get\_string — retrieve STRING or SARRAY data from a Dirfile database

## SYNOPSIS

**#include \<getdata.h\>**

**size\_t gd\_get\_string(DIRFILE \****dirfile***, const char \****field\_code***,** **size\_t** *len***, char \****data\_out***);**

## DESCRIPTION

The **gd\_get\_string**() function queries a [dirfile(5)](/dirfile/5.md) database specified by *dirfile* for the string scalar *field\_code*, which should not contain a representation suffix.  The first *len* characters of the string scalar are stored in the user-supplied buffer *data\_out*. If *field\_code* refers to a **SARRAY** field, the first element is returned.

The  *dirfile* argument must point to a valid DIRFILE object previously created by a call to [**gd\_open**(3)](/gd_open/3.md).

If *len* equals zero, or if *data\_out* equals NULL, no data will be copied to *data\_out*, but the length of the string scalar will still be returned by **gd\_get\_string**(). Otherwise, the argument *data\_out* must point to a valid memory location of sufficient size to hold at least *len* characters.  If the length of the string scalar is greater than *len*, *data\_out* will not be NUL-terminated.

The [**gd\_get\_sarray**(3)](/gd_get_sarray/3.md) function provides another way of retrieving **STRING** data, but without having to know the length of the returned string in advance. The code:

**size\_t** *len* **= gd\_get\_string(***dirfile***,** *field\_code***, 0, NULL);**\
 **char \****string* **= malloc(***len***);**\
 **gd\_get\_string(***dirfile***,** *field\_code***,** *len***,** *string***);**

which ensures the whole string, including the terminating NUL, is returned, can be replaced with, simply:

**const char \****string;*\
 **gd\_get\_sarray(***dirfile***,** *field\_code***, &***string***);**

with the added benefit of not having manage the memory for the string.

## RETURN VALUE

On success, **gd\_get\_string**() returns the actual length of the specified string scalar, including space for the trailing NUL-character.  A return value greater than *len* indicates that the output string is not NUL-terminated.

On error, this function returns 0 and stores a negative-valued error code in the **DIRFILE** object which may be retrieved by a subsequent call to [**gd\_error**(3)](/gd_error/3.md). Possible error codes are:\
  .TP 8 **GD\_E\_BAD\_CODE** The field specified by *field\_code* was not found in the database.\
  .TP 8 **GD\_E\_BAD\_DIRFILE** An invalid *dirfile* was supplied.\
  .TP 8 **GD\_E\_BAD\_FIELD\_TYPE** The supplied *field\_code* referred to a field of type other than **STRING** or **SARRAY**.\
  .TP 8 **GD\_E\_INTERNAL\_ERROR** An internal error occurred in the library while trying to perform the task. This indicates a bug in the library.  Please report the incident to the maintainer.

A descriptive error string for the error may be obtained by calling [**gd\_error\_string**(3)](/gd_error_string/3.md).

## HISTORY

The **get\_string**() function appeared in GetData-0.4.0.

In GetData-0.7.0, this function was renamed to **gd\_get\_string**().

## SEE ALSO

[**gd\_get\_sarray**(3)](/gd_get_sarray/3.md), [**gd\_error**(3)](/gd_error/3.md), [**gd\_error\_string**(3)](/gd_error_string/3.md), [**gd\_open**(3)](/gd_open/3.md), [**gd\_put\_string**(3)](/gd_put_string/3.md), [dirfile(5)](/dirfile/5.md)
