🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
rename() function in C is a standard library function that renames a file or directory. It is part of the C standard library (stdio.h) and is commonly used for file management operations.The rename() function is essential for file management in C. It allows you to change the name of a file or directory, moving it to a new name or location within the same filesystem.
rename() Function Syntax
The syntax for the rename() function is as follows:
int rename(const char *old_filename, const char *new_filename);
Parameters:
old_filename: A C string that contains the current name of the file or directory.new_filename: A C string that contains the new name for the file or directory.
Returns:
- The function returns
0if the file or directory was successfully renamed. If an error occurs, a non-zero value is returned.
Examples
Renaming a File
To demonstrate how to use rename() to rename a file, we will write a simple program.
Example
#include <stdio.h>
int main() {
const char *old_filename = "old_name.txt";
const char *new_filename = "new_name.txt";
// Attempt to rename the file
if (rename(old_filename, new_filename) == 0) {
printf("File renamed successfully from %s to %s.\n", old_filename, new_filename);
} else {
printf("Error: Could not rename the file %s.\n", old_filename);
}
return 0;
}
Output (if the file is renamed successfully):
File renamed successfully from old_name.txt to new_name.txt.
Output (if the file does not exist or cannot be renamed):
Error: Could not rename the file old_name.txt.
Real-World Use Case
Organizing Files
In real-world applications, the rename() function can be used to organize files by renaming them according to a specific naming convention or moving them to different directories.
Example
#include <stdio.h>
#include <string.h>
void organize_file(const char *filename, const char *new_directory) {
char new_filename[256];
snprintf(new_filename, sizeof(new_filename), "%s/%s", new_directory, filename);
// Attempt to rename (move) the file
if (rename(filename, new_filename) == 0) {
printf("File %s moved to %s.\n", filename, new_directory);
} else {
printf("Error: Could not move the file %s.\n", filename);
}
}
int main() {
const char *filename = "example.txt";
const char *new_directory = "organized_files";
// Organize the file by moving it to a new directory
organize_file(filename, new_directory);
return 0;
}
Output (if the file is moved successfully):
File example.txt moved to organized_files.
Output (if the file cannot be moved):
Error: Could not move the file example.txt.
Conclusion
The rename() function is essential for managing files in C. It allows you to change the name of a file or directory, which is useful for organizing files, moving them to different locations, or updating their names to match new naming conventions.
By understanding and using this function, you can efficiently manage file operations in your C programs. Always ensure to handle errors properly to avoid leaving files in an inconsistent state.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment