Qbs Manual | List of Built-in Services | BinaryFile Service

Qbs 1.12.2

# BinaryFile Service

The `BinaryFile` service allows you to read from and write into binary files.

## Related Declarations

### BinaryFile.OpenMode

      enum BinaryFile.OpenMode { ReadOnly, WriteOnly, ReadWrite }

List of modes that a file may be opened in.

The OpenMode values can be combined with the bitwise or operator.

## Available operations

### Constructor

      BinaryFile(filePath: string, openMode: OpenMode = BinaryFile.ReadOnly)

Opens the file at `filePath` in the given mode and returns the object representing the file.

Note: The mode influences which of the operations listed below can actually be used on the file.

### atEof

      atEof(): boolean

Returns `true` if no more data can be read from the file, `false` otherwise.

### close

      close(): void

Closes the file. It is recommended to always call this function as soon as you are finished with the file, in order to keep the number of in-flight file descriptors as low as possible.

### filePath

      filePath(): string

The absolute path of the file represented by this object.

### size

      size(): number

Returns the size of the file (in bytes).

### resize

      resize(size: number): void

Sets the file `size` (in bytes). If `size` is larger than the file currently is, the new bytes will be set to 0; if `size` is smaller, the file is truncated.

### pos

      pos(): number

Returns the position that data is written to or read from.

### seek

      seek(pos: number): void

Sets the current position to `pos`.

### read

      read(size: number): number[]

Reads at most `size` bytes of data from the file and returns it as an array.

### write

      write(data: number[]): void

Writes `data` into the file at the current position.