Example: putenv

The following example uses putenv to create a new environment variable LOGFILE_NAME. It is retrieved later on with the getenv function and used to write to the log file in the system's temporary directory.

    #include <stdio.h>
    char * tmp, logfile[512], dir_seperator;
    FILE* file;
    // Create an environment variable 
    putenv("LOGFILE_NAME=lr_test.txt");
    // Retrieve the temporary directory environment directory
    if(tmp = (char *)getenv("TEMP"))
        lr_output_message ("Temp Dir = %s", tmp);
    else {
        lr_output_message ("TEMP environment variable undefined");
        return -1;
    }
    sprintf(logfile, "%s\\%s", tmp, (char *)getenv("LOGFILE_NAME"));
    // Open and write to the log file
    if ((file = fopen(logfile, "w")) == NULL ) {
        lr_error_message ("Cannot open %s", logfile);
        return -1;
    }
    else
        lr_output_message ("Opened %s", logfile);
    fprintf(file, "this is a log file\n");
    fclose(file);
Output:
Action.c(11): Temp Dir = D:\TEMP
Action.c(24): Opened D:\TEMP\lr_test.txt