311 lines
13 KiB
HTML
311 lines
13 KiB
HTML
<!--startcut ==========================================================-->
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
|
<HTML>
|
|
<HEAD>
|
|
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
|
|
<META NAME="GENERATOR" CONTENT="Mozilla/4.03 [en] (X11; I; Linux 2.0.32 i586) [Netscape]">
|
|
<META NAME="Author" CONTENT="James M. Rogers">
|
|
<META NAME="Description" CONTENT="This article is the second in a series designed to explore the Standard C library implementation available for Linux">
|
|
<META NAME="Keywords" CONTENT="linux, standard c library, character functions">
|
|
<META NAME="Classification" CONTENT="Second Year Programming">
|
|
<TITLE>The Standard C Library for Linux Issue 32</TITLE>
|
|
<!--startcut ==========================================================-->
|
|
</HEAD>
|
|
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000FF" VLINK="#A000A0" ALINK="#FF0000">
|
|
<!--endcut ============================================================-->
|
|
|
|
<H4>
|
|
"Linux Gazette...<I>making Linux just a little more fun!</I>"
|
|
</H4>
|
|
|
|
<P> <HR> <P>
|
|
<!--===================================================================-->
|
|
<CENTER>
|
|
<H1>
|
|
<FONT COLOR="#800000">The Standard C Library for Linux</FONT></H1></CENTER>
|
|
|
|
<CENTER>
|
|
<H3>
|
|
<FONT COLOR="#000080">Part Three: <stdio.h> formatted input/output</FONT></H3></CENTER>
|
|
|
|
<CENTER>
|
|
<H4>
|
|
By <A HREF="mailto:jrogers@u.washington.edu">James M. Rogers</A></H4></CENTER>
|
|
|
|
<HR>
|
|
|
|
<P>The last article was on character I/O in the standard input/output library
|
|
<stdio.h>. This article is on formatted input and output.
|
|
I am assuming a knowledge of c programming on the part of the reader.
|
|
There is no guarantee of accuracy in any of this information nor suitability
|
|
for any purpose.
|
|
|
|
<P>As an example of formatted input output we will read in a file containing
|
|
a number and a label. We will subtotal the items by label and print
|
|
out the subtotal with its label along with a total for all subtotals.
|
|
The example is <A HREF="rogers_example3.c">example3.c</A>
|
|
and the data file is <A HREF="rogers_example3.dat">example3.dat</A><TT>.</TT>
|
|
|
|
<P>The code examples given for each function will typically not run unless
|
|
the the <angle bracked> items are replaced with real code. Normally
|
|
these are items that have to be treated differently depending on what you
|
|
are trying to do. As always, if you see an error in my documentation
|
|
please tell me and I will correct myself in a later document. See
|
|
corrections at end of the document to review corrections to the previous
|
|
articles.
|
|
|
|
<P><B><FONT SIZE=+1>Formatted Output</FONT></B>
|
|
<UL><TT>#include <stdio.h></TT>
|
|
|
|
<P><TT>int printf(const char *format, ...);</TT>
|
|
<BR><TT>int fprintf(FILE *stream, const char *format, ...);</TT>
|
|
<BR><TT>int sprintf(char *str, const char *format, ...);</TT>
|
|
<BR><TT>int snprintf(char *str, size_t size, const char *format,
|
|
...);</TT></UL>
|
|
<TT>const char *format</TT> is a format that will be used to process the
|
|
following arguments.
|
|
<BR><TT>...</TT> is a variable number of arguments that must match the
|
|
number of arguments used in const char *format.
|
|
<BR><TT>FILE *stream</TT> is a previously fopened stream.
|
|
<BR><TT>char *str</TT> is a string.
|
|
<BR><TT>size_t size</TT> is the maximum size of string that will be produced,
|
|
any excess is lost.
|
|
|
|
<P>p<B>rintf</B> is used to print out a formatted sequence of characters
|
|
to standard out.
|
|
|
|
<P><TT>int x=5;</TT>
|
|
<BR><TT>printf("We have %d apples.\n", x);</TT><TT></TT>
|
|
|
|
<P>This example prints to stdout "We have 5 apples." with a newline following.
|
|
|
|
<P><B>fprintf</B> is used to print out a formatted sequence of characters
|
|
to a file.
|
|
|
|
<P><TT>fprintf(stderr, "DEBUG\t%s\t%s\t%f", dateString, messageString,
|
|
errorNumber);</TT>
|
|
|
|
<P>Could print to stderr, "DEBUG 199808291055
|
|
you are here 1234.4567" followed by a newline.
|
|
|
|
<P><B>sprintf</B> is used to print out a formatted sequence of characters
|
|
to a character array.<TT></TT>
|
|
|
|
<P><TT>float x=99.1234;</TT>
|
|
<BR><TT>sprintf(string, "%d", x)</TT><TT></TT>
|
|
|
|
<P>This would create a string that contained the characters '9', '9', '.',
|
|
'1','2','3','4'. This is the reverse to the atoi function that
|
|
we will cover next month.
|
|
|
|
<P><B>snprint</B> is used to print out a formatted sequence of characters
|
|
to a string.
|
|
<BR>
|
|
<BR><TT>float x=99.1234;</TT>
|
|
<BR><TT>returnValue=sprintf(string, 4, "%d", x)</TT>
|
|
|
|
<P>This will create a string with the characters '9','9','.','1', returnValue
|
|
will contain a -1 because the field was truncated.
|
|
|
|
<P><B><FONT SIZE=+1>Formatted Input</FONT></B>
|
|
<UL><TT>#include <stdio.h></TT>
|
|
|
|
<P><TT>int scanf( const char *format, ...);</TT>
|
|
<BR><TT>int fscanf( FILE *stream, const char *format, ...);</TT>
|
|
<BR><TT>int sscanf( const char *str, const char *format, ...);</TT></UL>
|
|
<TT>const char *format</TT> is a format that will be used to process the
|
|
following arguments.
|
|
<BR><TT>...</TT> is a variable number of arguments that must match the
|
|
number of arguments used in const char *format.
|
|
<BR><TT>FILE *stream</TT> is a previously fopened stream.
|
|
<BR><TT>char *str</TT> is a string.
|
|
<BR><TT>size_t size</TT> is the maximum size of string that will be produced,
|
|
any excess is lost.
|
|
|
|
<P>These functions return an EOF on a read error, or the number of
|
|
items that were converted, zero or more.
|
|
|
|
<P>scanf will read in a format from standard input.
|
|
|
|
<P><TT>scanf("%f%2d%d", float1, int1, int2)</TT><TT></TT>
|
|
|
|
<P>with the following on stdin:
|
|
|
|
<P><TT>12.34 4567</TT>
|
|
|
|
<P>will set float1=12.34, int1=45 and int2=67.
|
|
|
|
<P>fscanf will read in a format from the given stream.
|
|
|
|
<P><TT>fscanf(stdin,"%f%2d%d", float1, int1, int2)</TT>
|
|
|
|
<P>This example is equivilent to the scanf example.
|
|
|
|
<P>sscanf will read a format from the given string.
|
|
|
|
<P><TT>sscanf(string, "%f%2d%d", float1, int1, int2)</TT>
|
|
|
|
<P>Will scan string for the float and decimal values.
|
|
|
|
<P><B><FONT SIZE=+1>Format Strings</FONT></B>
|
|
|
|
<P>The format will look like the above examples. A more general desription
|
|
is as follows:
|
|
|
|
<P>A format string contains zero or more of the following conversion specifications:
|
|
<BR>%
|
|
A conversion specification is introduced with this required character.
|
|
<BR>flags
|
|
followed by zero or more flags.
|
|
<BR>width
|
|
followed by an optional width field.
|
|
<BR>precision followed by an optional precision
|
|
field.
|
|
<BR>argument followed by an optional argument that
|
|
differs by specific conversions.
|
|
<BR>conversion ending in a required conversion type.
|
|
|
|
<P><B>Flags</B>
|
|
<BR> These are used to change the default behavior of
|
|
the conversion.
|
|
|
|
<P><B>- </B>
|
|
left justify the field.
|
|
<BR><B>+ </B>
|
|
used a sign with a number conversion.
|
|
<BR><B>#</B>
|
|
use 0 in front of octal conversions, 0x in front of hex conversions and
|
|
a decimal point with decimal conversions.
|
|
<BR><B>0</B>
|
|
pad number conversions with leading zeros, ignore if a - is present.
|
|
<BR><B>space </B> if a space follows the % then
|
|
a space will be placed before the output, ignore if a - is present.<B></B>
|
|
|
|
<P><B>Field Width</B>
|
|
<BR> The number of characters wide a field is.
|
|
Spaces are used to pad the extra characters if a value is not as wide as
|
|
the given width. If the value is larger than this number the field
|
|
will expand to fit the number.
|
|
|
|
<P><B>Precision</B>
|
|
<BR> The minimum number of digits to appear for integer
|
|
types or the number of digits to apear after a floating point number or
|
|
the maximum number of characters to print from a string. This takes
|
|
the form of a decimal point followed by an optional number. If no
|
|
number is given the precision defaults to 0.
|
|
|
|
<P><B>Argument</B>
|
|
<BR> These will be discussed with each relevant conversion.
|
|
They are h, l, or L.
|
|
|
|
<P><B>Conversions</B>
|
|
|
|
<P><B>d </B> int to signed decimal
|
|
-9999 or 99
|
|
<BR><B>i</B> int to signed decimal
|
|
-9999 or 99
|
|
<BR><B>o</B> unsigned int to unsigned
|
|
octal 8 becomes 10
|
|
<BR><B>u</B> unsigned int to decimal
|
|
1 becomes 1
|
|
<BR><B>x </B> unsigned int to hexidecimal
|
|
13 becomes 1d
|
|
<BR><B>X </B> unsigned int to hexidecimal
|
|
13 becomes 1D
|
|
<BR><B>h</B> is used in front of the above 6 to convert
|
|
from the short int and unsigned short int to whatever.
|
|
<BR><B>l</B> is used in front of the above 6 to convert
|
|
from the long int and unsigned long int to whatever.
|
|
<BR><B>f</B> double to decimal
|
|
-9999.99 or 99.9
|
|
<BR><B>e </B> double to scientific notation
|
|
-9.999e+99 or 9.9e-9
|
|
<BR><B>E</B> double to scientific notation
|
|
-9.999E+99 or 9.9E-9
|
|
<BR><B>g</B> double to decimal (f)
|
|
or scientific (e) if the # of digits are equal to or greater than the precision.
|
|
<BR><B>G</B> double to decimal (f) or scientific
|
|
(E) if the # of digits are equal to or greater then the precision.
|
|
<BR><B>L</B> is used in front of the above 5 to convert
|
|
from long double to whatever.
|
|
<BR><B>c</B> int to unsigned char
|
|
65 becomes an A in standard ASCII
|
|
<BR><B>s</B> pointer to string prints
|
|
out the string
|
|
<BR><B>n </B> pointer to an int into
|
|
which the number of characters already written to stream will be placed.
|
|
<BR><B>h</B> is used in front of the above 1 to specify
|
|
a short int.
|
|
<BR><B>l</B> is used in front of the above 1 to specify
|
|
a long int.
|
|
<BR><B>%</B> a %% will write out a single %. This
|
|
is the way to print a % when needed.
|
|
|
|
<P>
|
|
<HR>
|
|
|
|
<P>A correction to Part Two:
|
|
<UL><TT>Date: Wed, 12 Aug 1998 11:27:08 +0200</TT>
|
|
<BR><TT>From: Lars Hesdorf <hesdorf@ibm.net></TT>
|
|
<BR><TT>To: jrogers@u.washington.edu</TT>
|
|
<BR><TT>Subject: The Standard C Library for Linux, Part Two"</TT>
|
|
|
|
<P><TT>Hej James M. Rogers</TT>
|
|
|
|
<P><TT>You wrote somewhere in "The Standard C Library for Linux, Part Two"</TT>
|
|
|
|
<P><TT>"putchar writes a character to standard out. putchar(x) is
|
|
the same as</TT>
|
|
<BR><TT>fputc(x, STDIN)"</TT>
|
|
|
|
<P><TT>You probably meant "...fputc(x, STDOUT)".</TT>
|
|
|
|
<P><TT>Lars Hesdorf</TT>
|
|
<BR><TT>HESDORF@IBM.NET</TT></UL>
|
|
Yes I did, thanks a lot. One more problem with all of those examples,
|
|
STDOUT and STDIN are suposed to be lower case; stdout and stdin.
|
|
Sorry if this caused anyone any frustrations.
|
|
|
|
<P>
|
|
<HR>
|
|
<H4>
|
|
Bibilography:</H4>
|
|
<I>The ANSI C Programming Language, Second Edition</I>, Brian W. Kernighan,
|
|
Dennis M. Ritchie, Printice Hall Software Series, 1988
|
|
|
|
<P><I>The Standard C Library</I>, P. J. Plauger, Printice Hall P T R, 1992
|
|
|
|
<P><I>The Standard C Library, Parts 1, 2, and 3</I>, Chuck Allison, <I>C/C++
|
|
Users Journal</I>, January, February, March 1995
|
|
|
|
<P>STDIO(3), BSD MANPAGE, <I>Linux Programmer's Manual</I>, 29 November
|
|
1993
|
|
<P><HR><P>
|
|
<center><H4>Previous "The Standard C Library for Linux" Articles</H4></center>
|
|
<P><A HREF="http://www.linuxgazette.com/issue24/rogers.html"><I>The Standard
|
|
C Library for Linux, Part One</I>, James M. Rogers, January 1998</A>
|
|
|
|
<P><A HREF="http://www.linuxgazette.com/issue31/rogers1.html"><I>The Standard
|
|
C Library for Linux, Part Two</I>, James M. Rogers, January 1998</A>
|
|
|
|
<!--===================================================================-->
|
|
<P> <hr> <P>
|
|
|
|
<center><H5>Copyright © 1998, James M. Robers <BR>
|
|
Published in Issue 32 of <i>Linux Gazette</i>, September 1998</H5></center>
|
|
|
|
<!--===================================================================-->
|
|
<P> <hr> <P>
|
|
<A HREF="./index.html"><IMG ALIGN=BOTTOM SRC="../gx/indexnew.gif"
|
|
ALT="[ TABLE OF CONTENTS ]"></A>
|
|
<A HREF="../index.html"><IMG ALIGN=BOTTOM SRC="../gx/homenew.gif"
|
|
ALT="[ FRONT PAGE ]"></A>
|
|
<A HREF="./williams.html"><IMG SRC="../gx/back2.gif"
|
|
ALT=" Back "></A>
|
|
<A HREF="./lg_backpage32.html"><IMG SRC="../gx/fwd.gif" ALT=" Next "></A>
|
|
<P> <hr> <P>
|
|
<!--startcut ==========================================================-->
|
|
</BODY>
|
|
</HTML>
|
|
<!--endcut ============================================================-->
|