- Main Page
- Tutorials
- Functions
- Release Notes
- Examples

Functions
saveArray
Complete List of ArrayFire Functions ArrayFire Functions by Category » Input and Output functions » | Reading and writing arrays

Save an array to a binary file. More...

## Functions
AFAPI int | saveArray (const char *key, const array &arr, const char *filename, const bool append=false)
AFAPI af_err | af_save_array (int *index, const char *key, const af_array arr, const char *filename, const bool append)

## Detailed Description

Save an array to a binary file.

The saveArray and readArray functions are designed to provide store and read access to arrays using files written to disk.

The format of the file (version 1) is as follows:

Header:

Description | Data Type | Size (Bytes) | Detailed Desc
--- | --- | --- | ---
Version | Char | 1 | ArrayFire File Format Version for future use. Currently set to 1
Array Count | Int | 4 | No. of Arrays stored in file

Per Array:

Description | Data Type | Size (Bytes) | Detailed Desc
--- | --- | --- | ---
Length of Key String | Int | 4 | No. of characters (excluding null ending) in the key string
Key | Char [] | length | Key of the Array. Used when reading from file
Offset | Int64 | 8 | No of bytes between offset and start of next array
Array Type | Char | 1 | Type corresponding to af_dtype enum
Dims (4 values) | Int64 | 4 * 8 = 32 | Dimensions of the Array
Data | Type | sizeof(Type) * dims.elements() | Actual data of the array

The offset is equal to 1 byte (type) + 32 bytes (dims) + size of data.

An file with 2 arrays would look like (representative)

> 1
> 2
> Array 1 Key Length
> Array 1 Key
> Array 1 Offset
> Array 1 Type
> Array 1 Dims
> Array 1 Data
> Array 2 Key Length
> Array 2 Key
> Array 2 Offset
> Array 2 Type
> Array 2 Dims
> Array 2 Data

Save array allows you to append any number of Arrays to the same file using the append argument. If the append argument is false, then the contents of the file are discarded and new array is written anew.

On each append, the array counter in the header is incremented and the new array is written to the end of the file. This function does not check if the tag is unique or not.

## Function Documentation

AFAPI af_err af_save_array | ( | int * | index,
|  | const char * | key,
|  | const af_array | arr,
|  | const char * | filename,
|  | const bool | append
| ) |  |

Parameters

  [out] | index | is the index location of the array in the file
  [in] | key | is an expression used as tag/key for the array during readArray()
  [in] | arr | is the array to be written
  [in] | filename | is the path to the location on disk
  [in] | append | is used to append to an existing file when true and create or overwrite an existing file when false

AFAPI int af::saveArray | ( | const char * | key,
|  | const array & | arr,
|  | const char * | filename,
|  | const bool | append = `false`
| ) |  |

Parameters

  [in] | key | is an expression used as tag/key for the array during readArray
  [in] | arr | is the array to be written
  [in] | filename | is the path to the location on disk
  [in] | append | is used to append to an existing file when true and create or overwrite an existing file when false

Returns
  index of the saved array in the file