C Program to Find the Size of a File

In this article, we will write a C program to find the size of a file in bytes and kilobytes both.

The program asks the user to enter the file name or file path, calculates the total size of the file and prints it on the output screen.

Sample Input:

Enter the file name: temp.txt

Sample Output:

File size is 3979 bytes

In C programming language, there is no built-in method that we can directly use to calculate the total size of a file. However, it’s pretty easy and we can do it in just a few lines of code.

The basic idea to calculate the total size of a file is to read the content of the file from start to end and count the total number of characters present in the file.

For example, if a text file contains the content “Programmers”, its size will be 12 bytes. Where the 11 bytes are for the 11 characters of the file and one extra byte is for the end of the file(EOF) character.

To count the total characters from start to end of the file, we can use two built-in functions fseek() and ftell().

The fseek() function lets you move the file pointer from start to end of the file and the ftell() function tells the current position of the file pointer which in our case will be the end of the file.

Let’s first see the implementation of the program and later we will try to understand each line of the code.

// C program to calculate
// the total size of a file

#include<stdio.h>

int main(){
	
	char filename[100];
	
	printf("Enter the file name: ");
	scanf("%s", &filename);
	
	// Try to open the file in read mode
	FILE *fptr = fopen(filename, "r");
	
	if(fptr == NULL){
		printf("File does not exist");
		return -1;
	}
	
	// Seek to the end of the file
	fseek(fptr, 0, SEEK_END);
	
	// Get the current position of fptr which is the file size
	long filesize = ftell(fptr);
	
	if(filesize == -1){
		printf("Unable to determine the file size");
	}
	else{
		printf("File size is %ld bytes", filesize);
	}
	
	// Close the file
	fclose(fptr);
	
    return 0;
    
}

Output:

Enter the file name: temp.txt
File size is 3979 bytes

Enter the file name: data.txt
File does not exist

Code Explanation:

Here is a step by step explanation of the above program:

  • User Input: The program starts by declaring a character array filename to store the name of the file the user wants to check. It then prompts the user to enter the name of the file using printf() and reads the input using scanf().
  • File Opening: The program attempts to open the specified file in read mode (“r”) using the fopen() function. If the file does not exist or if there are any issues with opening it, fopen() returns NULL.
  • File Seeking: After successfully opening the file, the program uses fseek() to seek to the end of the file. This is done to position the file pointer at the end so that ftell() can determine the file’s size.
  • File Size Determination: The ftell() function is then used to retrieve the current position of the file pointer, which corresponds to the file’s size in bytes. If ftell() returns -1, it indicates an error in determining the file size.
  • File Closure: Finally, regardless of whether the file size was successfully determined or not, the program closes the file using fclose() to release any associated resources.

I hope this article helped you. Thanks for reading!

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts