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):
This is the output it generates:PROC main() DEF s:STRING StrCopy(s, 'Hello world', ALL) WriteF('The third element of s is "\c"\n', s) WriteF('or \d (decimal)\n', s) WriteF('or \h (hexadecimal)\n', s) WriteF('and s itself is \a\s\a\n', s) ENDPROC
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 usingThe third element of s is "l" or 108 (decimal) or 6C (hexadecimal) and s itself is 'Hello world'
[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:
Here's the output it should generate:PROC main() DEF s:STRING StrCopy(s, 'Hello world', ALL) WriteF('The third element of s is "\c"\n', s) WriteF('or \d (decimal)\n', s) WriteF('or \z\h (hexadecimal)\n', s) WriteF('\a\s\a are the first five elements of s \n', s) WriteF('and s in a very big field \a\s\a\n', s) WriteF('and s left justified in it \a\l\s\a\n', s) ENDPROC
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 '
WriteFuses the standard output, and this file handle is stored in the
stdoutvariable. If your program is started from Workbench this variable will contain
NIL. In this case, the first call to
WriteFwill open a special output window and put the file handle in the variables
conout, as outlined above (see 11.2 Built-In Variables).
PrintFworks just like
WriteFexcept 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).
WriteFexcept that the result is written to e-string instead of being printed. For example, the following code fragment sets
sto `00123 is a' (since the E-string is not long enough for the whole string):
DEF s:STRING StringF(s, '\z\d is a number', 123)
stdout, in which case the character is written to the standard output. (You need to make sure
NIL, and you can do this by using a
WriteF(")call.) In general, you obtain a filehandle using the Amiga system function
Openfrom the `dos.library' (see 19 String Handling and I/O).
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
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.)
There are some worked examples in Part Three (see 19 String Handling and I/O) which also show how to usePROC main() DEF s:STRING, fh WriteF(") fh:=IF stdin THEN stdin ELSE stdout WHILE ReadStr(fh, s)<>-1 UpperStr(s) EXIT StrCmp(s, 'QUIT', ALL) WriteF('Read: \a\s\a\n', s) ENDWHILE WriteF('Finished\n') ENDPROC
Openthe 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.
stdinbefore setting it to filehandle. Therefore, the following code fragments are equivalent:
oldstdin:=SetStdIn(newstdin) oldstdin:=stdin stdin:=newstdin
stdoutbefore setting it to filehandle, and is otherwise just like
Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.