
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
fopen() for existing file in write mode in C
fopen() method in C is used to open the specified file.
Let’s take an example to understand the problem
Syntax
FILE *fopen(filename, mode)
The following are valid modes of opening a file using fopen(): ‘r’, ‘w’, ‘a’, ‘r+’, ‘w+’, ‘a+’. For details visit visit C library function - fopen()
fopen() for an existing file in write mode
If the file to be opened does not exist in the current directory then a new empty file with write mode is created.
If the file to be opened exists in the current directory and is open using ‘w’ / ‘w+’, the contents will be deleted before writing.
Example
Program to illustrate the working of our solution
#include <stdio.h> #include <stdlib.h> int main(){ FILE *opFile = fopen("test.txt", "w"); if (opFile == NULL){ puts("Couldn't open file"); exit(0); } else{ fputs("includehelp", opFile); puts("Write operation successful"); fclose(opFile); } return 0; }
Output
Write operation successful
Initial contents of the file − C programming language
Contents after append operation − include help
The write operation does its work but deletes all the contents the were present in the file before the write operation is performed. To tackle this, the C programming language has been updated to two different approaches which the programmer can use based on the requirement of the program.
‘a’ (append) mode − this mode appends the new contents at the end of the contents written in the file.
‘wx’ mode − this will return NULL if the file already exists in the directory.
Example
Program to illustrate write operation on existing file using ‘a’ mode
#include <stdio.h> #include <stdlib.h> int main(){ FILE *opFile = fopen("test.txt", "a"); if (opFile == NULL){ puts("Couldn't open file"); exit(0); } else{ fputs("includehelp", opFile); puts("Write operation successful"); fclose(opFile); } return 0; }
Output
Write operation successful
Initial contents of the file − C programming language
Contents after append operation − C programming language includehelp
Example
Program to illustrate write operation on existing file using ‘wx’ mode
#include <stdio.h> #include <stdlib.h> int main(){ FILE *opFile = fopen("test.txt", "wx"); if (opFile == NULL){ puts("Couldn't open file"); exit(0); } else{ fputs("includehelp", opFile); puts("Write operation successful"); fclose(opFile); } return 0; }
Output
Write operation successful
- Related Articles
- fopen() for an existing file in write mode in C
- Write a C program to find total number of lines in an existing file
- How to open a file in read and write mode with Python?
- How to open a binary file in read and write mode with Python?
- Data file mode in 8085 Microprocessor
- fopen() function in PHP
- Explain write mode operation of files in C language
- Check whether the given file is an existing file in Java
- C# Program to make a copy of an existing file
- How to write my own header file in C?
- Read/Write Class Objects from/to File in C++
- What is fopen() and open() in Linux?
- Golang program to append a string in an existing file
- Check for Existing Document in MongoDB?
- How to open a file in binary mode with Python?
