Previous Table of Contents Next


3.10.1.8. Operations on Files

rename

int rename(const char *fromname, const char *toname)

rename attempts to rename the file named fromname, giving it the new name toname. The return value is zero if the function succeeds; nonzero if it fails.

remove

int remove(const char *name)

remove attempts to delete the file named name. The return value is 0 if the function succeeds; nonzero if it fails.

tmpfile, tmpnam

FILE *tmpfile(void)  char *tmpnam(char *buf)

tmpfile creates a temporary file and opens it as if by calling fopen with the mode string “wb+” (see section 3.10.1.1). tmpfile arranges that this file will be automatically removed when it is closed, or upon program termination.

tmpnam generates a temporary filename, which the program can open or otherwise use as it wishes. The filename generated by tmpnam is different each time it is called. If buf is non-null, it is assumed to point to a character array of size at least L_tmpnam (a constant defined in <stdio.h>) into which the filename is written. Otherwise, the filename is written to an internal static buffer. In either case, a pointer to the generated name is returned.

3.10.2. <string.h>

The functions declared by <string.h> mostly provide support for C’s null-terminated strings, although there are also a few functions (having names beginning with mem) that operate on arbitrary blocks of characters. The functions provided include those for copying, comparing, and performing simple searches in strings, as well as several other miscellaneous operations.

Few of these functions are at all sophisticated, and in fact many of them provide useful illustrations of the string and pointer manipulations discussed in section 3.6.5. Accordingly, several of the function descriptions here are accompanied by sample implementations.

3.10.2.1. String Length

strlen

size_t strlen(const char *s)

strlen returns the length of the string s (the number of characters up to but not including the terminating \0).

strlen is simple to implement; one possibility is

size_t strlen(const char *s)
{
       size_t len = 0;
       while(*s++ != ‘\0’)
                len++;
       return len;
}

3.10.2.2. Copying and Concatenating Strings

strcpy

char *strcpy(char *dest, const char *src)

strcpy copies the string pointed to by src to the destination dest, and returns dest.

strcpy is also simple to implement:

char *strcpy(char *dest, const char *src)
{
       char *dp = dest;
       while(*src != ‘\0’)
               *dp++ = *src++;
       *dp = ‘\0’;
       return dest;
}

It is traditional to collapse the character assignment and the test for ‘\0’; this alleviates the necessity to append the final ‘\0’ outside the loop:

while((*dp++ = *src++) != ‘\0’)
        ;
return dest;

strcat

char *strcat(char *dest, const char *src)

strcat appends the string pointed to by src to the destination dest, in place, and returns dest. (Its name is somewhat of a misnomer, as it does not concatenate two strings to form a third string.)

Here is a sample implementation of strcat:

char *strcat(char *dest, const char *src)
{
       char *dp = dest;
       while(*dp != ‘\0’)
               dp++;
       while(*src != ‘\0’)
               *dp++ = *src++;
       *dp = ‘\0’; return dest;
}

strncpy

char *strncpy(char *dest, const char *src, size_t n)

strncpy copies at most n characters from the string pointed to by src to the destination dest, stopping early if there are more than n characters before the terminating \0 in src. The copied string, dest, is returned.

For obscure historical reasons, strncpy has two unusual traits. If there are fewer than n characters in the source string, the destination is padded with as many \0 characters as are required so that exactly n characters total are always written. But if there are n or more characters in the source string, the destination receives no terminating \0 at all, and is not therefore a well-formed string. A workaround is to use strncat instead of strncpy: when a call to strncpy(p1, p2, n) should create a null-terminated string, it can be replaced with the sequence *a = ‘\0’, strncat(p1, p2, n) (as long as the array a contains space for at least n+1 characters).

strncat

char *strncat(char *dest, const char *src, size_t n)

strncat appends at most n characters from the string pointed to by src to the destination dest, in place. It appends a terminating \0 (which is not counted against the maximum n). The modified string, dest, is returned.


Previous Table of Contents Next