Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Advanced C 1992

.pdf
Скачиваний:
93
Добавлен:
17.08.2013
Размер:
4.28 Mб
Скачать

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.

596

 

ANSI C’s Library Function

C C C

 

 

14C

 

 

C C C

tolower()

 

C C

 

 

Header:

ctype.h & stdlib.h

 

Syntax:

int tolower(int chChar);

 

Description:

Converts chChar to lowercase if it was originally uppercase. If

 

chChar was not uppercase, then it is returned unchanged.

 

Parameters:

chChar—Uppercase letter to be converted to lowercase.

 

Returns:

The character converted to lowercase.

 

Example:

printf(“‘A’ in lowercase is ‘%c’\n”,

 

 

tolower(‘A’);

 

 

/* Will print ‘A’ in lowercase is ‘a’ */

 

Note:

See toupper().

 

toupper()

 

 

Header:

ctype.h & stdlib.h

 

Syntax:

int toupper(int chChar);

 

Description:

Converts chChar to uppercase if it was originally lowercase. If

 

chChar was not lowercase, then it is returned unchanged.

 

Parameters:

chChar—Lowercase letter to be converted to uppercase.

 

Returns:

The character converted to uppercase.

 

Example:

printf(“‘a’ in uppercase is ‘%c’\n”,

 

 

toupper(‘a’);

 

 

/* Will print ‘a’ in uppercase is ‘A’ */

 

Note:

See tolower().

 

ungetc()

 

 

Header:

stdio.h

 

Syntax:

int ungetc(int chChar, FILE * OpenFile);

 

597

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.

598

ANSI C’s Library Function

Hipson */

#include <limits.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h>

#define

TRUE

1

#define

FALSE

(!TRUE)

int

AddList(int nFirst, ...);

int

OurErrors(char * OutputFormat, ...);

void

main()

 

{

 

 

int

nSum;

 

nSum = AddList(10, 20, 30, 40, 50, 60, 70, 80, 90, INT_MIN);

(void)OurErrors(“%s - %d, %s\n”, “First”, nSum, “Second”);

}

int AddList(

int nFirst,

...)

{

int

nReturnValue = nFirst;

int

nThisValue;

va_list Arguments;

va_start(Arguments, nFirst);

C C C

C14C C

C C C

599

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.

600

 

ANSI C’s Library Function

C C C

 

 

14C

 

 

C C C

Parameters:

param—Variable argument list.

C C

 

Returns:

No return value.

 

Example:

(See va_arg(), preceding function described.)

 

Note:

See Chapter 13, “All About Header Files,” for more information.

va_start()

 

 

Header:

stdarg.h

 

Syntax:

void va_start(va_list param, previous);

 

Description:

Starts processing of a variable number of arguments.

 

Parameters:

paramva_list variable, used by the va_ functions.

 

 

previous—Name of the last fixed parameter being passed to the

 

called function.

 

Returns:

No return value.

 

Example:

(See va_arg(), previously described.)

 

Note:

See Chapter 13, “All About Header Files,” for more information.

vfprintf()

 

 

Header:

stdio.h

 

Syntax:

int vfprintf(FILE * OpenFile, const char *

 

 

szFormat, va_list VarArgs);

 

Description:

Prints to the specified file by using arguments passed by another

 

function.

 

Parameters:

OpenFile—Pointer to an opened file.

 

 

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.

 

601

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);

602

 

 

ANSI C’s Library Function

C C C

 

 

 

14C

 

 

 

C C C

Description:

 

 

C C

Prints to stdout using arguments passed by another function.

Parameters:

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 * OutputFormat, ...);

 

 

void

main()

 

 

{

 

 

 

int

nSum = 100;

 

 

 

(void)OurOutput(“%s - %d, %s\n”,

 

 

 

“First”, nSum, “Second”);

 

 

}

 

 

 

int OurOutput(

 

 

 

char * OutputFormat,

 

 

 

...)

 

 

{

 

 

 

va_list Arguments;

 

 

 

va_start(Arguments, OutputFormat);

 

 

 

vprintf(OutputFormat, Arguments);

 

 

 

va_end(Arguments);

 

 

 

return(0);

 

 

}

 

 

Note:

See vfprintf().

 

603

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,

604

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()

C C C

C14C C

C C C

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));

Note:

See wctomb().

605