Standard C Libraries -. More...
Go to the source code of this file.
Macros | |
#define | FILE FILE |
#define | fpos_t fpos_t |
#define | size_t size_t |
#define | _IOFBF _IOFBF |
#define | _IOLBF _IOLBF |
#define | _IONBF |
#define | BUFSIZ |
#define | EOF |
#define | FILENAME_MAX |
#define | FOPEN_MAX |
#define | L_tmpnam |
#define | NULL (0) |
#define | SEEK_CUR |
#define | SEEK_END |
#define | SEEK_SET |
#define | stderr stderr |
#define | stdin stdin |
#define | stdout stdout |
#define | TMP_MAX |
Functions | |
void | clearerr (FILE *stream) |
int | fclose (FILE *stream) |
int | feof (FILE *stream) |
int | ferror (FILE *stream) |
int | fflush (FILE *stream) |
int | fgetc (FILE *stream) |
int | fgetpos (FILE *stream, fpos_t *pos) |
char * | fgets (char *s, int n, FILE *stream) |
FILE * | fopen (const char *filename, const char *mode) |
int | fprintf (FILE *stream, const char *format,...) |
int | fputc (int c, FILE *stream) |
int | fputs (const char *s, FILE *stream) |
size_t | fread (void *ptr, size_t size, size_t nelem, FILE *stream) |
FILE * | freopen (const char *filename, const char *mode, FILE *stream) |
int | fscanf (FILE *stream, const char *format,...) |
int | fseek (FILE *stream, long offset, int mode) |
int | fsetpos (FILE *stream, const fpos_t *pos) |
long | ftell (FILE *stream) |
size_t | fwrite (const void *ptr, size_t size, size_t nelem, FILE *stream) |
int | getc (FILE *stream) |
int | getchar (void) |
char * | gets (char *s) |
void | perror (const char *s) |
int | printf (const char *format,...) |
int | putc (int c, FILE *stream) |
int | putchar (int c) |
int | puts (const char *s) |
int | remove (const char *filename) |
int | rename (const char *old, const char *new) |
void | rewind (FILE *stream) |
int | scanf (const char *format,...) |
void | setbuf (FILE *stream, char *buf) |
int | setvbuf (FILE *stream, char *buf, int mode, size_t size) |
int | sprintf (char *s, const char *format,...) |
int | sscanf (const char *s, const char *format,...) |
FILE * | tmpfile (void) |
char * | tmpnam (char *s) |
int | ungetc (int c, FILE *stream) |
int | vfprintf (FILE *stream, const char *format, va_list ap) |
int | vprintf (const char *format, va_list ap) |
int | vsprintf (char *s, const char *format, va_list ap) |
Standard C Libraries -.
The header file, stdio.h, consists of types, macros and functions that provide support to perform input and output operations on files and streams. When a file is opened it is associated with a stream. A stream is a pipeline for the flow of data into and out of files. Because different systems use different properties, the stream provides more uniform properties to allow reading and writing of the files.
Streams can be text streams or binary streams. Text streams consist of a sequence of characters divided into lines. Each line is terminated with a newline ('\n') character. The characters may be altered in their internal representation, particularly in regards to line endings. Binary streams consist of sequences of bytes of information. The bytes transmitted to the binary stream are not altered. There is no concept of lines - the file is just a series of bytes.
At start-up three streams are automatically opened: stdin, stdout, and stderr. stdin provides a stream for standard input, stdout is standard output and stderr is the standard error. Additional streams may be created with the fopen function. See fopen for the different types of file access that are permitted. These access types are used by fopen and freopen.
The type FILE is used to store information about each opened file stream. It includes such things as error indicators, end-of-file indicators, file position indicators, and other internal status information needed to control a stream. Many functions in the stdio use FILE as an argument.
There are three types of buffering: unbuffered, line buffered and fully buffered. Unbuffered means a character or byte is transferred one at a time. Line buffered collects and transfers an entire line at a time (i.e., the newline character indicates the end of a line). Fully buffered allows blocks of an arbitrary size to be transmitted. The functions, setbuf and setvbuf, control file buffering.
The stdio.h file also contains functions that use input and output formats. The input formats, or scan formats, are used for reading data. Their descriptions can be found under scanf, but they are also used by fscanf and sscanf. The output formats, or print formats, are used for writing data. Their descriptions can be found under printf. These print formats are also used by fprintf, sprintf, vfprintf, vprintf and vsprintf.
Certain compiler options may affect how standard I/O performs. In an effort to provide a more tailored version of the formatted I/O routines, the tool chain may convert a call to a printf or scanf style function to a different call. The options are summarized below:
Mixing modules compiled with these options may result in a larger executable size, or incorrect execution if large and small double-sized data is shared across modules.
The standard I/O relies on helper functions described in Chapter 4. "Standard C Libraries - Support Functions". These functions include read(), write(), open(), and close() which are called to read, write, open or close handles that are associated with standard I/O FILE pointers. The sources for these libraries are provided for you to customize as you wish.
The simplest way to redirect standard I/O to the peripheral of your choice is to select one of the default handles already in use. Also, you could open files with a specific name, via fopen(), by rewriting open() to return a new handle to be recognized by read() or write(), as appropriate.
If only a specific peripheral is required, then you could associate handle 1 == stdout, or 2 == stderr, to another peripheral by writing the correct code to talk to the interested peripheral. A complete generic solution might be:
Single letters were used in this example because they are faster to check and use less memory. However, if memory is not an issue, you could use strcmp to compare full names. In write(), you would write:
where you would fill in the appropriate code as specified in the comments. Now you can use the generic C STDIO features to write to another port:
The documentation in this header file has been copied from the documentation provided with the Microchip MPLAB XC16 compiler. The original license agreement included with the XC16 compiler applies!
#define _IOFBF _IOFBF |
#define _IOLBF _IOLBF |
#define _IONBF |
#define BUFSIZ |
Description: Defines the size of the buffer used by the function, setbuf.
Include: <stdio.h>
Value: 512
#define EOF |
Description: A negative number indicating the end-of-file has been reached or to report an error condition.
Include: <stdio.h>
Remarks:
If an end-of-file is encountered, the end-of-file indicator is set. If an error condition is encountered, the error indicator is set. Error conditions include write errors and input or read errors.
#define FILE FILE |
Description: Stores information for a file stream.
Include: <stdio.h>
#define FILENAME_MAX |
Description: Maximum number of characters in a filename including the null terminator.
Include: <stdio.h>
Value: 260
#define FOPEN_MAX |
Description: Defines the maximum number of files that can be simultaneously open.
Include: <stdio.h>
Value: 8
Remarks:
stderr, stdin and stdout are included in the FOPEN_MAX count.
#define fpos_t fpos_t |
Description: Type of a variable used to store a file position.
Include: <stdio.h>
#define L_tmpnam |
Description: Defines the number of characters for the longest temporary filename created by the function, tmpnam.
Include: <stdio.h>
Value: 16
Remarks:
L_tmpnam is used to define the size of the array used by tmpnam.
#define NULL (0) |
Description: The value of a null pointer constant.
Include: <stdio.h>
#define SEEK_CUR |
Description: Indicates that fseek should seek from the current position of the file pointer.
Include: <stdio.h>
Example:
See example for fseek.
#define SEEK_END |
Description: Indicates that fseek should seek from the end of the file.
Include: <stdio.h>
Example:
See example for fseek.
#define SEEK_SET |
Description: Indicates that fseek should seek from the beginning of the file.
Include: <stdio.h>
Example:
See example for fseek.
#define stderr stderr |
Description: File pointer to the standard error stream.
Include: <stdio.h>
#define stdin stdin |
Description: File pointer to the standard input stream.
Include: <stdio.h>
#define stdout stdout |
Description: File pointer to the standard output stream.
Include: <stdio.h>
#define TMP_MAX |
Description: The maximum number of unique filenames the function tmpnam can generate.
Include: <stdio.h>
Value: 32
void clearerr | ( | FILE * | stream) |
Description: Resets the error indictor for the stream.
Include: <stdio.h>
stream | stream to reset error indicators |
Remarks:
The function clears the end-of-file and error indicators for the given stream (i.e., feof and ferror will return false after the function clearerr is called).
Example:
Output:
Error
Error indicator reset
int fclose | ( | FILE * | stream) |
Description: Close a stream.
Include: <stdio.h>
stream | pointer to the stream to close |
Remarks:
fclose writes any buffered output to the file.
Example:
Output:
afile1 was opened
afile1 was closed
int feof | ( | FILE * | stream) |
Description: Tests for end-of-file.
Include: <stdio.h>
stream | stream to check for end-of-file |
Example:
Input:
Contents of afile.txt (used as input): This is a sentence. Output:
This is a sentence.
int ferror | ( | FILE * | stream) |
Description: Tests if error indicator is set.
Include: <stdio.h>
stream | pointer to FILE structure |
Example:
Output:
Error
Error indicator reset
int fflush | ( | FILE * | stream) |
Description: Flushes the buffer in the specified stream.
Include: <stdio.h>
stream | pointer to the stream to flush. |
Remarks:
If stream is a null pointer, all output buffers are written to files. fflush has no effect on an unbuffered stream.
int fgetc | ( | FILE * | stream) |
Description: Get a character from a stream
Include: <stdio.h>
stream | pointer to the open stream |
Remarks:
The function reads the next character from the input stream, advances the file-position indicator and returns the character as an unsigned char converted to an int.
Example:
Input:
Contents of afile.txt (used as input): Short Longer string Output:
S|h|o|r|t|
|L|o|n|g|e|r| |s|t|r|i|n|g|
Description: Gets the stream's file position.
Include: <stdio.h>
stream | target stream |
pos | position-indicator storage |
Remarks:
The function stores the file-position indicator for the given stream in pos if successful, otherwise, fgetpos sets errno.
Example:
Output:
Bytes read: program opens a file
Bytes read: and reads bytes at
Bytes read: program opens a file
char* fgets | ( | char * | s, |
int | n, | ||
FILE * | stream | ||
) |
Description: Get a string from a stream.
Include: <stdio.h>
s | pointer to the storage string |
n | maximum number of characters to read |
stream | pointer to the open stream. |
Remarks:
The function reads characters from the input stream and stores them into the string pointed to by s until it has read n-1 characters, stores a newline character or sets the end-of-file or error indicators. If any characters were stored, a null character is stored immediately after the last read character in the next element of the array. If fgets sets the error indicator, the array contents are indeterminate.
Example:
Input:
Contents of afile.txt (used as input): Short Longer string Output:
Short
|Longer string
FILE* fopen | ( | const char * | filename, |
const char * | mode | ||
) |
Description: Opens a file.
Include: <stdio.h>
filename | name of the file |
mode | type of access permitted |
Remarks:
Following are the types of file access:
Example:
Output:
Cannot open afile1
Second try, afile1 was opened
afile1 was closed
afile2 was opened
afile2 was closed
Explanation:
afile1 must exist before it can be opened for reading (r) or the fopen function will fail. If the fopen function opens a file for writing (w+) it does not have to already exist. If it doesn't exist, it will be created and then opened.
int fprintf | ( | FILE * | stream, |
const char * | format, | ||
... | |||
) |
Description: Prints formatted data to a stream.
Include: <stdio.h>
stream | pointer to the stream in which to output data |
format | format control string |
... | optional arguments |
Remarks:
The format argument has the same syntax and use that it has in print.
Example:
Output:
Number of characters printed to file = 25
Contents of afile:
Print this string 1 time
int fputc | ( | int | c, |
FILE * | stream | ||
) |
Description: Puts a character to the stream.
Include: <stdio.h>
c | character to be written |
stream | pointer to the open stream |
Remarks:
The function writes the character to the output stream, advances the file-position indicator and returns the character as an unsigned char converted to an int.
Example:
Output:
T|h|i|s| |i|s| |t|e|x|t|
int fputs | ( | const char * | s, |
FILE * | stream | ||
) |
Description: Puts a string to the stream.
Include: <stdio.h>
s | string to be written |
stream | pointer to the open stream |
Remarks:
The function writes characters to the output stream up to but not including the null character.
Example:
Output:
This is text
Description: Reads data from the stream.
Include: <stdio.h>
ptr | pointer to the storage buffer |
size | size of item |
nelem | maximum number of items to be read |
stream | pointer to the stream |
Remarks:
The function reads characters from a given stream into the buffer pointed to by ptr until the function stores size * nelem characters or sets the end-of-file or error indicator. fread returns n/size where n is the number of characters it read. If n is not a multiple of size, the value of the last element is indeterminate. If the function sets the error indicator, the file-position indicator is indeterminate.
Example:
Output:
10.0/1 = 10.000000
10.0/2 = 5.000000
10.0/3 = 3.333333
10.0/4 = 2.500000
10.0/5 = 2.000000
10.0/6 = 1.666667
10.0/7 = 1.428571
10.0/8 = 1.250000
10.0/9 = 1.111111
10.0/10 = 1.000000
Wrote 10 numbers
Read 10 numbers
1 * 10.000000 = 10.000000
2 * 5.000000 = 10.000000
3 * 3.333333 = 10.000000
4 * 2.500000 = 10.000000
5 * 2.000000 = 10.000000
6 * 1.666667 = 10.000000
7 * 1.428571 = 10.000000
8 * 1.250000 = 10.000000
9 * 1.111111 = 10.000000
10 * 1.000000 = 10.000000
Explanation:
This program uses fwrite to save 10 numbers to a file in binary form. This allows the numbers to be saved in the same pattern of bits as the program is using which provides more accuracy and consistency. Using fprintf would save the numbers as text strings which could cause the numbers to be truncated. Each number is divided into 10 to produce a variety of numbers. Retrieving the numbers with fread to a new array and multiplying them by the original number shows the numbers were not truncated in the save process.
Description: Reassigns an existing stream to a new file.
Include: <stdio.h>
filename | name of the new file |
mode | type of access permitted |
stream | pointer to the currently open stream |
Remarks:
The function closes the file associated with the stream as though fclose was called. Then it opens the new file as though fopen was called. freopen will fail if the specified stream is not open. See fopen for the possible types of file access.
Example:
Output:
afile1 was opened
afile2 was opened
Explanation:
This program uses myfile2 to point to the stream when freopen is called so if an error occurs, myfile1 will still point to the stream and can be closed properly. If the freopen call is successful, myfile2 can be used to close the stream properly.
int fscanf | ( | FILE * | stream, |
const char * | format, | ||
... | |||
) |
Description: Scans formatted text from a stream.
Include: <stdio.h>
stream | pointer to the open stream from which to read data |
format | format control string |
... | optional arguments |
Remarks:
The format argument has the same syntax and use that it has in scanf.
Example:
Input:
Contents of afile: Print this string 100 times Output:
Print
this
string
100
times
int fseek | ( | FILE * | stream, |
long | offset, | ||
int | mode | ||
) |
Description: Moves file pointer to a specific location.
Include: <stdio.h>
stream | stream in which to move the file pointer. |
offset | value to add to the current position |
mode | type of seek to perform |
Remarks:
mode can be one of the following:
SEEK_SET - seeks from the beginning of the file
SEEK_CUR - seeks from the current position of the file pointer
SEEK_END - seeks from the end of the file
Example:
Output:
"This is the beginning"
"this is the middle and this is the end."
"this is the end."
Explanation:
The file, afile.out, is created with the text, "This is the beginning,
this is the middle and this is the end". The function, fseek, uses an offset of zero and SEEK_SET to set the file pointer to the beginning of the file. fgets then reads 22 characters which are "This is the beginning", and adds a null character to the string.
Next, fseek uses an offset of two and SEEK_CURRENT to set the file pointer to the current position plus two (skipping the comma and space). fgets then reads up to the next 70 characters. The first 39 characters are "this is the middle and this is the end". It stops when it reads EOF and adds a null character to the string.
Finally, fseek uses an offset of negative 16 characters and SEEK_END to set the file pointer to 16 characters from the end of the file. fgets then reads up to 70 characters. It stops at the EOF after reading 16 characters "this is the end". and adds a null character to the string.
Description: Sets the stream's file position.
Include: <stdio.h>
stream | target stream |
pos | position-indicator storage as returned by an earlier call to fgetpos |
Remarks:
The function sets the file-position indicator for the given stream in *pos if successful; otherwise, fsetpos sets errno.
Example:
Output:
Bytes read: program opens a file
Bytes read: and reads bytes at
Bytes read: program opens a file
long ftell | ( | FILE * | stream) |
Description: Gets the current position of a file pointer.
Include: <stdio.h>
stream | stream in which to get the current file position |
Example:
Output:
Read some characters:
"This is a very long sentence "
The current position of the file pointer is 29
Description: Writes data to the stream.
Include: <stdio.h>
ptr | pointer to the storage buffer |
size | size of item |
nelem | maximum number of items to be read |
stream | pointer to the open stream |
Remarks:
The function writes characters to a given stream from a buffer pointed to by ptr up to nelem elements whose size is specified by size. The file position indicator is advanced by the number of characters successfully written. If the function sets the error indicator, the file-position indicator is indeterminate.
Example:
Output:
10.0/1 = 10.000000
10.0/2 = 5.000000
10.0/3 = 3.333333
10.0/4 = 2.500000
10.0/5 = 2.000000
10.0/6 = 1.666667
10.0/7 = 1.428571
10.0/8 = 1.250000
10.0/9 = 1.111111
10.0/10 = 1.000000
Wrote 10 numbers
Read 10 numbers
1 * 10.000000 = 10.000000
2 * 5.000000 = 10.000000
3 * 3.333333 = 10.000000
4 * 2.500000 = 10.000000
5 * 2.000000 = 10.000000
6 * 1.666667 = 10.000000
7 * 1.428571 = 10.000000
8 * 1.250000 = 10.000000
9 * 1.111111 = 10.000000
10 * 1.000000 = 10.000000
Explanation:
This program uses fwrite to save 10 numbers to a file in binary form. This allows the numbers to be saved in the same pattern of bits as the program is using which provides more accuracy and consistency. Using fprintf would save the numbers as text strings, which could cause the numbers to be truncated. Each number is divided into 10 to produce a variety of numbers. Retrieving the numbers with fread to a new array and multiplying them by the original number shows the numbers were not truncated in the save process.
int getc | ( | FILE * | stream) |
Description: Get a character from the stream.
Include: <stdio.h>
stream | pointer to the open stream |
Remarks:
getc is the same as the function fgetc.
Example:
Input:
Contents of afile.txt (used as input): Short Longer string Output:
S|h|o|r|t|
|L|o|n|g|e|r| |s|t|r|i|n|g|
int getchar | ( | void | ) |
Description: Get a character from stdin.
Include: <stdio.h>
Remarks:
Same effect as fgetc with the argument stdin.
Example:
Input:
Contents of UartIn.txt (used as stdin input for simulator): Short Longer string Output:
S|h|o|r|t|
char* gets | ( | char * | s) |
Description: Get a string from stdin.
Include: <stdio.h>
s | pointer to the storage string |
Remarks:
The function reads characters from the stream stdin and stores them into the string pointed to by s until it reads a newline character (which is not stored) or sets the end-of-file or error indicators. If any characters were read, a null character is stored immediately after the last read character in the next element of the array. If gets sets the error indicator, the array contents are indeterminate.
Example:
Input:
Contents of UartIn.txt (used as stdin input for simulator): Short Longer string Output:
Text: Short
void perror | ( | const char * | s) |
Description: Prints an error message to stderr.
Include: <stdio.h>
s | string to print |
Remarks:
The string s is printed followed by a colon and a space. Then, an error message based on errno is printed followed by an newline.
Example:
Output:
Cannot open samp.fil: file open error
int printf | ( | const char * | format, |
... | |||
) |
Description: Prints formatted text to stdout.
Include: <stdio.h>
format | format control string |
... | optional arguments |
Remarks:
There must be exactly the same number of arguments as there are format specifiers. If the are less arguments than match the format specifiers, the output is undefined. If there are more arguments than match the format specifiers, the remaining arguments are discarded. Each format specifier begins with a percent sign followed by optional fields and a required type as shown here:
flags
- | left justify the value within a given field width |
0 | Use 0 for the pad character instead of space (which is the default) |
+ | generate a plus sign for positive signed values space generate a space or signed values that have neither a plus nor a minus sign |
# | to prefix 0 on an octal conversion, to prefix 0x or 0X on a hexadecimal conversion, or to generate a decimal point and fraction digits that are otherwise suppressed on a floating-point conversion |
width
Specify the number of characters to generate for the conversion. If the asterisk (*) is used instead of a decimal number, the next argument (which must be of type int) will be used for the field width. If the result is less than the field width, pad characters will be used on the left to fill the field. If the result is greater than the field width, the field is expanded to accommodate the value without padding.
precision
The field width can be followed with dot (.) and a decimal integer representing the precision that specifies one of the following:
If the period appears without the integer the integer is assumed to be zero. If the asterisk (*) is used instead of a decimal number, the next argument (which must be of type int) will be used for the precision.
size
type
Example:
Output:
a
-4 | 4
0012
0x1c
1.100000E+20
-3.35
0.02
int putc | ( | int | c, |
FILE * | stream | ||
) |
Description: Puts a character to the stream.
Include: <stdio.h>
c | character to be written |
stream | pointer to FILE structure |
Remarks:
putc is the same as the function fputc.
Example:
Output:
T|h|i|s| |i|s| |t|e|x|t|
int putchar | ( | int | c) |
Description: Put a character to stdout.
Include: <stdio.h>
c | character to be written |
Remarks:
Same effect as fputc with stdout as an argument.
Example:
Output:
This is text
int puts | ( | const char * | s) |
Description: Put a string to stdout.
Include: <stdio.h>
s | string to be written |
Remarks:
The function writes characters to the stream stdout. A newline character is appended. The terminating null character is not written to the stream.
Example:
Output:
This is text
int remove | ( | const char * | filename) |
Description: Deletes the specified file.
Include: <stdio.h>
filename | name of file to be deleted |
Remarks:
If filename does not exist or is open, remove will fail.
Example:
Output:
File removed
int rename | ( | const char * | old, |
const char * | new | ||
) |
Description: Renames the specified file.
Include: <stdio.h>
old | pointer to the old name |
new | pointer to the new name |
Remarks:
The new name must not already exist in the current working directory, the old name must exist in the current working directory.
Example:
Output:
File renamed
void rewind | ( | FILE * | stream) |
Description: Resets the file pointer to the beginning of the file.
Include: <stdio.h>
stream | stream to reset the file pointer |
Remarks:
The function calls fseek(stream, 0L, SEEK_SET) and then clears the error indicator for the given stream.
Example:
Output:
I have 10 cookies.
I ate 10 cookies.
int scanf | ( | const char * | format, |
... | |||
) |
Description: Scans formatted text from stdin.
Include: <stdio.h>
format | format control string |
... | optional arguments |
Remarks:
Each format specifier begins with a percent sign followed by optional fields and a required type as shown here:
*
indicates assignment suppression. This will cause the input field to be skipped and no assignment made.
width
Specify the maximum number of input characters to match for the conversion not including white space that can be skipped.
modifier
type
Example:
Input:
Contents of UartIn.txt (used as stdin input for simulator): 5 T Green 300000 123-45-6789
Output:
Enter your favorite number, favorite letter,
favorite color, desired salary and SSN:
Number of items scanned = 5
Favorite number = 5, Favorite letter = T
Favorite color = Green, Desired salary = $300000.00
Social Security Number = 123-45-6789
void setbuf | ( | FILE * | stream, |
char * | buf | ||
) |
Description: Defines how a stream is buffered.
Include: <stdio.h>
stream | pointer to the open stream |
buf | user allocated buffer |
Remarks:
setbuf must be called after fopen but before any other function calls that operate on the stream. If buf is a null pointer, setbuf calls the function setvbuf(stream, 0, _IONBF, BUFSIZ) for no buffering; otherwise setbuf calls setvbuf(stream, buf, _IOFBF, BUFSIZ) for full buffering with a buffer of size BUFSIZ. See setvbuf.
Example:
Output:
myfile1 has no buffering
myfile2 has full buffering
Description: Defines the stream to be buffered and the buffer size.
Include: <stdio.h>
stream | pointer to the open stream |
buf | user allocated buffer |
mode | type of buffering |
size | size of buffer |
Remarks:
setvbuf must be called after fopen but before any other function calls that operate on the stream. For mode use one of the following:
_IOFBF - for full buffering
_IOLBF - for line buffering
_IONBF - for no buffering
Example:
Output:
myfile1 has no buffering
myfile2 has a buffer of 256 characters
int sprintf | ( | char * | s, |
const char * | format, | ||
... | |||
) |
Description: Prints formatted text to a string.
Include: <stdio.h>
s | storage string for output |
format | format control string |
... | optional arguments |
Remarks:
The format argument has the same syntax and use that it has in printf.
Example:
Output:
Number of characters printed to string buffer = 25
String = Print this string 1 time
int sscanf | ( | const char * | s, |
const char * | format, | ||
... | |||
) |
Description: Scans formatted text from a string.
Include: <stdio.h>
s | storage string for input |
format | format control string |
... | optional arguments |
Remarks:
The format argument has the same syntax and use that it has in scanf.
Example:
Output:
Number of items scanned = 4
Favorite number = 5
Favorite letter = T
Favorite color = green
Desired salary = $3000000.00
FILE* tmpfile | ( | void | ) |
Description: Creates a temporary file.
Include: <stdio.h>
Remarks:
tmpfile creates a file with a unique filename. The temporary file is opened in w+b (binary read/write) mode. It will automatically be removed when exit is called; otherwise the file will remain in the directory.
Example:
Output:
Temporary file was created
char* tmpnam | ( | char * | s) |
Description: Creates a unique temporary filename.
Include: <stdio.h>
s | pointer to the temporary name |
Remarks:
The created filename will not conflict with an existing file name. Use L_tmpnam to define the size of array the argument of tmpnam points to.
Example:
Output:
Temporary file ctm00001.tmp was created
int ungetc | ( | int | c, |
FILE * | stream | ||
) |
Description: Pushes character back onto stream.
Include: <stdio.h>
c | character to be pushed back |
stream | pointer to the open stream |
Remarks:
The pushed back character will be returned by a subsequent read on the stream. If more than one character is pushed back, they will be returned in the reverse order of their pushing. A successful call to a file positioning function (fseek, fsetpos or rewind) cancels any pushed back characters. Only one character of push back is guaranteed. Multiple calls to ungetc without an intervening read or file positioning operation may cause a failure.
Example:
Input:
Contents of afile.txt (used as input): Short Longer string Output:
Sho2rt
Longe2r st2ring
int vfprintf | ( | FILE * | stream, |
const char * | format, | ||
va_list | ap | ||
) |
Description: Prints formatted data to a stream using a variable length argument list.
stream | pointer to the open stream |
format | format control string |
ap | pointer to a list of arguments |
Remarks:
The format argument has the same syntax and use that it has in printf. To access the variable length argument list, the ap variable must be initialized by the macro va_start and may be reinitialized by additional calls to va_arg. This must be done before the vfprintf function is called. Invoke va_end after the function returns. For more details, see stdarg.h.
Example:
Output:
Contents of afile.txt
Error: The letter 'a' is not an integer value.
Error: Requires 3 or more characters.
int vprintf | ( | const char * | format, |
va_list | ap | ||
) |
Description: Prints formatted text to stdout using a variable length argument list.
format | format control string |
ap | pointer to a list of arguments |
Remarks:
The format argument has the same syntax and use that it has in printf. To access the variable length argument list, the ap variable must be initialized by the macro va_start and may be reinitialized by additional calls to va_arg. This must be done before the vprintf function is called. Invoke va_end after the function returns. For more details, see stdarg.h
Example:
Output:
Error: The letter 'a' is not an integer value.
Error: Requires 3 or more characters.
int vsprintf | ( | char * | s, |
const char * | format, | ||
va_list | ap | ||
) |
Description: Prints formatted text to a string using a variable length argument list.
s | storage string for output |
format | format control string |
ap | pointer to a list of arguments |
Remarks:
The format argument has the same syntax and use that it has in printf. To access the variable length argument list, the ap variable must be initialized by the macro va_start and may be reinitialized by additional calls to va_arg. This must be done before the vsprintf function is called. Invoke va_end after the function returns. For more details, see stdarg.h
Example:
Output:
Error: The letter 'a' is not an integer value.
Error: Requires 3 or more characters.