Share this page

# fclose

Input-Output Functions

Closes a file.

    int fclose( FILE *file_pointer);

file_pointer | A pointer to the file to close.

## Return Values

If the stream is successfully closed, returns 0. If an error occurs, returns EOF.

For fclose details, refer to your C language documentation.

## Example

The following example, for Windows platforms, opens a file, reads it into a buffer, and finally closes the file using fclose.

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";
        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;
            }
     // Add up actual bytes read
            total += count;
        }
    // 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

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: