C Program To Delete A File https://www.skillvertex.com/blog Wed, 06 Mar 2024 11:38:38 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 https://www.skillvertex.com/blog/wp-content/uploads/2024/01/favicon.png C Program To Delete A File https://www.skillvertex.com/blog 32 32 C Program To Delete A File https://www.skillvertex.com/blog/c-program-to-delete-a-file/ https://www.skillvertex.com/blog/c-program-to-delete-a-file/#respond Wed, 06 Mar 2024 11:38:38 +0000 https://www.skillvertex.com/blog/?p=3630 Read more]]>

Table of Contents

C Program To Delete A File

The remove() function is used in C/C++ to delete a file. It is part of the standard I/O library and is declared in the <stdio.h> header file. The function returns 0 if the file is deleted successfully. However, if there is an issue with deleting the file, it returns a non-zero value.

Syntax of remove()

remove("filename");

Parameters

This function will take a string as a parameter, which can represent the name of the file to be deleted.

Return Value

The function will return 0 if the file is deleted successfully. Otherwise, it will give a non-zero value.

Examples of remove()

C program will illustrate the use of the remove() function.



// C program that demonstrates
// the use of remove() function
#include <stdio.h>
 
int main()
{
    if (remove("abc.txt") == 0)
        printf("Deleted successfully");
    else
        printf("Unable to delete the file");
 
    return 0;
}

Output

If file deleted successfully
Deleted successfully
            OR
If file not deleted successfully
Unable to delete the file

Example 2:

// C program that can destroyitself
//after it is compiled and executed
//using remove() function.
#include <stdio.h>
#include <stdlib.h>
 
int main(int c, char* argv[])
{
    printf("By the time you will compile me I will be "
           "destroyed \n");
 
    // array of pointers to command line arguments
    remove(argv[0]);
 
    // Note: argv[0] will contain the executable file i.e.
    // 'a.out'
 
    return 0;
}

Output

By the time you will compile me I will be destroyed

Explanation

In a Linux environment, the remove() function in a C program could potentially delete the program’s executable file (a.out). However, caution should be exercised as deleting the running program’s executable file may lead to unexpected behavior. It’s advisable to use file manipulation functions responsibly and implement checks to prevent accidental deletions.

Note: After the output shown above, the a.out file will be removed.

FAQ- C Program To Delete A File

Q1. How to use delete in C?

Ans. The delete keyword in C++ replaces the free function in C, freeing memory allocated with new. The code declares and allocates memory for an integer using new with ptr1, and for a float using new with ptr2. Later, memory is deallocated using delete for both pointers to prevent memory leaks.

Q2. How to clear txt file in C?

Ans. Using fopen() with “w” mode can clear text files in C. While opening a file with the “w” flag it will produce an empty file for writing. If a file with the same name already exists, its contents are erased and the file is treated as an empty new file

Q3.How to delete C program files in command prompt?

Ans.The syntax for deleting a file will be ‘del “filename”
The syntax for force deleting a file will be ‘ del /f “filename”
del file1 file2 file3 file4.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

]]>
https://www.skillvertex.com/blog/c-program-to-delete-a-file/feed/ 0