×

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

Infix to Postfix program in C

Infix Expression: In infix expression, an operator is placed between the two operands. Example: x + y, here operator + is placed between operands x and y. Postfix Expression: In...

3 minutes read.

Scope of variables in C

Introduction The scope of variables in C can be defined as the scope of reach of a variable, the term scope is used to determine the visible range of an object....

4 minutes read.

Branching Statements in C

What is branching in c? Branching gets its name from the fact that the computer can select which branch to follow.Programs written in the C language execute statements one after the...

6 minutes read.

What is the main in C?

In the C programming language, "main" is a predefined term or function. It is the primary function in every C program and is in charge of beginning and terminating the...

6 minutes read.

Doubly Linked list in C

To know the Doubly Linked List in C, first we should know about how the Linked List works. Linked List The Linked list is the linear data structure. In the Linked...

5 minutes read.

Actual and Formal Parameters

Any variable declared within the parenthesis is referred to as the parameters during the function declaration. Parameters tell the function about the argument datatype, their order, and the number of...

5 minutes read.

Malloc function in C

When the user or programmer does not know how much memory space is needed in the application, it a needs dynamic memory allocation during runtime. Dynamic memory allocation helps us to...

4 minutes read.

Do WHILE LOOP in C Programming Examples

Before understanding the programming examples of the Do-While loop, we have to know what is Do-While loop in C. So, let's start with the definition of the Do-While loop. What is...

5 minutes read.

Self-referential structure in C

Self-referential structure in C A self-referential structure is a structure that can have members which point to a structure variable of the same type. They can have one or more pointers...

3 minutes read.

Runtime vs compile time in C

In the C programming language, the often used terms in every step consist of compile time and runtime. The compile time is referred to the source code that will be...

4 minutes read.

C Language Environment Setup

To compile C program, we must have GCC compiler installed on our machine. In this C tutorial, all the examples are compiled and tested using GCC compiler. Although we can...

3 minutes read.

pow() function in C

pow() function is one of the in-built functions present in math.h header file Power function is used to calculate the powers of the given number. The syntax of the power...

3 minutes read.

C vs Java Strings

String in C In C, we can define a string as a bunch of characters. A character array is distinguished from a string by the presence of the special character '\0'...

5 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

Explain the two-way selection in C

In C programming, a two-way selection statement is used to choose between two different options based on a Boolean expression. The two-way selection statement in C is the if statement. The...

6 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

4 minutes read.

Const vs Volatile in C

Introduction Qualifiers are nothing but keywords which are used to modify the properties of a variable. Const and Volatile keywords are qualifiers in C. The Const qualifier is applied to the...

4 minutes read.

Data Types in C

Data type is a very important concept in C programming language. Simply, “A data type is the classification of data values that a data item can have.” We need to...

11 minutes read.

Variable Declaration in C

What is a Variable? A Variable is nothing more than a name for a memory place where data/information can be stored. Any alphabet (from a to z or A to Z), the...

4 minutes read.

Character Set in C

Introduction Every language has some basic elements which are used to represent information. The English language includes alphabets which together shape a sentence, after which the sentences are used to shape...

3 minutes read.