×

How to delete a file in C

A file is a group of data kept on a secondary device, such as a hard disc. It is typically utilized as a real-world application with a lot of data.

These applications have two following problems:

  1. Handling such a massive volume of data requires a lot of time and effort.
  2. The entire data is lost if the software is closed or the computer is turned off when the I/O terminal is in use. Consequently, it is essential to save the data on a durable medium.

There are two categories of files

  1. Stream-oriented - They are either high-level or standard files. Compared to system-oriented data files, they are more common and simpler to utilize.
  2. System-oriented - These files are of a low level.

The library functions to manage the files are given below:

  • High-level file I/O functions - They have command over their buffers.
  • Low-level file I/O functions - Programmers are responsible for managing buffers.

File operations in C Language

  • Opening a file (fopen())
  • Closing a file (fclose())
  • Reading a file (fscanf())
  • Writing a file (fputs())

Opening a file: Create a file pointer first before a user can access any file.

Syntax:

FILE *ptr;

Closing a file: A file is closed using the fclose() method. The file pointer is separated from a file when this function is utilized.

Syntax:

int fclose(FILE *fp);

Reading a file: Using the method fopen(), open a file and save its reference in a FILE pointer. Use one of these functions, fgetc(), fgets(), fscanf(), or fread(), to read the file's contents (). Close the file using the function fclose ().

There are numerous techniques to read a text file. Several of them are.

  1. Completely read a file's contents.
  2. Read every word in a text file.
  3. Read each character in the text file.

Writing a file: Using fprintf() and fscanf, we can read from and write to text files (). They are all file versions of the printf() and scanf() functions. The main distinction is that fscanf() and fprintf() require a pointer to the FILE structure.    

Syntax:

int print(FILE *stream, const char *format);

To add a line to a file, use the fputs() function.

Syntax:

Int fputs(const char *str,FILE *stream);

To the stream pointed to by stream, the fputs() function writes the string pointed to by str. It returns 0 upon successful completion and end of file otherwise.

Delete File by using C program

Use the remove () method of stdio. h to remove a file in the C programming language. The remove() function deletes a file by taking a file name (or path if it is not in the same directory) as an input. Remove () returns 0 if the file is successfully deleted. Else it produces a non-zero value. We'll delete a file using stdio.h's remove() function.

What is remove() in C?

Remove is a C library function (). The C library's int remove(const char *filename) function deletes and renders unavailable the provided filename.

Syntax:

 remove (“file_name”);

What is the remove function?

The REMOVE() function returns a fixed-length string after removing unnecessary characters from character data.

Delete a file using the C language

This C application deletes the user-specified file. The file to be removed must exist in the directory where this program's executable file is located.

The file is deleted using a remove macro, and the user must additionally specify the file extension. If a problem removes the file, a message will be printed using the error function.

/* This C program shows how to delete user-specified files from the directory */ 
#include<stdio.h>
#include<conio.h>
int main()
{
    int status;
    char file_name[25]; //declaration of file of character type
    
    printf("Enter the name of file you wish to delete\n");
    gets(file_name); //File input
    
    status = remove(file_name); //deletion or removal of file
    
    if( status == 0 ){    
        printf("%s file deleted successfully.\n",file_name);
    }    
    else
    {    
        printf("Unable to delete the file\n");
        perror("Error");    
    }
return 0;
}//main block ends

Output

How to delete a file in C

The deleted file does not go to the trash or recycle bin, so the user might not be able to get it back. Delete files can be restored using specialized recovery software if they aren't overwritten on the storage medium.


Related Topics

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 minutes read.

Double Specifier in C

What is a double specifier? The term double denotes the double data type in C. It more accurately depicts floating point numbers. The idea that it has twice the precision of...

3 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.

Caesar Cipher Program in C

What is Caesar Cipher? The Caesar Cipher is a type of substitution cipher that is named after Julius Caesar, who is said to have used it to encrypt messages sent to...

2 minutes read.

How to Calculate Time Complexity in C?

What is time complexity? An algorithm's time complexity measures how long it takes to complete a task in relation to the size of the input. It should be noted that the...

5 minutes read.

Prime Number code in C

Prime Number Programs in C language In this tutorial, we will learn how to check whether the given number is a prime number or not, and how to print all the...

4 minutes read.

C Program to find the Roots of a Quadratic Equation

What is Quadratic Equation: Equations of degree 2 in polynomial form are called quadratic equations. A, B, and C are the coefficient variables in the equation, which is written as ax2...

3 minutes read.

Why does sizeof(x++) not Increment x in C

Sizeof() is a much-involved operator in C or C++. It is an incorporated time unary administrator which can be utilized to figure the size of its operand. The consequence of...

5 minutes read.

Type Conversion in the C

The process of changing one data type to another in the C programming language is known as "type conversion." The type conversion method is only executed on data types that...

4 minutes read.

Ferror() in c

Ferror():  In C, the ferror() function checks assuming there is a blunder in the given stream. The ferror() function is utilized to check for the document blunder on given stream. A return...

4 minutes read.

Big O Notation in C

Big O Notation in C: In the C programming language, we have so many algorithms and solutions to the problems that exist, which have different aspects and purposes to an...

3 minutes read.

Pyramid Pattern in C

Pyramid Pattern in C A pyramid is a polyhedron created by connecting a base that will be a polygon, and all the lateral faces are triangles. All the bases are connected...

5 minutes read.

tolower() Function in C

The tolower() capability is characterized in the ctype.h header document. In the event that the person passed is a capitalized letter set, the tolower() capability switches capitalized letters in order...

3 minutes read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

Advantages of Dynamic Memory Allocation in C

Dynamic Memory Allocation Dynamic memory allocation is a process of assigning or allocating memory to a variable during the execution of a program. Dynamic memory allocation provides storage according to the...

3 minutes read.

Find Day from Day in C Without using function

Introduction: In the given article, I find daily in C without using functions. It takes 365 days for the earth to revolve around the sun. It will be close to...

3 minutes read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

What is required in each C Program?

Each C program must require one function, i.e., main() function. It is because when we execute the C program, C compiler looks for the main() function, and from here only...

5 minutes read.

Array Of Structures in C

The C programming language allows us to store multiple elements of the same type in arrays. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

fflush in C

In this article, we will understand what is fflush(), the need for fflush and fflush(stdin), and fflush(stdout). The fflush() function is used to clear the output buffer and move the buffered...

3 minutes read.