Amiga C Tutorial

File Input and Output

Amiga DOS library includes some simple file i/o functions to open, read,write and close files. The following functions are commonly used:

BPTR Open( CONST_STRPTR name, LONG accessMode ); /* Open a file for read or write */
LONG Close( BPTR file ); /* Close a file */
LONG Read( BPTR file, APTR buffer, LONG length ); /* Read n bytes into a buffer (unbuffered) */
LONG Write( BPTR file, CONST APTR buffer, LONG length ); /* Write n bytes into a buffer (unbuffered) */
LONG Seek( BPTR file, LONG position, LONG offset ); /* Seek to a n bytes from current, beginning or end of file */
LONG FGetC( BPTR fh ); /* Get a character from file */
LONG FPutC( BPTR fh, LONG ch ); /* Write a character to file */
LONG UnGetC( BPTR fh, LONG character ); /* Return a character to buffer */
LONG FRead( BPTR fh, APTR block, ULONG blocklen, ULONG number ); /* Read a number of blocks from file (buffered) */
LONG FWrite( BPTR fh, CONST APTR block, ULONG blocklen, ULONG number ); /* Write a number of blocks to a file (buffered) */
STRPTR FGets( BPTR fh, STRPTR buf, ULONG buflen ); /* Gets a string from a file */
LONG FPuts( BPTR fh, CONST_STRPTR str ); /* Writes a string to a file */
VOID VFWritef( BPTR fh, CONST_STRPTR format, CONST LONG *argarray ); /* Write a BCPL formatted string to a file */
LONG VFPrintf( BPTR fh, CONST_STRPTR format, CONST APTR argarray ); /* Format and print a string to a file */
LONG Flush( BPTR fh ); /* Flush buffers to file */
LONG SetVBuf( BPTR fh, STRPTR buff, LONG type, LONG size ); /*Set buffering modes and size */

Amiga DOS supports buffered and unbuffered file I/O. Buffered file I/O is more efficient than unbuffered i/o for small files. FPuts and FGets would be ideal for getting lines of text from a text file with line feed or null terminated strings. FGetC and FPutC would be ideal for reading binary files. To detect End of File, most functions will return -1 (EOF), 0 or NULL values depending on the return value of the function, there is no EOF type function as in ANSI C. When opening a file, you need to specify the accessMode which can be MODE_OLDFILE (read an existing file), MODE_NEWFILE (create a new file for writing) or MODE_READWRITE (for read/writing to an existing file), the Open command returns a file handle of BPTR type which is used in most I/O functions.

The following example, will write out 20 numbers to a file, and read them back in and print them to the console.

/* File Example
using DOS.library
by PHutchison
3/1/05
*/

#include <proto/dos.h>
#include <stdio.h>
#include <stdlib.h>

/* Write some numbers to a file */
void WriteNumbers(void) {
  BPTR fh;
  int n;
  UBYTE numstr[5];

  printf("Writing numbers to numbers.dat\n\n");
  /* Open file for writing */
  fh = Open("numbers.dat", MODE_NEWFILE);
  if (fh) {
    for (n=1; n<=20; n++) {
      /* Convert number to a string with new line
          & write string to file */
      sprintf(numstr, "%d\n", n);
      FPuts(fh, numstr);
     }
     Close(fh);
  }
}

/* Read numbers from a file */
void ReadNumbers(void) {
  BPTR fh;
  int i;
  UBYTE numstr[5];
  UBYTE *buffer;

  printf("Reading file numbers.dat.\n");
  /* Open existing file for reading */
  fh = Open("numbers.dat", MODE_OLDFILE);
  if (fh) {
     /* Read a string and check for End of File */
     while (buffer = FGets(fh, numstr, 5L)) {
         /* Convert string to number and print it */
         i = atoi(numstr);
         printf("Number %d\n", i);
     }
     Close(fh);
  }
  printf("EOF found\n");
}

int main(void)
{
  WriteNumbers();

  ReadNumbers();

  return 0;
}