Share this page

# strncpy

String Manipulation Functions

Copies the first n characters of one string to another.

    char *strncpy( char *dest, const char *source, size_t n );

dest | The destination string to which n characters are copied.
source | The source string from which n characters are copied.
n | The number of characters copied.

## Return Values

Returns the destination string.

## Example

The following example of strncpy creates a filename that conforms to the 8.3 DOS file name format i.e., the name must be no more than 8 characters and the file suffix must be 3 characters long.

The file name created is in the form [Vuser_id]_apptest.log. When running the example in VuGen, the Vuser ID returned by lr_whoami is -1, two characters in length. The remaining characters, "_apptest" make up more than the required 8 characters. strncpy ensures that only 8 characters are copied to filename. The final name is "-1_appte.log".

        #include <string.h>

        int id, length;
        char tmp[1024];

    // 8 characters + period + 3 characters + null

        char filename[13];
        lr_whoami (&id, NULL, NULL);
        sprintf(tmp, "%d_apptest", id);
        length = strlen(tmp);
        lr_output_message ("name=%s length=%d", tmp, length);

    // Copy 8 characters only

        strncpy(filename, tmp, 8);
        lr_output_message ("filename without suffix=%s", filename);
        strcat(filename, ".log");
        lr_output_message ("final filename=%s", filename);

    Example: Output:
    Action.c(10): name=-1_apptest length=10
    Action.c(13): filename without suffix=-1_appte
    Action.c(15): final filename=-1_appte.log

Tips to improve your search results... VuGen Function Reference Version 26.1 Function Reference 26.1 All View Local Help Center View Online Help Center Version: