We will discuss about four file hacks listed as below-
- Rename – Rename a file using C/C++
- Remove – Remove a file using C/C++
- File Size – Get the file size using C/C++
- Check existence – Check whether a file exists or not in C/C++
// A C++ Program to demonstrate the // four file hacks every C/C++ must know // Note that we are assuming that the files // are present in the same file as the program // before doing the below four hacks #include<stdio.h> #include<stdlib.h> #include<stdbool.h> // A Function to get the file size unsigned long long int fileSize(const char *filename) { // Open the file FILE *fh = fopen(filename, "rb"); fseek(fh, 0, SEEK_END); unsigned long long int size = ftell(fh); fclose(fh); return (size); } // A Function to check if the file exists or not bool fileExists(const char * fname) { FILE *file; if (file = fopen(fname, "r")) { fclose(file); return (true); } return (false); } // Driver Program to test above functions int main() { printf("%llu Bytes\n", fileSize("Passwords.txt")); printf("%llu Bytes\n", fileSize("Notes.docx")); if (fileExists("OldData.txt") == true ) printf("The File exists\n"); else printf("The File doen't exist\n"); rename("Videos", "English_Videos"); rename("Songs", "English_Songs"); remove("OldData.txt"); remove("Notes.docx"); if (fileExists("OldData.txt") == true ) printf("The File exists\n"); else printf("The File doesn't exist\n"); return 0; } |
chevron_right
filter_none
Screenshot before executing the program :

Screenshot after executing the program :

This article is contributed by Rachit Belwariar. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Bitwise Hacks for Competitive Programming
- C | File Handling | Question 1
- C | File Handling | Question 2
- C | File Handling | Question 3
- C | File Handling | Question 4
- C | File Handling | Question 5
- tellp() in file handling with c++ with example
- Basics of File Handling in C
- File Handling through C++ Classes
- Set position with seekg() in C++ language file handling
- How does free() know the size of memory to be deallocated?
- C program to copy contents of one file to another file
- When should we write our own copy constructor?
- Comma operator should be used carefully
- When should we write our own assignment operator in C++?
- Why copy constructor argument should be const in C++?
- Why we should avoid using std::endl
- Top 5 IDEs for C++ That You Should Try Once
- If memory allocation using new is failed in C++ then how it should be handled?
- Exception handling and object destruction | Set 1


