Share this page

# fopen

Input-Output Functions

Opens a file for buffered I/0.

    long fopen( const char *filename, const char *access_mode);

filename | The name of the file to open.
access_mode | The type of access mode: r, w, a or r+, w+, a+, where the "+" sign indicates that the file must already exist.

The fopen access_mode parameter serves also to specify whether to open the file as text or binary, adding t or b characters to this access mode string.

t | Text mode. In text mode the end of file is assumed to be at first Ctrl+Z character. Some conversions can occur reading and writing with End Of Line / Feedback characters depending on your compiler and your Operating System.
b | Binary mode. End of file is reached at last byte of the file. No conversions.

## Return Values

If the file has been successfully opened the function will return a pointer to the file. Otherwise it returns a NULL pointer.

## Example

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.

Copy code

    #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);

    Example:
            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.

Copy code

    #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

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