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

Advanced C 1992

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

Part IV • Documenting the Differences

strpbrk()

Header: string.h

Syntax: char * strpbrk(const char * szString, const char * szCharacters);

Description: Finds the first occurrence of any character from szCharacters found in szString.

Parameters: szString—Pointer to a string to search.

szCharacters—Pointer to a string containing characters to search for.

Returns:

Pointer to the first character found in szString that is in

 

szCharacters.

Example:

char

szString1[] =

 

{“Now is

all the time for all good men”};

 

char

*

pChars;

pChars = strpbrk(szString1, “fzxy”);

if (pChars)

{

printf(“found at ‘%s’\n”, pChars);

}

/* Will print found at ‘for all good men’ */

strrchr()

Header: string.h

Syntax: char * strrchr(const char * szString, int chChar);

Description: Finds the last occurrence of chChar found in szString.

Parameters: szString—Pointer to a string to search.

chChar—Character to search for.

586

 

 

ANSI C’s Library Function

C C C

 

 

 

14C

 

 

 

C C C

Returns:

 

 

C C

Pointer to the last occurrence of chChar found in szString.

 

Example:

char

szString1[] =

 

 

{“Now is all the time for all good men”};

 

 

char

* pChars;

 

 

 

pChars = strrchr(szString1, ‘a’);

 

 

 

if (pChars)

 

 

 

{

 

 

 

printf(“found at ‘%s’\n”,

 

 

 

pChars);

 

 

 

}

 

 

/* Will print found at ‘all good men’ */

 

strspn()

 

 

 

Header:

string.h

 

Syntax:

size_t strspn(const char * szString, const char *

 

 

 

szChars);

 

Description:

Returns the length of the initial string that contains characters

 

found in szChars.

 

Parameters:

szString—Pointer to a string to be searched.

 

 

szChars—String containing characters that must be contained.

Returns:

Length of the initial string that contains only characters from

 

szChars, up to the length of szString.

 

Example:

char

szString[100] =

 

 

{“Now is the time for all good men.”};

 

 

char

szOutput[100];

 

memset(szOutput, 0, sizeof(szOutput));

strncpy(szOutput, szString, strspn(szString, “woN si teh”));

587

Part IV • Documenting the Differences

 

printf(“%s”,

 

szOutput);

 

/* will print Now is the ti */

Note:

Also see strcspn().

strstr()

Header:

string.h

Syntax:

char * strstr(const char * szString, const char *

 

szCharacters);

Description:

Finds the first occurrence of szCharacters in szSting.

Parameters:

szString—Pointer to the string to search.

 

szCharacters—Pointer to the characters to search for.

Returns:

Pointer to the point where the characters were found.

Example:

char szString[100] =

 

{“Now is the time for all good men.”};

 

printf(“‘%s’”, strstr(szString, “me”));

 

/* will print ‘me for all good men.’ */

Note:

Basically a search for a substring in a string function.

strtod()

Header:

stdlib.h

Syntax:

double strtod(const char * szString,

 

char ** pEnd);

Description:

Converts the string to a floating point number, stopping when an

 

invalid character has been reached. The stopping point is stored

 

in the variable pointed to by pEnd if pEnd is not NULL.

Parameters:

szString—Pointer to the string to convert.

 

pEnd—Pointer to a string pointer.

588

 

 

ANSI C’s Library Function

 

 

Returns:

The floating point number converted.

Example:

double

dValue;

 

char

szString[100] =

 

{“123.34 Now is the time for all good men.”};

 

char

* pEnd;

 

dValue = strtod(szString, &pEnd);

 

printf(“Converted %f stopped at ‘%s’\n”,

 

dValue,

 

pEnd);

 

/*

 

* Prints: Converted 123.340000 stopped at

 

‘ Now is the time for all good men.’

 

*/

Note:

See strtol().

strtok()

Header: string.h

Syntax: char * strtok(char * szString, const char * szTokenSep);

C C C

C14C C

C C C

Description: Breaks the string pointed to by szString into tokens, when each token is separated by one (or more) of the characters found in

szTokenSep.

Parameters: szString—Pointer to a string to break into tokens. This string will be modified, so use a copy if necessary.

szTokenSep—Pointer to a string of token separators.

Returns:

Pointer to a token from szString.

Example:

char

szString[100] =

 

{“Now is

the time for all good men.”};

 

char

szTokens[] = {“ .”};

 

char

*

pToken;

589

Part IV • Documenting the Differences

printf(“‘%s’\n”, szString);

pToken = strtok(szString, szTokens);

do

{

printf(“Token ‘%s’\n”, pToken);

} while (pToken = strtok(NULL, szTokens));

printf(“‘%s’\n”, szString);

/*

*Prints:

*‘Now is the time for all good men.’

*Token ‘Now’

*Token ‘is’

*Token ‘the’

*Token ‘time’

*Token ‘for’

*Token ‘all’

*Token ‘good’

*Token ‘men’

*‘Now’

*/

Note: Don’t forget that this function modifies the string passed.

strtol()

Header: stdlib.h

Syntax: long strtol(const char * szString, char ** pEnd,

int nBase);

Description: Converts the string to a long integer number, stopping when an invalid character has been reached. The stopping point is stored in the variable pointed to by pEnd if pEnd is not NULL. The parameter nBase determines what base is used and must be either 0 or 2 through 36. If nBase is zero, then the base of the

590

 

 

ANSI C’s Library Function

C C C

 

 

 

14C

 

 

 

C C C

 

 

 

C C

 

number is determined from the number’s format—if the number

 

starts with 0x or 0X, then it is base 16; if it starts with a zero, then

 

base 8 is assumed; otherwise it is decimal based.

 

Parameters:

szString—Pointer to the string to convert.

 

 

pEnd—Pointer to a string pointer.

 

 

nBase—Base of the number to be converted.

 

Returns:

The long integer converted.

 

Example:

long

lValue;

 

 

char

szString[100] =

 

 

{“123.34 Now is the time for all good men.”};

 

 

char

* pEnd;

 

 

 

lValue = strtol(szString, &pEnd, 0);

 

 

 

printf(“Converted %ld stopped at ‘%s’\n”,

 

 

 

lValue,

 

 

 

pEnd);

 

 

/*

 

 

 

* Prints: Converted 123 stopped at

 

 

‘.34 Now is the time for all good men.’

 

 

*/

 

 

Note:

See strtoul().

 

strtoul()

 

 

 

Header:

stdlib.h

 

Syntax:

unsigned long int strtoul(const char * szString,

 

 

 

char ** pEnd,

 

 

 

int nBase);

 

Description:

Converts the string to an unsigned long integer number,

 

stopping when an invalid character is reached. The stopping point

 

is stored in the variable pointed to by pEnd if pEnd is not NULL. The

 

parameter nBase determines what base is used and must be

591

Part IV • Documenting the Differences

 

either 0 or 2 through 36. If nBase is zero, then the base of the

 

number is determined from the number’s format; if the number

 

starts with 0x or 0X, then it is base 16; if it starts with a zero,

 

then base 8 is assumed; otherwise it is decimal based.

Parameters:

szString—Pointer to the string to convert.

 

pEnd—Pointer to a string pointer.

 

nBase—Base of the number to be converted.

Returns:

The long integer converted.

Example:

unsigned long lValue;

 

char

szString[100] =

 

{“123.34 Now is the time for all good men.”};

 

char

* pEnd;

 

 

lValue = strtoul(szString, &pEnd, 0);

 

 

printf(“Converted %ld stopped at ‘%s’\n”,

 

 

lValue,

 

 

pEnd);

 

/*

 

 

* Prints: Converted 123 stopped at ‘.34 Now is the time

 

for all good men.’

 

*/

 

Note:

See strtol().

strxfrm()

Header:

string.h

Syntax:

size_t strxfrm (char * szDestination, const char *

 

szSource, size_t nLength);

Description:

Copies the string pointed to by szSource to the buffer pointed to

 

by szDestination, using the collating sequence set by setlocale

 

(). The function is identical to strncpy() when the locale is “C”,

592

 

 

ANSI C’s Library Function

C C C

 

 

 

14C

 

 

 

C C C

 

 

 

C C

 

except thestring is not paddedwith NULL characters when szSource

 

is shorter than nLength.

 

Parameters:

szDestination—Pointer to a buffer where szSource will be

 

copied to.

 

 

szSource—Pointer to a string to copy and convert.

 

 

nLength—Maximum number of characters to copy and convert.

Returns:

Length of the converted string.

 

Example:

char

szSource[100] =

 

 

{“Now is the time for all good men.”};

 

 

char

szDestination[100];

 

strxfrm(szDestination, szSource, strlen(szSource));

printf(“Converted \n’%s’ \nto \n’%s’\n”, szSource,

szDestination);

/*

*Prints:

*Converted

*‘Now is the time for all good men.’

*to

*‘Now is the time for all good men.’

*/

Note:

See strncpy()

system()

Header: process.h & stdlib.h

Syntax: int system(const char * szCommand);

Description: Passes the string pointed to by szCommand to the operating system’s command processor.

593

Part IV • Documenting the Differences

Parameters:

szCommand—Pointer to a string containing an operating system

 

command, or NULL to determine if there is a command processor.

Returns:

If szCommand is NULL, non-zero if there is a command processor,

 

otherwise a zero value. If szCommand is not NULL, then zero if there

 

was no error, or a non-zero value if the command processor

 

couldn’t be loaded.

Example:

/* Check for command processor, and do a dir command if

 

present */

 

if (system(NULL))

 

{

 

system(“dir *.*”);

 

}

Note:

Most operating systems have a loadable command processor.

tan()

Header:

math.h

Syntax:

double tan(double dValue);

Description:

Returns the tangent of dValue, measured in radians.

Parameters:

dValue—Value for which tangent is desired.

Returns:

The tangent of dValue.

Example:

double dResult;

 

dResult = tan(1.5);

 

/* dResult will be 14.10142 */

Note:

Also see tanh().

tanh()

Header: math.h

Syntax: double tanh(double dValue);

594

 

 

ANSI C’s Library Function

C C C

 

 

 

14C

 

 

 

C C C

Description:

 

 

C C

Returns the hyperbolic tangent of dValue, measured in radians.

Parameters:

dValue—Value for which hyperbolic tangent is desired.

 

Returns:

The hyperbolic tangent of dValue.

 

Example:

double

dResult;

 

 

dResult = tanh(1.5);

 

 

/* dResult will be 0.905148 */

 

Note:

Also see tan().

 

time()

 

 

 

Header:

time.h

 

 

Syntax:

time_t time(time_t * TimeValue);

 

Description:

Returns the current calendar time encoded into a time_t type.

Parameters:

TimeValue—Pointer to a type time_t, which if not NULL, will also

 

receive the time.

 

Returns:

The current time.

 

Example:

char

szBuffer[100];

 

 

time_t

OurTime;

 

OurTime = time(NULL);

strftime(szBuffer, sizeof(szBuffer), “Today is %A %B %d, %Y”,

localtime(&OurTime));

printf(“%s\n”, szBuffer);

/* Will print Today is Saturday June 27, 1992 */

Note: Also see strftime() and ctime(). The function time()’s parameter is often NULL as shown in the preceding example.

595