LxF. Standard IO - JulTob/Ada GitHub Wiki

16.10 Stdio Files

C has a library called stdio, or STanDard IO, which contains standard operations for text files. Loosely, stdio is the C equivalent of Ada's Text_IO package.The standard gnat package cstreams(?) provides a thin binding to many of the stdio functions. In this section, we'll looking at using stdio directly.

Some of the stdio functions can't be used from Ada because of differences in the languages. For example, printf, the standard command for writing to the screen, has a variable number of parameters. Because there's no way to express a variable number of parameters in Ada, printf can't be imported into Ada.

with System; type AStdioFileID is new System.Address;

function fputc( c : integer; fid : AStdioFileID ) return integer; pragma import( C, fputc, "fputc" ); Part of standard C library. Writes one charcter to a file.

function fputs( s : string; fid : AStdioFileID ) return integer; pragma import( C, fputs, "fputs" ); Writes a C string to a file.

function ferror( fid : AStdioFileID ) return integer; pragma import( C, ferror); Return error from last file operation, if any.

procedure clearerr( fid : AStdioFileID ); pragma import( C, clearerr); Clear the error and end of file information.

function feof( fid : AStdioFileID ) return integer; pragma import( C, feof); Return non-zero if you are at the end of the file.

function fileno( fid : AStdioFileID ) return integer; pragma import( C, fileno); Return the file number for use with Linux file kernel calls.

function flock( fd, operation : integer ) return integer; pragma import( C, flock ); Locks or unlocks a file (or a portion of a file). This is for compatibility with other UNIXes--use fcntl instead.

Operation: LOCK_SH (1) - shared lock LOCK_EX (2) - exclusive lock LOCK_NB (4) - no block flag (may be added to others) LOCK_UN (8) - unlock