Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.

11.3.1 Input and output functions

Writes a string to the standard output and returns the number of characters written. If place-holders are used in the string then the appropriate number of parameters must be supplied after the string in the order they are to be printed as part of the string. So far we've only met the `\d' place-holder for decimal numbers. The complete list is:

Place-Holder  Parameter Type   Prints
     \c          Number        Character
     \d          Number        Decimal number
     \h          Number        Hexadecimal number
     \s          String        String

So to print a string you use the `\s' place-holder in the string and supply the string (i.e., a PTR TO CHAR) as a parameter. Try the following program (remember `\a' prints an apostrophe character):

PROC main()
  DEF s[30]:STRING
  StrCopy(s, 'Hello world', ALL)
  WriteF('The third element of s is "\c"\n', s[2])
  WriteF('or \d (decimal)\n',                s[2])
  WriteF('or \h (hexadecimal)\n',            s[2])
  WriteF('and s itself is \a\s\a\n',         s)
This is the output it generates:

The third element of s is "l"
or 108 (decimal)
or 6C (hexadecimal)
and s itself is 'Hello world'
You can control how the parameter is formatted in the `\d', `\h' and `\s' fields using another collection of special character sequences before the place-holder and size specifiers after it. If no size is specified the field will be as big as the data requires. A fixed field size can be specified using [number] after the place-holder. For strings you can also use the size specifier (min,max) which specifies the minimum and maximum sizes of the field. By default the data is right justified in the field and the left part of the field is filled, if necessary, with spaces. The following sequences before the place-holder can change this:

Sequence        Meaning
   \l     Left justify in field
   \r     Right justify in field
   \z     Set fill character to "0"

See how these formatting controls affect this example:

PROC main()
  DEF s[30]:STRING
  StrCopy(s, 'Hello world', ALL)
  WriteF('The third element of s is "\c"\n', s[2])
  WriteF('or \d[4] (decimal)\n',             s[2])
  WriteF('or \z\h[4] (hexadecimal)\n',       s[2])
  WriteF('\a\s[5]\a are the first five elements of s \n', s)
  WriteF('and s in a very big field  \a\s[20]\a\n',   s)
  WriteF('and s left justified in it \a\l\s[20]\a\n', s)
Here's the output it should generate:

The third element of s is "l"
or  108 (decimal)
or 006C (hexadecimal)
'Hello' are the first five elements of s 
and s in a very big field  '         Hello world'
and s left justified in it 'Hello world         '
WriteF uses the standard output, and this file handle is stored in the stdout variable. If your program is started from Workbench this variable will contain NIL. In this case, the first call to WriteF will open a special output window and put the file handle in the variables stdout and conout, as outlined above (see 11.2 Built-In Variables).

PrintF works just like WriteF except it uses the more efficient, buffered output routines only available if your Amiga is using Kickstart version 37 or greater (i.e., AmigaDOS 2.04 and above).

The same as WriteF except that the result is written to e-string instead of being printed. For example, the following code fragment sets s to `00123 is a' (since the E-string is not long enough for the whole string):

  DEF s[10]:STRING
  StringF(s, '\z\d[5] is a number', 123)

Outputs a single character, char, to the file or console window denoted by filehandle, and returns -1 to indicate success (so any other return value means an error occurred). For instance, filehandle could be stdout, in which case the character is written to the standard output. (You need to make sure stdout is not NIL, and you can do this by using a WriteF(") call.) In general, you obtain a filehandle using the Amiga system function Open from the `dos.library' (see 19 String Handling and I/O).

Reads and returns a single character from filehandle. If -1 is returned then the end of the file (EOF) was reached, or there was an error.

Reads a whole string from filehandle and returns -1 if EOF was reached or an error occurred. Characters are read up to a linefeed or the size of the string, whichever is sooner. Therefore, the resulting string may be only a partial line. If -1 is returned then EOF was reached or an error occurred, and in either case the string so far is still valid. So, you still need to check the string even if -1 is returned. (This will most commonly happen with files that do not end with a linefeed.) The string will be empty (i.e., of zero length) if nothing more had been read from the file when the error or EOF happened.

This next little program reads continually from its input until an error occurs or the user types quit. It echoes the lines that it reads in uppercase. If you type a line longer than ten characters you'll see it reads it in more than one go. Because of the way normal console windows work, you need to type a return before a line gets read by the program (but this allows you to edit the line before the program sees it). If the program is started from Workbench then stdin would be NIL, so WriteF(") is used to force stdout to be valid, and in this case it will be a new console window which can be used to accept input! (To make the compiled program into a Workbench program you simply need to create a tool icon for it. A quick way of doing this is to copy an existing tool's icon.)

PROC main()
  DEF s[10]:STRING, fh
  fh:=IF stdin THEN stdin ELSE stdout
  WHILE ReadStr(fh, s)<>-1
  EXIT StrCmp(s, 'QUIT', ALL)
    WriteF('Read: \a\s\a\n', s)
There are some worked examples in Part Three (see 19 String Handling and I/O) which also show how to use ReadStr.

Returns the length of the file named in string, or -1 if the file doesn't exist or an error occurred. Notice that you don't need to Open the file or have a filehandle, you just supply the filename. There is a worked example in Part Three (see 19 String Handling and I/O) which shows how to use this function.

Returns the value of stdin before setting it to filehandle. Therefore, the following code fragments are equivalent:



Returns the value of stdout before setting it to filehandle, and is otherwise just like SetStdIn.

Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.