---
name: "g2cio.c"
section: "3"
platform: "linux"
package: "libg2c-dev"
version: "2.1.0-2"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/g2cio.c"
---

# g2cio.c(3)

File I/O functions for the g2c library.

## NAME

g2cio.c - File I/O functions for the g2c library.

## SYNOPSIS

`#include 'grib2_int.h'`\

### Macros

\#define **BITSHIFT\_15**   15\
 15 bits.  #define **BITSHIFT\_31**   31\
 31 bits.  #define **BITSHIFT\_63**   63\
 63 bits.  #define **BITSHIFT\_7**   7\
 7 bits.

### Functions

int **g2c\_file\_io** (FILE \*f, int write, int g2ctype, void \*var)\
 Read or write a big-endian integer type to an open file, with conversion between native and big-endian format.  int **g2c\_file\_io\_byte** (FILE \*f, int write, char \*var)\
 Read or write a big-endian signed byte to an open GRIB2 file, with conversion between native and big-endian format, and special GRIB2 handling of negative numbers.  int **g2c\_file\_io\_int** (FILE \*f, int write, int \*var)\
 Read or write a big-endian 4-byte signed int to an open GRIB2 file, with conversion between native and big-endian format, and special GRIB2 handling of negative numbers.  int **g2c\_file\_io\_longlong** (FILE \*f, int write, long long \*var)\
 Read or write a big-endian signed long long to an open GRIB2 file, with conversion between native and big-endian format, and special GRIB2 handling of negative numbers.  int **g2c\_file\_io\_short** (FILE \*f, int write, short \*var)\
 Read or write a big-endian signed short to an open GRIB2 file, with conversion between native and big-endian format, and special GRIB2 handling of negative numbers.  int **g2c\_file\_io\_template** (FILE \*f, int rw\_flag, int map, long long int \*template\_value)\
 Read or write a big-endian 4-byte int or unsigned int from or to an open file, with conversion between native and big-endian format, and handling of GRIB negative numbers.  int **g2c\_file\_io\_ubyte** (FILE \*f, int write, unsigned char \*var)\
 Read or write a big-endian unsigned byte to an open GRIB2 file, with conversion between native and big-endian format.  int **g2c\_file\_io\_uint** (FILE \*f, int write, unsigned int \*var)\
 Read or write a big-endian 4-byte unsigned int to an open GRIB2 file, with conversion between native and big-endian format.  int **g2c\_file\_io\_ulonglong** (FILE \*f, int write, unsigned long long \*var)\
 Read or write a big-endian unsigned long long to an open GRIB2 file, with conversion between native and big-endian format.  int **g2c\_file\_io\_ushort** (FILE \*f, int write, unsigned short \*var)\
 Read or write a big-endian unsigned short to an open GRIB2 file, with conversion between native and big-endian format.

## Detailed Description

File I/O functions for the g2c library.

**Author**

Ed Hartnett

**Date**

Nov 11, 2022

Definition in file **g2cio.c**.

## Macro Definition Documentation

### #define BITSHIFT\_15 15

15 bits.

Definition at line **10** of file **g2cio.c**.

### #define BITSHIFT\_31 31

31 bits.

Definition at line **11** of file **g2cio.c**.

### #define BITSHIFT\_63 63

63 bits.

Definition at line **12** of file **g2cio.c**.

### #define BITSHIFT\_7 7

7 bits.

Definition at line **9** of file **g2cio.c**.

## Function Documentation

### int g2c\_file\_io (FILE \* f, int write, int g2ctype, void \* var)

Read or write a big-endian integer type to an open file, with conversion between native and big-endian format. GRIB2 handles negative numbers in a special way. Instead of storing two-compliments, like every other programmer and computing organization in the world, GRIB2 flips the first bit, then stores the rest of the int as an unsigned number in the remaining 31 bits. How exciting!

This function takes the excitement out of GRIB2 negative numbers.

**Parameters**

*f* Pointer to the open FILE. \
 *write* Non-zero if function should write, otherwise function will read. \
 *g2ctype* The type to be read or written. \
 *var* Pointer to the int to be written, or pointer to the storage that gets the int read.

**Returns**

- •

  :: G2C\_NOERROR No error.

- •

  :: G2C\_EINVAL Invalid input.

- •

  :: G2C\_EFILE Error reading/writing file.

**Author**

Ed Hartnett

**Date**

11/7/22

Definition at line **41** of file **g2cio.c**.

References **BITSHIFT\_15**, **BITSHIFT\_31**, **BITSHIFT\_63**, **BITSHIFT\_7**, **EIGHT\_BYTES**, **FOUR\_BYTES**, **G2C\_BYTE**, **G2C\_EBADTYPE**, **G2C\_EFILE**, **G2C\_EINVAL**, **G2C\_INT**, **G2C\_INT64**, **G2C\_NOERROR**, **G2C\_SHORT**, **G2C\_UBYTE**, **G2C\_UINT**, **G2C\_UINT64**, **G2C\_USHORT**, **hton64**, **ntoh64**, **ONE\_BYTE**, and **TWO\_BYTES**.

Referenced by **g2c\_file\_io\_byte()**, **g2c\_file\_io\_int()**, **g2c\_file\_io\_longlong()**, **g2c\_file\_io\_short()**, **g2c\_file\_io\_ubyte()**, **g2c\_file\_io\_uint()**, **g2c\_file\_io\_ulonglong()**, and **g2c\_file\_io\_ushort()**.

### int g2c\_file\_io\_byte (FILE \* f, int write, char \* var)

Read or write a big-endian signed byte to an open GRIB2 file, with conversion between native and big-endian format, and special GRIB2 handling of negative numbers.

**Parameters**

*f* Pointer to the open GRIB2 FILE. \
 *write* Non-zero to write, zero to read. \
 *var* Pointer to the byte.

**Returns**

- •

  :: G2C\_NOERROR No error.

- •

  :: G2C\_EINVAL Invalid input.

- •

  :: G2C\_EFILE Error reading/writing file.

**Author**

Ed Hartnett 11/13/22

Definition at line **316** of file **g2cio.c**.

References **G2C\_BYTE**, and **g2c\_file\_io()**.

Referenced by **g2c\_file\_io\_template()**.

### int g2c\_file\_io\_int (FILE \* f, int write, int \* var)

Read or write a big-endian 4-byte signed int to an open GRIB2 file, with conversion between native and big-endian format, and special GRIB2 handling of negative numbers.

**Parameters**

*f* Pointer to the open GRIB2 FILE. \
 *write* Non-zero to write, zero to read. \
 *var* Pointer to the int.

**Returns**

- •

  :: G2C\_NOERROR No error.

- •

  :: G2C\_EINVAL Invalid input.

- •

  :: G2C\_EFILE Error reading/writing file.

**Author**

Ed Hartnett 11/7/22

Definition at line **230** of file **g2cio.c**.

References **g2c\_file\_io()**, and **G2C\_INT**.

Referenced by **g2c\_file\_io\_template()**.

### int g2c\_file\_io\_longlong (FILE \* f, int write, long long \* var)

Read or write a big-endian signed long long to an open GRIB2 file, with conversion between native and big-endian format, and special GRIB2 handling of negative numbers.

**Parameters**

*f* Pointer to the open GRIB2 FILE. \
 *write* Non-zero to write, zero to read. \
 *var* Pointer to the long long.

**Returns**

- •

  :: G2C\_NOERROR No error.

- •

  :: G2C\_EINVAL Invalid input.

- •

  :: G2C\_EFILE Error reading/writing file.

**Author**

Ed Hartnett 11/13/22

Definition at line **359** of file **g2cio.c**.

References **g2c\_file\_io()**, and **G2C\_INT64**.

### int g2c\_file\_io\_short (FILE \* f, int write, short \* var)

Read or write a big-endian signed short to an open GRIB2 file, with conversion between native and big-endian format, and special GRIB2 handling of negative numbers.

**Parameters**

*f* Pointer to the open GRIB2 FILE. \
 *write* Non-zero to write, zero to read. \
 *var* Pointer to the short.

**Returns**

- •

  :: G2C\_NOERROR No error.

- •

  :: G2C\_EINVAL Invalid input.

- •

  :: G2C\_EFILE Error reading/writing file.

**Author**

Ed Hartnett 11/13/22

Definition at line **273** of file **g2cio.c**.

References **g2c\_file\_io()**, and **G2C\_SHORT**.

Referenced by **g2c\_file\_io\_template()**, **g2c\_rw\_section1\_metadata()**, **g2c\_start\_index\_record()**, and **g2c\_start\_index\_record\_lf()**.

### int g2c\_file\_io\_template (FILE \* f, int rw\_flag, int map, long long int \* template\_value)

Read or write a big-endian 4-byte int or unsigned int from or to an open file, with conversion between native and big-endian format, and handling of GRIB negative numbers. This is for template values.

With template values, if the map value is negative, then the template value may be negative.

**Parameters**

*f* Pointer to the open FILE. \
 *rw\_flag* Non-zero if function should write, otherwise function will read. \
 *map* The map value for this template item. \
 *template\_value* Pointer to the template value to be written, or pointer to the storage that gets the templage value read.

**Returns**

- •

  :: G2C\_NOERROR No error.

- •

  :: G2C\_EINVAL Invalid input.

- •

  :: G2C\_EFILE Error reading/writing file.

**Author**

Ed Hartnett 11/7/22

Definition at line **408** of file **g2cio.c**.

References **FOUR\_BYTES**, **G2C\_EBADTEMPLATE**, **g2c\_file\_io\_byte()**, **g2c\_file\_io\_int()**, **g2c\_file\_io\_short()**, **g2c\_file\_io\_ubyte()**, **g2c\_file\_io\_uint()**, **g2c\_file\_io\_ushort()**, **G2C\_NOERROR**, **ONE\_BYTE**, and **TWO\_BYTES**.

Referenced by **g2c\_rw\_section3\_metadata()**, **g2c\_rw\_section4\_metadata()**, and **g2c\_rw\_section5\_metadata()**.

### int g2c\_file\_io\_ubyte (FILE \* f, int write, unsigned char \* var)

Read or write a big-endian unsigned byte to an open GRIB2 file, with conversion between native and big-endian format.

**Parameters**

*f* Pointer to the open GRIB2 FILE. \
 *write* Non-zero to write, zero to read. \
 *var* Pointer to the unsigned byte.

**Returns**

- •

  :: G2C\_NOERROR No error.

- •

  :: G2C\_EINVAL Invalid input.

- •

  :: G2C\_EFILE Error reading/writing file.

**Author**

Ed Hartnett 11/13/22

Definition at line **337** of file **g2cio.c**.

References **g2c\_file\_io()**, and **G2C\_UBYTE**.

Referenced by **g2c\_file\_io\_template()**, **g2c\_open\_index()**, **g2c\_rw\_section1\_metadata()**, **g2c\_rw\_section3\_metadata()**, **g2c\_rw\_section6\_metadata()**, **g2c\_start\_index1\_record()**, **g2c\_start\_index\_record()**, **g2c\_start\_index\_record\_lf()**, **g2c\_write\_index()**, and **read\_msg\_metadata()**.

### int g2c\_file\_io\_uint (FILE \* f, int write, unsigned int \* var)

Read or write a big-endian 4-byte unsigned int to an open GRIB2 file, with conversion between native and big-endian format.

**Parameters**

*f* Pointer to the open GRIB2 FILE. \
 *write* Non-zero to write, zero to read. \
 *var* Pointer to the unsigned int.

**Returns**

- •

  :: G2C\_NOERROR No error.

- •

  :: G2C\_EINVAL Invalid input.

- •

  :: G2C\_EFILE Error reading/writing file.

**Author**

Ed Hartnett 11/7/22

Definition at line **251** of file **g2cio.c**.

References **g2c\_file\_io()**, and **G2C\_UINT**.

Referenced by **g2c\_file\_io\_template()**, **g2c\_open\_index()**, **g2c\_rw\_section1\_metadata()**, **g2c\_rw\_section3\_metadata()**, **g2c\_rw\_section5\_metadata()**, **g2c\_start\_index1\_record()**, **g2c\_start\_index\_record()**, **g2c\_start\_index\_record\_lf()**, **g2c\_write\_index()**, and **read\_msg\_metadata()**.

### int g2c\_file\_io\_ulonglong (FILE \* f, int write, unsigned long long \* var)

Read or write a big-endian unsigned long long to an open GRIB2 file, with conversion between native and big-endian format.

**Parameters**

*f* Pointer to the open GRIB2 FILE. \
 *write* Non-zero to write, zero to read. \
 *var* Pointer to the unsigned long long.

**Returns**

- •

  :: G2C\_NOERROR No error.

- •

  :: G2C\_EINVAL Invalid input.

- •

  :: G2C\_EFILE Error reading/writing file.

**Author**

Ed Hartnett 11/13/22

Definition at line **380** of file **g2cio.c**.

References **g2c\_file\_io()**, and **G2C\_UINT64**.

Referenced by **g2c\_start\_index\_record()**, and **g2c\_start\_index\_record\_lf()**.

### int g2c\_file\_io\_ushort (FILE \* f, int write, unsigned short \* var)

Read or write a big-endian unsigned short to an open GRIB2 file, with conversion between native and big-endian format.

**Parameters**

*f* Pointer to the open GRIB2 FILE. \
 *write* Non-zero to write, zero to read. \
 *var* Pointer to the unsigned short.

**Returns**

- •

  :: G2C\_NOERROR No error.

- •

  :: G2C\_EINVAL Invalid input.

- •

  :: G2C\_EFILE Error reading/writing file.

**Author**

Ed Hartnett 11/13/22

Definition at line **294** of file **g2cio.c**.

References **g2c\_file\_io()**, and **G2C\_USHORT**.

Referenced by **g2c\_file\_io\_template()**, **g2c\_rw\_section3\_metadata()**, **g2c\_rw\_section4\_metadata()**, and **g2c\_rw\_section5\_metadata()**.

## Author

Generated automatically by Doxygen for NCEPLIBS-g2c from the source code.
