old-www/LDP/LG/issue32/rogers.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: &lt;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
&lt;stdio.h>.&nbsp; This article is on formatted input and output.&nbsp;&nbsp;
I am assuming a knowledge of c programming on the part of the reader.&nbsp;
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.&nbsp; We will subtotal the items by label and print
out the subtotal with its label along with a total for all subtotals.&nbsp;
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 &lt;angle bracked> items are replaced with real code.&nbsp; Normally
these are items that have to be treated differently depending on what you
are trying to do.&nbsp; As always, if you see an error in my documentation
please tell me and I will correct myself in a later document.&nbsp; 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 &lt;stdio.h&gt;</TT>
<P><TT>int&nbsp;&nbsp; printf(const char *format, ...);</TT>
<BR><TT>int&nbsp; fprintf(FILE *stream, const char *format, ...);</TT>
<BR><TT>int&nbsp; sprintf(char *str, const char *format, ...);</TT>
<BR><TT>int snprintf(char *str, size_t size, const&nbsp; char&nbsp; *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",&nbsp; dateString, messageString,
errorNumber);</TT>
<P>Could print to stderr, "DEBUG&nbsp;&nbsp;&nbsp; 199808291055&nbsp;&nbsp;&nbsp;
you are here&nbsp;&nbsp;&nbsp; 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'.&nbsp;&nbsp; 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>&nbsp;
<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 &lt;stdio.h&gt;</TT>
<P><TT>int&nbsp; 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&nbsp;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.&nbsp; A more general desription
is as follows:
<P>A format string contains zero or more of the following conversion specifications:
<BR>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
A conversion specification is introduced with this required character.
<BR>flags&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
followed by zero or more flags.
<BR>width&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
followed by an optional width field.
<BR>precision&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; followed by an optional precision
field.
<BR>argument&nbsp;&nbsp;&nbsp;&nbsp; followed by an optional argument that
differs by specific conversions.
<BR>conversion&nbsp;&nbsp; ending in a required conversion type.
<P><B>Flags</B>
<BR>&nbsp;&nbsp;&nbsp; These are used to change the default behavior of
the conversion.
<P><B>-&nbsp;&nbsp;</B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
left justify the field.
<BR><B>+&nbsp;</B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
used a sign with a number conversion.
<BR><B>#</B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
use 0 in front of octal conversions, 0x in front of hex conversions and
a decimal point with decimal conversions.
<BR><B>0</B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
pad number conversions with leading zeros, ignore if a - is present.
<BR><B>space&nbsp;</B>&nbsp;&nbsp;&nbsp; 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>&nbsp;&nbsp;&nbsp; The number of characters wide a field is.&nbsp;
Spaces are used to pad the extra characters if a value is not as wide as
the given width.&nbsp; If the value is larger than this number the field
will expand to fit the number.
<P><B>Precision</B>
<BR>&nbsp;&nbsp;&nbsp; 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.&nbsp; This takes
the form of a decimal point followed by an optional number.&nbsp; If no
number is given the precision defaults to 0.
<P><B>Argument</B>
<BR>&nbsp;&nbsp;&nbsp; These will be discussed with each relevant conversion.&nbsp;
They are h, l, or L.
<P><B>Conversions</B>
<P><B>d&nbsp;</B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int to signed decimal&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
-9999 or 99
<BR><B>i</B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int to signed decimal&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
-9999 or 99
<BR><B>o</B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unsigned int to unsigned
octal&nbsp;&nbsp;&nbsp;&nbsp; 8 becomes 10
<BR><B>u</B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unsigned int to decimal&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
1 becomes 1
<BR><B>x&nbsp;</B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unsigned int to hexidecimal&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
13 becomes 1d
<BR><B>X&nbsp;</B>&nbsp;&nbsp;&nbsp;&nbsp; unsigned int to hexidecimal&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; double to decimal&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
-9999.99 or 99.9
<BR><B>e&nbsp;</B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; double to scientific notation&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
-9.999e+99&nbsp; or 9.9e-9
<BR><B>E</B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; double to scientific notation&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
-9.999E+99 or 9.9E-9
<BR><B>g</B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; double to decimal (f)
or scientific (e) if the # of digits are equal to or greater than the precision.
<BR><B>G</B>&nbsp;&nbsp;&nbsp;&nbsp; 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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int to unsigned char&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
65 becomes an A in standard ASCII
<BR><B>s</B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pointer to string prints
out the string
<BR><B>n&nbsp;</B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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>&nbsp;&nbsp;&nbsp; a %% will write out a single %.&nbsp; 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 &lt;hesdorf@ibm.net&gt;</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.&nbsp; 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.&nbsp; One more problem with all of those examples,&nbsp;
STDOUT and STDIN are suposed to be lower case; stdout and stdin.&nbsp;
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 &copy; 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 ============================================================-->