w3resource

C Exercises: Remove a file from the disk


15. Delete File from Disk

Write a program in C to remove a file from the disk.

Sample Solution:

C Code:

#include <stdio.h>

void main()
{
	int status;
	char fname[20];
	printf("\n\n Remove a file from the disk :\n");
	printf("----------------------------------\n"); 	
	printf(" Input the name of file to delete : ");
	scanf("%s",fname);
	status=remove(fname);
	if(status==0)
	{
		printf(" The file %s is deleted successfully..!!\n\n",fname);
	}
	else
	{
		printf(" Unable to delete file %s\n\n",fname);
	}
}

Sample Output:

 Remove a file from the disk :                                                                                
----------------------------------                                                                            
 Input the name of file to delete : test.txt                                                                  
 The file test.txt is deleted successfully..!!

Flowchart:

Flowchart: Remove a file from the disk

For more Practice: Solve these Related Problems:

  • Write a C program to delete multiple files specified by the user from a directory.
  • Write a C program to securely delete a file by overwriting its contents before removal.
  • Write a C program to prompt the user for a filename, check if it exists, and then delete it if confirmed.
  • Write a C program to delete all files in a directory that match a specific file extension.

Go to:


PREV : Decrypt File.
NEXT : Common Directory Tree.

C Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.