×

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

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

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

7 Best IDEs for C/C++ Developers in 2024

As we all know that in programming languages, C is known as the building block which cannot be contradicted, and C++ is the prolong kind of C and it can...

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.

Snake Game in C

Snake game in C is basic desktop console program which was created in 1970’s. It is an old classic game which was played by every child across the world. This...

4 minutes read.

Recursion in C

Recursion in C: In the C programming language, the concept known as recursion exists that is a technique in which a function calls itself either directly or indirectly. It allows...

4 minutes read.

How to include graphics.h in C?

How to include graphics.h in code blocks? Graphics .h is a header that allows drawing lines, rectangles, ovals, arcs, polygons, pictures, and strings on a graphical window by giving access to...

6 minutes read.

Fibonacci Series in C Using For Loop

In this C article, we will let you know about the procedure of displaying the Fibonacci series of the first “n” positive integers. The syntax of for loop used in the...

4 minutes read.

Call by Value and Call by Reference in C

Call by reference and call by value are two different ways of passing arguments to a function in C programming language. Call by reference means that the called function is...

4 minutes read.

Types of Array in C

What is an Array? One value can be stored in a variable at once. How many variables will you need if you have 100 values? Well, the answer isn't 100. It...

14 minutes read.

getch() function in C

getch() is a pre-define or built-in function present in the conio.h library. It returns the given character immediately without waiting for the enter key to be entered. By using getch()...

3 minutes read.

Sizeof in C

A pointer in C is defined as a variable that stores the information of the address of another variable. A pointer can be incremented or decremented, which means we can...

4 minutes read.

Loop Statement in C

Loop statement is used to execute one or more statement repeatedly multiple times. There are three types of loops in C language. Why use loop? We can use loop because it executes a...

2 minutes read.

Prime Number Program in C using for Loop

In this article, we will know about the procedure of checking whether a natural number inputted by the user is a prime number or non-prime number. Definition of the prime number A...

3 minutes read.

Comma Operator in C

What is a comma operator? The comma operator in the C programming language has the least priority. The comma operator is essentially a binary operator that operates on the first operand...

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

Explain the Increment and Decrement Operators in C

In the C programming language, the increment operator (++) and the decrement operator (--) are used to increase or decreasing a variable’s value by 1, respectively. The increment operator is written...

10 minutes read.

Operators in C

An operator is a special symbol that tells the compiler to perform specific mathematical or logical operations. It is the combination of constants and variables through expressions. Example: int c = a+b*5 Where,...

5 minutes read.

Free() Function in C

Free() function is a built-in function which is define in the stdlib.h header file. If we want to use this function in our program, we must include the stdlib.h header...

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.