In C, fopen() is used to open a file in different modes. To open a file in write mode, “w” is specified. When mode “w” is specified, it creates an empty file for output operations.
What if the file already exists?
If a file with the same name already exists, its contents are discarded and the file is treated as a new empty file. For example, in the following program, if “test.txt” already exists, its contents are removed and “GeeksforGeeks” is written to it.
#include <stdio.h> #include <stdlib.h> int main() { FILE *fp = fopen("test.txt", "w"); if (fp == NULL) { puts("Couldn't open file"); exit(0); } else { fputs("GeeksforGeeks", fp); puts("Done"); fclose(fp); } return 0; } |
The above behavior may lead to unexpected results. If programmer’s intention was to create a new file and a file with same name already exists, the existing file’s contents are overwritten.
The latest C standard C11 provides a new mode “x” which is exclusive create-and-open mode. Mode “x” can be used with any “w” specifier, like “wx”, “wbx”. When x is used with w, fopen() returns NULL if file already exists or could not open. Following is modified C11 program that doesn’t overwrite an existing file.
#include <stdio.h> #include <stdlib.h> int main() { FILE *fp = fopen("test.txt", "wx"); if (fp == NULL) { puts("Couldn't open file or file already exists"); exit(0); } else { fputs("GeeksforGeeks", fp); puts("Done"); fclose(fp); } return 0; } |
References:
Do not make assumptions about fopen() and file creation
http://en.wikipedia.org/wiki/C11_(C_standard_revision)
http://www.cplusplus.com/reference/cstdio/freopen/
This article is compiled by Abhay Rathi. 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:
- C fopen() function with Examples
- lseek() in C/C++ to read the alternate nth byte and write it in another file
- Write a C program that displays contents of a given file like 'more' utility in Linux
- Read/Write structure to a file in C
- How to write your own header file in C?
- C program to copy contents of one file to another file
- C program to check whether the file is JPEG file or not
- Write a C program to print "Geeks for Geeks" without using a semicolon
- Write a one line C function to round floating point numbers
- Write a C macro PRINT(x) which prints x
- Write one line functions for strcat() and strcmp()
- When should we write our own copy constructor?
- Does C++ compiler create default constructor when we write our own?
- When should we write our own assignment operator in C++?
- Write a C program that does not terminate when Ctrl+C is pressed
- Write a C program that won't compile in C++
- Write a program that produces different results in C and C++
- How to write a running C code without main()?
- Write your own memcpy() and memmove()
- How to write long strings in Multi-lines C/C++?

