---
name: "rtbl_add_column_entry"
section: "3"
platform: "linux"
package: "heimdal-dev"
version: "7.8.git20221117.28daf24+dfsg-9+deb13u1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/rtbl_add_column_entry"
---

# rtbl\_add\_column\_entry(3)

format data in simple tables

## NAME

**rtbl\_create**, **rtbl\_destroy**, **rtbl\_set\_flags**, **rtbl\_get\_flags**, **rtbl\_set\_prefix**, **rtbl\_set\_separator**, **rtbl\_set\_column\_prefix**, **rtbl\_set\_column\_affix\_by\_id**, **rtbl\_add\_column**, **rtbl\_add\_column\_by\_id**, **rtbl\_add\_column\_entry**, **rtbl\_add\_column\_entry\_by\_id**, **rtbl\_new\_row**, **rtbl\_format** – format data in simple tables

## LIBRARY

The roken library (libroken, -lroken)

## SYNOPSIS

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

*int*\

**rtbl\_add\_column**(*rtbl\_t table*, *const char \*column\_name*, *unsigned int flags*);

*int*\

**rtbl\_add\_column\_by\_id**(*rtbl\_t table*, *unsigned int column\_id*, *const char \*column\_header*, *unsigned int flags*);

*int*\

**rtbl\_add\_column\_entry**(*rtbl\_t table*, *const char \*column\_name*, *const char \*cell\_entry*);

*int*\

**rtbl\_add\_column\_entry\_by\_id**(*rtbl\_t table*, *unsigned int column\_id*, *const char \*cell\_entry*);

*rtbl\_t*\

**rtbl\_create**(*void*);

*void*\

**rtbl\_destroy**(*rtbl\_t table*);

*int*\

**rtbl\_new\_row**(*rtbl\_t table*);

*int*\

**rtbl\_set\_column\_affix\_by\_id**(*rtbl\_t table*, *unsigned int column\_id* , *const*, *char*, *\*prefix"*, *const char \*suffix*);

*int*\

**rtbl\_set\_column\_prefix**(*rtbl\_t table*, *const char \*column\_name*, *const char \*prefix*);

*unsigned int*\

**rtbl\_get\_flags**(*rtbl\_t table*);

*void*\

**rtbl\_set\_flags**(*rtbl\_t table*, *unsigned int flags*);

*int*\

**rtbl\_set\_prefix**(*rtbl\_t table*, *const char \*prefix*);

*int*\

**rtbl\_set\_separator**(*rtbl\_t table*, *const char \*separator*);

*int*\

**rtbl\_format**(*rtbl\_t table* , *FILE*, *\*file"*);

## DESCRIPTION

This set of functions assemble a simple table consisting of rows and columns, allowing it to be printed with certain options. Typical use would be output from tools such as [ls(1)](/ls/1.md) or [netstat(1)](/netstat/1.md), where you have a fixed number of columns, but don't know the column widths before hand.

A table is created with **rtbl\_create**() and destroyed with **rtbl\_destroy**().

Global flags on the table are set with *rtbl\_set\_flags* and retrieved with *rtbl\_get\_flags*. At present the only defined flag is RTBL\_HEADER\_STYLE\_NONE which suppresses printing the header.

Before adding data to the table, one or more columns need to be created. This would normally be done with **rtbl\_add\_column\_by\_id**(), *column\_id* is any number of your choice (it's used only to identify columns), *column\_header* is the header to print at the top of the column, and *flags* are flags specific to this column. Currently the only defined flag is RTBL\_ALIGN\_RIGHT, aligning column entries to the right. Columns are printed in the order they are added.

There's also a way to add columns by column name with **rtbl\_add\_column**(), but this is less flexible (you need unique header names), and is considered deprecated.

To add data to a column you use **rtbl\_add\_column\_entry\_by\_id**(), where the *column\_id* is the same as when the column was added (adding data to a non-existent column is undefined), and *cell\_entry* is whatever string you wish to include in that cell. It should not include newlines. For columns added with **rtbl\_add\_column**() you must use **rtbl\_add\_column\_entry**() instead.

**rtbl\_new\_row**() fills all columns with blank entries until they all have the same number of rows.

Each column can have a separate prefix and suffix, set with *rtbl\_set\_column\_affix\_by\_id*; *rtbl\_set\_column\_prefix* allows setting the prefix only by column name. In addition to this, columns may be separated by a string set with*rtbl\_set\_separator*, *(* by default columns are not seprated by anything).

The finished table is printed to *file* with *rtbl\_format*.

## EXAMPLES

This program:

```
#include <stdio.h>
#include <rtbl.h>
int
main(int argc, char **argv)
{
    rtbl_t table;
    table = rtbl_create();
    rtbl_set_separator(table, "  ");
    rtbl_add_column_by_id(table, 0, "Column A", 0);
    rtbl_add_column_by_id(table, 1, "Column B", RTBL_ALIGN_RIGHT);
    rtbl_add_column_by_id(table, 2, "Column C", 0);
    rtbl_add_column_entry_by_id(table, 0, "A-1");
    rtbl_add_column_entry_by_id(table, 0, "A-2");
    rtbl_add_column_entry_by_id(table, 0, "A-3");
    rtbl_add_column_entry_by_id(table, 1, "B-1");
    rtbl_add_column_entry_by_id(table, 2, "C-1");
    rtbl_add_column_entry_by_id(table, 2, "C-2");
    rtbl_add_column_entry_by_id(table, 1, "B-2");
    rtbl_add_column_entry_by_id(table, 1, "B-3");
    rtbl_add_column_entry_by_id(table, 2, "C-3");
    rtbl_add_column_entry_by_id(table, 0, "A-4");
    rtbl_new_row(table);
    rtbl_add_column_entry_by_id(table, 1, "B-4");
    rtbl_new_row(table);
    rtbl_add_column_entry_by_id(table, 2, "C-4");
    rtbl_new_row(table);
    rtbl_format(table, stdout);
    rtbl_destroy(table);
    return 0;
}
```

will output the following:

```
Column A  Column B  Column C
A-1            B-1  C-1
A-2            B-2  C-2
A-3            B-3  C-3
A-4
               B-4
                    C-4
```
