Part IV • Documenting the Differences
tmpfile()
Header: |
stdio.h |
Syntax: |
FILE * tmpfile(void); |
Description: |
Creates a temporary work file and opens it for update. This file is |
|
removed by the system either when it is closed or the program |
|
ends. |
Parameters: |
None. |
Returns: |
Handle to a file or NULL if the function fails. |
Example: |
FILE * TempWork = tmpfile(); |
Note: |
Be careful not to close the file in error because this removes the |
|
file. |
tmpnam()
Header: |
stdio.h |
|
Syntax: |
char * tmpnam(char * szFileName); |
Description: |
Creates a save name for a temporary work file. This function is |
|
used when tmpfile() cannot be used, such as in situations where |
|
the file must be closed for some reason. |
Parameters: |
szFileName—Pointer to a buffer to hold the filename. This buffer |
|
must be at least L_tmpnam characters long. |
Returns: |
Pointer to the filename buffer. If szFileName is NULL, then the |
|
buffer is a static internal buffer. |
Example: |
char |
szBuffer[L_tmpnam]; |
tmpnam(szBuffer);
printf(“The temporary work file is ‘%s’\n”, szBuffer);
Note: Don’t forget to remove the file when the program ends.
Part IV • Documenting the Differences
Description: |
This function pushes back a character to the file OpenFile (which |
|
was opened for input). |
Parameters: |
chChar—Character to be pushed back to the file. |
|
OpenFile—Pointer to an opened file. |
Returns: |
The character that was pushed back. |
Example: |
char |
szBuffer[129]; |
|
printf(“Please press the ‘b’ key: “); |
|
szBuffer[0] = (char)getc(stdin); |
|
ungetc(‘A’, stdin); |
|
szBuffer[1] = (char)getc(stdin); |
|
printf(“szBuffer has %2.2s\n”, szBuffer); |
|
/* Will print szBuffer has bA (if you type a ‘b’ |
|
at the prompt) */ |
Note: |
The character need not be the same one as was last read. You may |
|
ungetc() only one character before the character is read or |
|
discarded (by a call to fseek(), fsetpos(), or rewind()). |
va_arg()
Header: |
stdarg.h |
Syntax: |
type va_arg(va_list param, type); |
Description: |
Obtains the next argument from a list of variable arguments to a |
|
function. |
Parameters: |
param—Parameter list pointer. |
|
type—Type of the next (to be fetched) parameter. |
Returns: |
Value of the parameter being fetched. |
Example: |
/* Program VARGS, written 17 June 1999 by Peter D. |
Part IV • Documenting the Differences
while((nThisValue = va_arg(Arguments, int)) !=
INT_MIN)
{
|
|
nReturnValue += nThisValue; |
|
|
} |
|
|
va_end(Arguments); |
|
|
return(nReturnValue); |
|
} |
|
|
int |
OurErrors( |
|
|
char * OutputFormat, |
|
|
...) |
|
{ |
|
|
va_list Arguments; |
|
|
va_start(Arguments, OutputFormat); |
|
|
vfprintf(stderr, OutputFormat, Arguments); |
|
|
va_end(Arguments); |
|
|
return(0); |
|
} |
|
Note: |
See Chapter 13, “All About Header Files,” for more information. |
va_end() |
|
|
Header: |
stdarg.h |
Syntax: |
void va_end(va_list param); |
Description: |
Ends the processing of the variable number of arguments. |
Part IV • Documenting the Differences
Example: |
/* Program VARGS, written 17 June 1999 by |
|
Peter D. Hipson */ |
|
#include <limits.h> |
|
#include <stdarg.h> |
|
#include <stdio.h> |
|
#include <stdlib.h> |
|
int |
OurErrors(char * OutputFormat, ...); |
|
void |
main() |
|
{ |
|
|
int |
nSum = 100; |
|
|
(void)OurErrors(“%s - %d, %s\n”, |
|
|
“First”, nSum, “Second”); |
|
} |
|
|
int |
OurErrors( |
|
|
char * OutputFormat, |
|
|
...) |
|
{ |
|
|
va_list Arguments; |
|
|
va_start(Arguments, OutputFormat); |
|
|
vfprintf(stderr, OutputFormat, |
|
|
Arguments); |
|
|
va_end(Arguments); |
|
|
return(0); |
|
} |
|
Note: |
See vprintf(). |
vprintf()
Header: stdio.h
Syntax: int vprintf(const char * szFormat, va_list
VarArgs);
Part IV • Documenting the Differences
vsprintf()
Header: |
stdio.h |
Syntax: |
int vsprintf(char * szBuffer, const char * |
|
|
szFormat, va_list VarArgs); |
Description: |
Prints, using arguments passed by another function, to the buffer |
|
pointed to by szBuffer. |
Parameters: |
szBuffer—Pointer to a buffer to write to. |
|
szFormat—Pointer to a string containing format information. |
|
VarArgs—Variable argument list. |
Returns: |
Number of characters written or a negative value if there was an |
|
error. |
Example: |
/* Program VARGS, written 17 June 1992 by |
|
Peter D. Hipson */ |
|
#include <limits.h> |
|
#include <stdarg.h> |
|
#include <stdio.h> |
|
#include <stdlib.h> |
|
int |
OurOutput(char * OutputBuffer, |
|
|
char * OutputFormat, ...); |
|
void |
main() |
|
{ |
|
|
char |
szBuffer[100]; |
|
int |
nSum = 100; |
|
|
(void)OurOutput(szBuffer, |
|
|
“%s - %d, %s\n”, |
|
|
“First”, nSum, “Second”); |
|
|
printf(“%s”, szBuffer); |
|
} |
|
|
int |
OurOutput( |
|
|
char * OutputBuffer, |
ANSI C’s Library Function
char * OutputFormat,
...)
{
va_list Arguments;
va_start(Arguments, OutputFormat);
vsprintf(OutputBuffer, OutputFormat, Arguments);
va_end(Arguments);
return(0);
}
Note: See vfprintf() and vprintf().
wcstombs()
Header: stdlib.h
Syntax: size_t wcstombs(char * szDestination,
const wchar_t * pWideChars, size_t nSize);
Description: Converts the wide characters in the buffer pointed to by pWideChars to multibyte characters. Stores up to nSize characters
in szDestination.
Parameters: szDestination—Pointer to a buffer to receive the multibyte characters.
pWideCharacters—Pointer to a buffer containing the wide characters to be converted.
nCount—Size of szDestination.
Returns: Number of characters converted.
Example: wcstombs(szBuffer, szWideChars,
sizeof(szBuffer));