×

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given statements.

C: C is a programming language developed at AT and T in bell laboratories by Dennis Ritchie, it is a system programming language and procedure-oriented programming language, and it is known as a mid-level programming language.

Features of C Language

  • Portability
  • Easy to extend
  • Fast and efficient
  • Simple and faster
  • Rich in library

The size of a File concept is part of the C Programming language, which we will discuss below:

What is the file size?

In our daily life or the life of software, we come across many files which may be of different formats and different sizes. We also use a zip file where we compress the file to create this. To know the content in the zip file, we need to extract the file, and then we need to import the file into our laptop. File size refers to how much information or data is in the system file.

File sizes may be of different sizes. For example, it can be expressed in bytes, bits, kilobytes (KB), megabytes (MB), gigabytes (GB), etc. In the mentioned above sizes, a gigabyte is the highest file size, and we are going to have many other which are greater than gigabytes. The picture shown below is very familiar to us in our systems.

C program to find the size of a File

1) Program to find file size

#include<stdio.h>
#include<stdlib.h>
int main()
{
	FILE *fp;
	int n;
	fp=fopen("demo.txt"r");
	if(fp==NULL)
	{
		printf("\nError cannot open file");
		exit(1);
	}
	fseek (fp,0, SEEK_END); 
	n=ftell(fp); 
	printf("\nSize of the file = %d", n) ;
	fclose(fp);
}

Output:

Size of the file = 23

Explanation of above Program

To get the file size, we commonly use certain functions like fseek(), ftell(), fopen(), and fclose(). This will perform an individual task.

fseek(): This function is used to move or transfer file pointer, which is linked with the present file you have created to a particular position or destination.

ftell(): This function is used to find the total file size after the file pointer is moved to the particular position

fclose(): This function is used to close the stream. Used at the last of the Program to exit from the stream.

fopen(): This function is used to open a file and perform a specific task or operation, which consists of writing and reading, which are represented with the parameters w and r.

These functions come in libraries that need to be imported from the standard library, which is indicated as #include<stdio.h>.

2) Program to find file size

#include<stdio.h>
#include<conio.h>


int main()
{
    FILE *fp;
    char ch;
    int size = 0;


    fp = fopen("demo.txt", "r");
    if (fp == NULL)
    {
        printf("\nunable to open the file");
    }
    else
    {
        printf("\nopened the file");
    }
    fseek(fp, 0, SEEK_END);   
    size = ftell(fp);   
    printf("\nthe opened file size is %d\n", size);
    fclose(fp);
}

Output:

Opened the file
The opened file size is 23 

Explanation of above Program

In the above Program, we will open the file in a reading mode which lets us know the size after opening, and we cannot edit the text file. If you want to edit the text, we need to open the file using writing mode, which is indicated with the parameter 'w', and reading mode is indicated with the parameter 'r'. Now we are going to use conditional statements in which we will specify if fp = NULL, we are going to print unable to open the file. Else, it will print the opened file. When fopen() is unable to open a file, then it will display NULL, then this word is being used in the conditional statements if and else.

Compact Program to find the Size of a File:

#include<stdio.h>
int main()
{
 FILE *fp;
 long int p;
 fp=fopen("demo.txt", "r");
 fseek(fp,0,SEEK_END);
 p=ftell(fp);
 printf("the opened file size is : %ld bytes.",p);
 return 0;
}

Output:

The opened file size is: 23

Note: In my case file size is 23, but it may vary for others.

Explanation of above Program

The above Program is the simple one. In this Program, we have not used conditional statements or any other complicated statements that could raise an error.

We used 3 functions, fopen, fseek, and ftell, in which one will open the file, another will change the destination of the file pointer, and another will tell the file size. This is the process involved in finding the size of the file.

Note: In the fopen function in the above all programs, we give "demo.text" because we need to find the size of that file, and that file should exist in your system or computer.

If the file does not open, it will display an error or other information on the terminal.

This is all about the size of the file in c programming language and their programs.


Related Topics

Use of free() function in C

Introduction The free() function uses in C programming language. The free() function in the C programming language uses to release or deallocate the memory blocks; these blocks are previously allocated by calloc(), malloc() or realloc()...

3 minutes read.

C program that does not Suspend when Ctrl+Z is Pressed

In Programming when a program breakdown and runs surprisingly in the terminal compiler the developer has the ability to prevent the program from running unequivocally. For expressly halting the program,...

3 minutes read.

How to Find Square Free Numbers in C?

Introduction Square-free number program is a typical interview and academic question in C Programming. In this section, we will define square-free integers, construct algorithms and C program to print the nth...

11 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

getc() function in C

Getc is one of the file handling technique in C. The Getc() is a C library function gets the next character or new characters  from the specific stream and supports...

4 minutes read.

10 Best IDEs for C or C++ Developers in 2024

Nobody can deny the fact that C and C++ were the first programming languages used by significant developers worldwide. Even now, newcomers who want to start programming are most frequently...

6 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

Difference between Array and List in C

C# Array vs List is where the abstraction and implementation of human computing meet.  Arrays are incredibly related to the hardware concept of contiguous and contiguous memory, where each part is...

3 minutes read.

Deadlock Detection Program in C

What is Deadlock? A deadlock is a circumstance where a group in the process is halted because they are each holding onto resources while waiting for other methods to obtain them. Think...

5 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

Difference between C and Java

C programming and Java programming are two of the earliest programming languages. C programming follows a procedural approach whereas Java programming follows an object-oriented approach. Java programming is a part...

3 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.

C and C++ Binary Files

What is a binary file? A file in which the content is written in binary format is called a binary file. A binary file is not a text file. There are...

7 minutes read.

How to get ASCII value in C

For text data on computers and the internet, ASCII (American Standard Code for Information Interchange). It is the most widely used character encoding standard. One hundred twenty-eight alphabetic, numeric, special...

3 minutes read.

Floor() Function in C

Floor() function is a built-in function in C which is defined in the math.h header file. This function is used to return the nearest integer value which is less than...

4 minutes read.

Displaying Array in C

Reference a collection of variables of similar data type in an array using a single element. The parts are stored next to each other. When declaring an array, you must specify...

4 minutes read.

Find Union and Intersection of Two Arrays in C

Union You can find the union of the two sorted arrays using the join merge method on arr1[] and arr2[]. Use two index variables i and j with initial values i =...

4 minutes read.

Pseudo Code in C

Pseudo code in C can be referred to as a simple way or method to write programming code or program algorithm in English. A pseudocode is an informal representation of...

4 minutes read.

Multiplication table program in C using For loop

Before we move on the program of multiplication table of any natural number, first we have to know about the For loop statement. The syntax of ‘for’ loop in C programming...

3 minutes read.