Example: strncat

The following example of strncat 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 as returned by lr_whoami is -1, two characters in length. strncat appends the string "_apptest" to this, but makes sure that it appends no more than the amount of characters needed to make up 8 characters only. The final name is "-1_appte.log".

    #include <string.h>
    int id, length, chars_remaining;
// 8 characters + full stop + 3 characters + null 
    char filename[13]; 
    lr_whoami (&id, NULL, NULL);
    sprintf (filename, "%d_", id);
    lr_output_message ("filename so far=%s", filename);
    length = strlen(filename);
    lr_output_message ("length so far=%d", length);
    chars_remaining = 8 - length;
    lr_output_message ("chars remaining=%d", chars_remaining);
    strncat(filename, "apptest", chars_remaining);
    lr_output_message ("filename without suffix=%s", filename);
    strcat(filename, ".log");
    lr_output_message ("final filename=%s", filename);
Output:
Action.c(8): filename so far=-1_
Action.c(10): length so far=3
Action.c(12): chars remaining=5
Action.c(14): filename without suffix=-1_appte
Action.c(16): final filename=-1_appte.log