Rename and Delete a File in C Using rename() and remove() with Examples

Renaming and Deleting Files in C Language

In C language, we can rename an existing file and delete a file using standard library functions. The rename() function is used to change the name of an existing file, whereas the remove() function is used to delete a file.

सी लैंग्वेज में Standard Library Functions की सहायता से किसी Existing File का नाम बदला जा सकता है और किसी File को Delete भी किया जा सकता है। rename() Function का उपयोग किसी File का नाम बदलने के लिए तथा remove() Function का उपयोग File को Delete करने के लिए किया जाता है।

Both functions are declared in the standard input/output header file stdio.h.

ये दोनों Functions Standard Input/Output Header File stdio.h में Declare किए जाते हैं।

1. rename() Function in C

The rename() function is used to change the name of an existing file. It takes the old file name and the new file name as arguments.

rename() Function का उपयोग किसी Existing File का नाम बदलने के लिए किया जाता है। इसमें Old File Name और New File Name को Arguments के रूप में दिया जाता है।

Syntax:

rename(old_filename, new_filename);

Parameters:

  1. old_filename – Name of the existing file.

  2. new_filename – New name to be assigned to the file.

Example:

rename("old.txt", "new.txt");

This statement changes the file name from old.txt to new.txt, provided the operation is supported and successful.

यह Statement old.txt नाम की File का नाम बदलकर new.txt कर देता है, यदि यह Operation सफलतापूर्वक पूरा हो जाता है।

Return Value:

The rename() function returns 0 on success and a non-zero value if an error occurs.

सफलतापूर्वक Execute होने पर rename() Function 0 Return करता है और Error होने पर Non-Zero Value Return करता है।

C Program to Rename a File Using rename()

The following program renames an existing file from old.txt to new.txt using the rename() function.

निम्नलिखित प्रोग्राम rename() Function की सहायता से old.txt नाम की Existing File का नाम बदलकर new.txt करता है।

Program:

#include <stdio.h>
int main(void)
{
if (rename("old.txt", "new.txt") == 0)
{
printf("File renamed successfully.\n");
}
else
{
printf("Unable to rename the file.\n");
return 1;
}
return 0;
}

Output:

File renamed successfully.

Explanation:

  1. rename("old.txt", "new.txt") changes the name of the file from old.txt to new.txt. यह File का नाम old.txt से बदलकर new.txt करता है।

  2. The if statement checks whether the function returns 0. यह Check करता है कि Rename Operation सफल हुआ है या नहीं।

  3. If the function returns 0, the program displays a success message. यदि Function 0 Return करता है, तो Program Success Message प्रदर्शित करता है।

2. remove() Function in C

The remove() function is used to delete an existing file. It takes the file name as an argument.

remove() Function का उपयोग किसी Existing File को Delete करने के लिए किया जाता है। इसमें Delete की जाने वाली File का Name Argument के रूप में दिया जाता है।

Syntax:

remove(filename);

Example:

remove("new.txt");

This statement deletes the file named new.txt, provided the operation is successful.

यह Statement new.txt नाम की File को Delete कर देता है, यदि Operation सफल होता है।

Return Value:

The remove() function returns 0 on success and a non-zero value if an error occurs.

सफलतापूर्वक Execute होने पर remove() Function 0 Return करता है और Error होने पर Non-Zero Value Return करता है।

C Program to Delete a File Using remove()

The following program deletes an existing file named new.txt using the remove() function.

निम्नलिखित प्रोग्राम remove() Function की सहायता से new.txt नाम की Existing File को Delete करता है।

Program:

#include <stdio.h>
int main(void)
{
if (remove("new.txt") == 0)
{
printf("File deleted successfully.\n");
}
else
{
printf("Unable to delete the file.\n");
return 1;
}
return 0;
}

Output:

File deleted successfully.

Explanation:

  1. remove("new.txt") deletes the specified file. यह दी गई File को Delete करता है।

  2. The if statement checks the return value of remove(). यह Function के Return Value को Check करता है।

  3. If the function returns 0, the program displays a success message. यदि Function 0 Return करता है, तो Program Success Message प्रदर्शित करता है।

3. C Program to Rename and Delete a File

The following program first renames old.txt to new.txt and then deletes the renamed file using remove().

निम्नलिखित प्रोग्राम पहले old.txt का नाम बदलकर new.txt करता है और फिर remove() Function की सहायता से नई File को Delete करता है।

Program:

#include <stdio.h>
int main(void)
{
/* Rename the file */
if (rename("old.txt", "new.txt") == 0)
{
printf("File renamed successfully.\n");
}
else
{
printf("Unable to rename the file.\n");
return 1;
}
/* Delete the renamed file */
if (remove("new.txt") == 0)
{
printf("File deleted successfully.\n");
}
else
{
printf("Unable to delete the file.\n");
return 1;
}
return 0;
}

Output:

File renamed successfully.
File deleted successfully.

4. Difference Between rename() and remove()

rename()remove()
Used to change the name of a file.Used to delete a file.
Takes old and new file names.Takes the file name to be deleted.
Returns 0 on success.Returns 0 on success.
Does not normally delete the file's contents.Removes the specified file.

rename() Function का उपयोग File का नाम बदलने के लिए किया जाता है, जबकि remove() Function का उपयोग File को Delete करने के लिए किया जाता है। दोनों Functions सफल होने पर 0 Return करते हैं।

5. Important Points About rename() and remove()

  1. Both rename() and remove() functions are declared in stdio.h. इन दोनों Functions के लिए stdio.h Header File Include करना आवश्यक है।

  2. The file must exist before using rename() or remove(). इन Functions का उपयोग करने से पहले संबंधित File का उपलब्ध होना आवश्यक है।

  3. The program must have sufficient permission to rename or delete the file. Program के पास File को Rename या Delete करने की उचित Permission होनी चाहिए।

  4. The behavior of rename() when the destination file already exists can depend on the implementation and operating system. इसलिए Rename करने से पहले यह सुनिश्चित करना चाहिए कि Destination File के पहले से मौजूद होने की स्थिति को सही तरीके से Handle किया गया है।

  5. File paths are interpreted relative to the program's current working directory when relative paths are used. यदि Relative Path का उपयोग किया जाता है, तो File का Location Program की Current Working Directory के अनुसार निर्धारित होता है।

  6. Always check the return value of these functions to detect errors. किसी भी File Operation के सफल या असफल होने की जानकारी के लिए Return Value को Check करना चाहिए।

The remove() function deletes the specified file. Use it carefully because the deleted file may not be recoverable through the program.

remove() Function दी गई File को Delete कर देता है। इसका उपयोग सावधानीपूर्वक करें क्योंकि Program के माध्यम से Delete की गई File को वापस प्राप्त करना संभव नहीं भी हो सकता है।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025