Example: fopen

Example #1

The following example, for Windows platforms, opens a file using fopen, reads it into a buffer, and then closes it.

To run the example, copy the file readme.txt from the installation's dat directory to the c drive, or copy another file and change the value of filename.

    
#include <stdio.h>
    int count, total = 0;
    char buffer[1000];
    FILE* file_stream;
    char * filename = "c:\\readme.txt";
    // Open the file with read access 
    if ((file_stream = fopen(filename, "r")) == NULL) {
        lr_error_message ("Cannot open %s", filename);
        return -1;
    }
    // Read until end of file
    while (!feof(file_stream)) {
    // Read 1000 bytes while maintaining a running count 
        count = fread(buffer, sizeof(char), 1000, file_stream);
        lr_output_message ("%3d read", count);
    // Check for file I/O errors 
        if (ferror(file_stream)) {
            lr_output_message ("Error reading file %s", filename);
            break;
        }
        total += count; // Add up actual bytes read 
    }
    // Display final total
    lr_output_message ("Total number of bytes read = %d", total );
    // Close the file stream 
    if (fclose(file_stream))
        lr_error_message ("Error closing file %s", filename);
        Output:
        Action.c(19): 1000 bytes read
        Action.c(19): 1000 bytes read
        ...
        Action.c(19): 1000 bytes read 
        Action.c(20): 977 read
        Action.c(34): Total number of bytes read = 69977

Example #2

The following example, for Windows platforms, reads an entire file into memory. You can use this code to load a file into memory at VUser initialization.

#include <stdio.h>
vuser_init()
{
    lr_save_string ("\\\\Myfileserver\\MyShare\\template.tpl", "FilePath");  // read from a UNC file
    templateFile = readFile();
    lr_output_message("File contents: %s", templateFile);
    free(templateFile);
}

// fileReader.h

#ifndef _FILEREADER
#define _FILEREADER

#define SEEK_SET 0 /* beginning of file. */
#define SEEK_CUR 1 /* current position. */
#define SEEK_END 2 /* end of file */

// read the file specified by {FilePath} and returns a null terminated buffer 

char* readFile()
{
    FILE* fileHandle;
    char* buffer;
    int fileLen, bytesRead;
          
     if ((fileHandle = fopen(lr_eval_string("{FilePath}"),  "rb+")) == NULL)  {
        lr_error_message ("Cannot open file %s", lr_eval_string("{FilePath}"));   
        lr_abort();
    }
     
    fseek(fileHandle, 0, SEEK_END);
    fileLen=ftell(fileHandle);
    fseek(fileHandle, 0, SEEK_SET);
    lr_output_message("File name: %s", lr_eval_string("{FilePath}"));
    lr_output_message("File length is: %9d bytes.", fileLen);
     
    buffer=(char *)malloc(fileLen+1);
     
    if (!buffer) {
         lr_error_message("Could not malloc %10d bytes", fileLen+1);
         fclose(fileHandle);
         lr_abort();
    }
    
    bytesRead = fread(buffer, 1, fileLen, fileHandle);
    if (bytesRead != fileLen)
    {
         lr_error_message("File length is %10d bytes but only read %10d bytes", fileLen, bytesRead);
    }
    else
    {
         lr_log_message("Successfully read %9d bytes from file: ", bytesRead);
    }
    fclose(fileHandle);
    
    buffer[bytesRead] = NULL;  // null terminate
     
    return buffer;
}
#endif // _FILEREADER