How to find the size of a file in C?

Determining the size of a file is a common task in C programming, crucial for managing resources efficiently.

This comprehensive guide explores various techniques to find the size of a file in C, covering standard library functions, system calls, and best practices for accurate and reliable results.

Table of Contents #

  1. Introduction to File Size in C
  2. Using Standard Library Functions
  3. Using System Calls (UNIX/Linux Only)
  4. Best Practices for File Size in C
  5. Conclusion

1. Introduction to File Size in C

The size of a file represents the amount of storage it occupies on a system.

In C, finding the size of a file is essential for tasks such as memory allocation, copying files, and ensuring the integrity of data operations.

2. Using Standard Library Functions

2.1. Using "fseek" and "ftell" Functions

The standard library functions fseek and ftell can be employed to determine the size of a file.

The basic idea is to seek to the end of the file and then retrieve the file position.

#include <stdio.h>

long int getFileSize(FILE* file) {
    long int size;

    // Seek to the end of the file
    fseek(file, 0, SEEK_END);

    // Get the file position, representing the size
    size = ftell(file);

    // Seek back to the beginning of the file (optional)
    fseek(file, 0, SEEK_SET);

    return size;
}

int main() {
    FILE* file = fopen("example.txt", "r");

    if (file != NULL) {
        long int size = getFileSize(file);
        printf("File size: %ld bytes\n", size);

        fclose(file);
    } else {
        perror("Error opening file");
    }

    return 0;
}

2.2. Using "fstat" Function (UNIX/Linux Only)

On UNIX/Linux systems, the fstat function can be used to obtain file status, including the file size.

#include <stdio.h>
#include <sys/stat.h>

long int getFileSize(FILE* file) {
    struct stat fileStat;

    if (fstat(fileno(file), &fileStat) == 0) {
        return fileStat.st_size;
    }

    return -1;  // Error
}

int main() {
    FILE* file = fopen("example.txt", "r");

    if (file != NULL) {
        long int size = getFileSize(file);
        if (size != -1) {
            printf("File size: %ld bytes\n", size);
        } else {
            perror("Error getting file size");
        }

        fclose(file);
    } else {
        perror("Error opening file");
    }

    return 0;
}

3. Using System Calls (UNIX/Linux Only)

On UNIX/Linux systems, the stat system call can be employed to obtain file information, including the size.

#include <stdio.h>
#include <sys/stat.h>

long int getFileSize(const char* filename) {
    struct stat fileStat;

    if (stat(filename, &fileStat) == 0) {
        return fileStat.st_size;
    }

    return -1;  // Error
}

int main() {
    const char* filename = "example.txt";

    long int size = getFileSize(filename);
    if (size != -1) {
        printf("File size: %ld bytes\n", size);
    } else {
        perror("Error getting file size");
    }

    return 0;
}

4. Best Practices for File Size in C

5. Conclusion

Determining the size of a file in C is a fundamental operation, and several methods are available to achieve this.

Whether using standard library functions like fseek and ftell or system calls like stat, understanding these techniques allows programmers to handle file sizes accurately and efficiently.

By incorporating best practices and error handling, C programs can navigate file sizes reliably, contributing to robust file management and data processing.