F

Top  Previous  Next

fabs

#include <math.h>

double fabs (

   double arg

);

stand-alone

fabs returns the absolute value of arg.

fclose

#include <stdio.h>

int fclose (

   FILE *stream

);

server

stack: 256 bytes

fclose causes any buffers for the specified stream to be emptied, and the file to be closed. Buffers allocated by the standard I/O system are freed.

 

fclose returns non-zero if stream is not associated with an output file, or if buffered data cannot be transferred to that file.

 

fclose is called automatically upon calling exit.

feof

#include <stdio.h>

int feof (

   FILE *stream

);

server

feof returns non-zero when end of file is read on the named input stream, otherwise zero.

 

It is implemented as a macro.

ferror

#include <stdio.h>

int ferror (

   FILE *stream

);

server

ferror returns non-zero when an error has occurred reading the named stream, otherwise zero.

 

Unless cleared by clearerr, the error indication lasts until the stream is closed.

 

ferror is implemented as a macro.

fflush

#include <stdio.h>

int fflush (

   FILE *stream

);

server

fflush causes any buffered data for the named output stream to be written to the file or device associated with that stream. The stream remains open.

 

EOF is returned if stream is not associated with an output file or if buffered data cannot be transferred to that file.

 

fflush is called automatically by close, and when all streams are implicitly closed by exit.

fgetc

#include <stdio.h>

int fgetc (

   FILE *stream

);

server

fgetc returns the next character from the specified input stream. Successive calls return successive characters from the stream.

 

EOF is returned at end of file or if a read error occurs.

 

fgetc is a genuine function, unlike getc which is a macro.

fgetpos

#include <stdio.h>

int fgetpos (

   FILE   *stream,

   fpos_t *pos

);

server

fgetpos stores the file position in the object pointed to by pos. The type fpos_t is defined in<stdio.h> .

 

If successful, fgetpos returns zero. If it fails, it returns a non-zero value, and sets errno to EBADF for a bad file descriptor, or EINVAL for any other error.

 

Dragons003In the current version of Diamond, you should use fgetpos only with binary files.

fgets

#include <stdio.h>

char *fgets (

   char *str,

   int   n,

   FILE *stream

);

server

fgets reads a maximum of n-1 characters from the stream and stores them in the string str. Reading stops when a newline has been stored or when end-of-file is encountered. The last character read into str is followed by a NUL character.

 

fgets normally returns str. If an error occurs, or if end-of-file is encountered before any characters have been read, fgets returns NULL.

 

Note that fgets behaves differently from gets with respect to any terminating newline character: fgets keeps the newline, gets deletes it from the string.

floor

#include <math.h>

double floor(double x);

stand-alone

floor returns the largest integer not greater than x, expressed as a floating-point value.

fmod

#include <math.h>

double fmod (

   double x,

   double y

);

stand-alone

fmod returns the remainder from x/y.

fopen

#include <stdio.h>

FILE *fopen (

   const char *filename,

   const char *type

);

server

stack: 300 bytes

fopen opens the file named by filename and associates a stream with it. fopen returns a pointer to be used to identify the stream in subsequent operations; if filename cannot be accessed in the way requested, the function returns NULL.

 

type is a character string made up of the following parts:

 

1.A specification of whether the file is to be opened for reading "r", writing "w" or appending "a". This specifier must appear as the first character in the type string.
2.An optional 'update' specifier "+". If included, the file is opened for both reading and writing. If omitted, the file is opened in the mode described by the first character of type.
3.An optional specification of whether the file is to be a text file, "t", or a binary file "b". If this specifier is omitted, the file is taken to be a text file. Text and binary files are discussed *here*.

 

The second and third parts of the type string may appear in any order. For example, "r+b" and "rb+" are equivalent.

 

Here are some examples of possible values for type, along with a description of their interpretation.


"r"

open text file for reading


"rt"

open text file for reading


"rb"

open binary file for reading


"rb+"

open binary file for update


"r+b"

open binary file for update


"w"

truncate and write to, or create, text file


"a"

append to, or create, text file


"ab"

append to, or create, binary file

 

fopen fails if the file is to be opened for reading ("r") and it does not exist.

 

For writing ("w") or appending ("a"), the file is created if it does not exist.

 

If a file is open to read and write (the type argument includes a "+"), it is not possible to switch directly from reading to writing or vice versa. Instead, there must be a call to fseek between them. If this is not done, the results are undefined.

fprintf

#include <stdio.h>

int fprintf (

   FILE       *stream,

   const char *format,

   ...

);

server

stack: 368 bytes

The arguments that follow the format argument are output to the specified stream, using putc.

 

The format argument is a character string that controls the way in which the following argument list is converted for output.

The format argument contains two types of object: plain characters, which are simply copied to the output stream, and conversion specifications, each of which causes conversion and output of the next argument. Each conversion specification is introduced by the character '%', following which there may be the following, in the given order:

 


Flags Field

This optional field includes any of the following flags, in any order:


-

The value is left-justified.


+

The value always starts with a sign.


space

If the value does not start with a sign, a space is placed before it.


#

Use an 'alternate form' conversion. The alternate forms depend on the conversion character, as follows:


o

Increase specified precision by one character, so that the leading digit is

always 0.


x

Precede non-zero value by "0x"


X, p

Precede non-zero value by "0X"


e,E,f,g,G

Output decimal point even if no digits follow it.


g, G

Do not remove trailing zeroes


0

For the 'd', 'i', 'o', 'u', 'x', 'X', 'p', 'e', 'E', 'f', 'g', 'G' conversion characters, the value is padded with zeroes. If the '-' flag appears as well, '0' is ignored. For the 'd', 'I', 'o', 'u', 'x', 'X', 'p' conversion characters, if a precision is specified, the '0' flag is ignored.


Field Width

This optional field is a decimal integer; or an asterisk '*', indicating that the value for the field width is taken from the next argument, which should be an int.

 

The converted value is padded on the left (or on the right, if the '-' flag has been specified). If a '0' flag is in force, padding uses '0' characters; otherwise, it uses spaces.


Precision

This optional field consists of a '.' and either a decimal integer, or an asterisk, '*'. If it is an asterisk, the value for the precision is taken from the next argument, which should be an int. If only a '.' is specified, the precision is taken as zero. The meaning of the precision depends on the conversion character, as follows:


d, I, o, u, x, X

the minimum number of digits


e, E, f

the number of digits to appear after the decimal point


g, G

the maximum number of significant digits


s

the maximum number of characters to be written from a string


Prefixes

This optional field may contain one of the following:


h

The following 'd', 'i', 'o', 'u', 'x' or 'X' conversion character corresponds to a short int or unsigned short int argument; or, the following n conversion character corresponds to an argument which is a pointer to a short int.


l

The following 'd', 'i', 'o', 'u', 'x' or 'X' conversion character corresponds to a long int or unsigned long int argument; or, the following n conversion character corresponds to an argument which is a pointer to a long int.


L

The following 'e', 'E', 'f', 'g' or 'G' conversion character corresponds to a long double argument.


Conversion Character

The conversion characters and their meanings are:


d, I

The int argument is converted to decimal notation. The default precision is 1.


o, u, x, X

The unsigned int argument is converted to unsigned octal ('o'), unsigned decimal ('u') or unsigned hexadecimal ('x' or 'X'). The default precision is 1. When writing a hexadecimal number, the letters abcdef are used for 'x' conversion, and ABCDEF for 'X' conversion.


F

The double argument is converted to decimal notation in the form '[–]ddd.ddd' where the number of digits after the decimal point is equal to the precision specification for the argument. The default precision is 6.


e, E

The double argument is converted to decimal notation of the form '[–]d.ddde[±]dd'. There is one digit before the decimal point and the number after is equal to the precision specification for the argument; the default precision is 6. The 'e' conversion character generates 'e' as the exponent character, while 'E' generates 'E'.


g, G

The double argument is output in style 'f' or 'e'. The precision specifies the number of significant digits; the default is 1. Style 'e' is used only if the exponent after conversion is less than -4 or greater than or equal to the precision. Style 'E' is used in place of 'e' if 'G' is specified.


C

The int argument is converted to unsigned char and printed.


S

The argument is taken to be a string (character pointer) and characters from the string are printed until a NUL character is reached or until the number of characters indicated by the precision specification is reached; however, if the precision is zero or missing, all characters up to, but not including, a NUL are printed. Note that a NULL pointer is interpreted as an empty string:

fprintf(f, "%s"0); // generates no output


P

The value of the pointer-to-void argument is printed as a hexadecimal number. The default precision is 8.


N

No output is performed. Instead, the number of characters output so far by this call to fprintf is placed in the int variable that the argument points at.


%

Print a '%' character; no argument is converted

 

In no case does a non-existent or small field width cause truncation of a field.

 

The maximum length for a single converted argument is 512 characters.

 

fprintf returns the number of characters output, or a negative value if an output error occurred.

fputc

#include <stdio.h>

int fputc (

   int   cval,

   FILE *stream

);

server

fputc appends the character cval to the specified output stream. It returns the character written. fputc, unlike putc, is a genuine function rather than a macro.

 

fputc returns EOF if an error occurs.

fputs

#include <stdio.h>

int fputs (

   const char *str,

   FILE       *stream

);

server

fputs copies the NUL-terminated string str to the specified output stream. The NUL character that terminates the string is not written to the stream.

 

Note that unlike puts, fputs does not append a newline to the output string.

fread

#include <stdio.h>

size_t fread (

   void   *ptr,

   size_t  size,

   size_t  nitems,

   FILE   *stream

);

server

fread reads nitems objects, each size characters in length, from the specified input stream into memory at location ptr. It returns the number of complete items actually read. Zero is returned on error conditions or end of file.

 

For example, the following code fragment reads ten integer values from the file f into the integer array values:

 

#include <stdio.h>

FILE *f;

int values[10];

fread(values, sizeof(int), 10, f);

 

Note that the size argument specifies the size of an item in characters, that is, in octets (eight-bit bytes).  fread always reads size octets from the file for each item, and the same number of octets are written to memory.

free

#include <stdlib.h>

void free (

   void *ap

);

stand-alone

stack: 32 bytes

free returns to the heap memory pointed to by ap, which must have been obtained originally by a call to malloc, memalign, calloc or realloc. If ap is a null pointer, no action is taken.

 

It is an error to attempt to free space that was not allocated by a call to malloc, memalign, calloc or realloc.

freopen

#include <stdio.h>

FILE *freopen (

   const char *filename,

   const char *type,

   FILE       *stream

);

server

freopen substitutes the named file filename in place of the open stream. It returns the original value of stream. The original stream is closed.

 

freopen is typically used to attach the pre-opened constant names stdin, stdout and stderr to specified files. type is a character string specifying the way in which the file is to be opened. Refer to the description of fopen for a full description of the type string.

 

freopen returns the pointer NULL if filename cannot be accessed.

frexp

#include <math.h>

double frexp (

   double  value,

   int    *exp

);

stand-alone

frexp breaks value into its normalized fraction and an integral power of 2. The function returns the fractional part and the integral part is stored in the variable pointed to by exp.

fscanf

#include <stdio.h>

int fscanf (

   FILE       *stream,

   const char *format,

   ...

);

server

fscanf reads characters from the specified input stream, interprets them according to a format string and stores the results in the variables pointed to by the arguments following format.

 

The format string is regarded as a sequence of directives, which are processed one by one. fscanf tries to match each directive with characters read from the input stream; the way in which this matching is done depends on the directive. If a directive does not match characters from the input stream, we say that a matching error has happened and the character which caused the error is not read; fscanf returns at once.

 

There are three types of directive:

 

1.White space of any length matches white space of any length. If the input stream does not have white space at this point, the directive is ignored.
2.A conversion specification, which is a sequence of characters starting with a '%'. These are discussed below.
3.Any other character matches the next character of the input stream if they are the same.

 

A conversion specification consists of the following, in this order:

 

1.The character '%';
2.An optional character '*', indicating that the converted value is not to be stored;
3.The field width: an optional non-zero integer which specifies the maximum allowable width of the input field;
4.A prefix character, which indicates the type of the associated argument, as shown in the following table (note that on the C6000, double and long double variables are represented in the same way):

 


Prefix

Conversion characters


'd', 'i', 'n'

'o', 'u', 'x'

'e', 'f', 'g'


h

short int

unsigned short int

 


l

long int

unsigned long int

double


L

 

 

long double

 

5.One of the conversion specifiers listed below.

 

Each conversion specification matches a sequence of characters of a particular format, and these characters are read from the input stream. Reading stops when the first character that does not fit into this format is encountered; this character is not read. It is a matching error if no characters are read, that is, if not even one character would fit the assumed format for this specification.

 

The character sequence that has been read is converted in one of a variety of ways, and the resulting internal value is stored in the variable pointed to by the next argument (unless '*' was included in the specifier). If this variable is not of an appropriate type for the value that has been converted, the effect is undefined.

 

The following specifiers are recognized:

 


%

Match a '%' character. The complete specifier must be '%%'.

 

No argument is used.


c

Match a sequence of characters of the length specified in the field width (1 by default).  Note that unlike 's', the 'c' specifier does not skip white space; to read the next non-space character, use '%1s'.

 

The argument should be a pointer to an array of characters large enough to accept the string.


d

Match an optionally-signed decimal integer.

 

The argument should be a pointer to an integer


e, f, g

Match a floating-point number with a format such as would be acceptable to the strtod function. This means an optionally-signed string of digits, possibly containing a decimal point, followed by an optional exponent field consisting of an 'E' or 'e' followed by an optionally-signed integer.

 

The argument should be a pointer to a floating-point variable.


i

Match an optionally-signed integer with a format such as would be acceptable to the strtol function with a base value of 0. This means that strings starting with "0x" or "0X" are interpreted as hexadecimal, strings starting with '0' are interpreted as octal, and others as decimal.

 

The argument should be a pointer to an integer.


n

The n specifier reads no characters.

 

The argument should be a pointer to an integer, and in this integer is written the number of characters read so far by this call to fscanf.


o

Matches an optionally-signed octal integer.

 

The argument should be a pointer to an integer.


p

Matches a pointer value of the format output by a p specifier in a fprintf function call.

 

The argument should be a pointer to a pointer to void.


s

Matches a character string which includes no white space.

 

The argument should be a pointer to an array of characters large enough to accept the string and a terminating NUL character, which will be added.


u

Matches an optionally-signed decimal integer.

 

The argument should be a pointer to an unsigned integer.


x

Matches an optionally-signed hexadecimal integer with a format such as would be acceptable to the strtoul function with a base value of 16. This means that the string may, but need not, start with "0x" or "0X".

 

The argument should be a pointer to an integer.


[

This specifier includes all the characters from the '[' up to a later ']'. The characters between the brackets are called the scan-set. The specifier matches a sequence of characters all of which are members of the scanset. So, for example, '[aeiou]' would match a sequence of vowels, of any length and in any order.

 

If the first character of the scan-set is a '^', then the specifier matches a sequence of characters none of which are members of the scan-set. To enable the scan-set to include a ']', the standard provides that if the scan-set starts with ']' or '^]' this does not end the specifier and another ']' is needed. In other words, '[])>]' is a valid specifier, defining a scan-set consisting of ']', ')' and '>'.

 

The argument should be a pointer to an array of characters large enough to accept the resulting sequence.

 

The conversion specifiers 'E', 'G' and 'X' are treated as being equivalent to 'e', 'g', and 'x'. In addition, for compatibility purposes only, 'F' is accepted as being equivalent to 'lf', that is, a floating-point conversion that expects a pointer to double as the argument. If end-of-file or an input error occurs before any conversion is done, fscanf returns 'EOF'. Otherwise, it returns the number of input items successfully converted and stored. The specifier n and specifiers including a '*' do not count towards this number.

fseek

#include <stdio.h>

int fseek (

   FILE     *stream,

   long int  offset,

   int       whence

);

server

stack: 280 bytes

fseek sets the file position indicator of the specified stream. The new position is at the signed distance offset characters from a location specified in whence.

 

Three macros are provided for specifying whence:

 


SEEK_SET

the start of the file


SEEK_CUR

the current file position


SEEK_END

the end of the file

 

fseek undoes any effects of ungetc; that is, characters which have been 'pushed back' into a stream by ungetc will not subsequently be read. Instead, reading proceeds from the new file position.

 

fseek returns -1 for improper seeks, or zero for normal completion.

 

When operating on a text file, fseek's arguments are limited in the following ways:

 

offset may only be 0.
whence may only be SEEK_SET or SEEK_END.

 

The ANSI standard also allows fseek to be applied to a text file with whence set to SEEK_CUR and offset set to a value previously found by applying ftell to the same stream. The current version of Diamond does not support this.

fsetpos

#include <stdio.h>

int fsetpos (

   FILE         *stream,

   const fpos_t *pos

);

server

fsetpos sets the file position of the specified stream to the position stored in the object pointed to by pos. This value should have been stored by an earlier call to fgetpos.

 

If successful, fsetpos returns zero. If it fails, it returns a non-zero value, and sets errno to EBADF for a bad file descriptor, or EINVAL for any other error.

 

Dragons003In the current version of Diamond, you should use fsetpos only with binary files.

ftell

#include <stdio.h>

long int ftell (

   FILE *stream

);

server

stack: 188 bytes

ftell returns the current value of the offset relative to the beginning of the file associated with the named stream. This offset is measured in characters.

When operating on a text file, ftell may not give an accurate position unless the current position is either at the beginning or the end of the file.

fwrite

#include <stdio.h>

size_t fwrite (

   const void *ptr,

   size_t      size,

   size_t      nitems,

   FILE       *stream

);

server

fwrite writes nitems objects, each of size characters, from memory at location ptr to the specified output stream.

 

It returns the number of complete items actually written. Zero is returned on error conditions.

 

For example, the following code fragment writes the contents of the int array values into the file data:

 

#include <stdio.h>

FILE *data;

int values[10];

fwrite(values, sizeof(int), 10, data);

 

Note that the size argument specifies the size of an item in characters, that is, in octets (eight-bit bytes).  fwrite always takes size octets from memory for each item, and the same number of octets are written to the file.