File Opening Modes in C language - r, w, a, r+, w+, a+, rb, wb, ab, rb+, wb+, ab+ with Examples
In C language, a file is opened using the fopen() function. The opening mode specifies how the program will access the file, such as reading, writing, appending or performing both reading and writing operations. The fopen() function accepts two arguments: the file name and the opening mode. It returns a FILE pointer if the file is opened successfully; otherwise, it returns NULL.
C लैंग्वेज में फाइल को fopen() Function की सहायता से Open किया जाता है। Opening Mode यह निर्धारित करता है कि प्रोग्राम फाइल को किस प्रकार Access करेगा, जैसे Reading, Writing, Appending या Reading और Writing दोनों Operations करना। fopen() Function दो Arguments ग्रहण करता है: फाइल का नाम और Opening Mode। फाइल सफलतापूर्वक Open होने पर यह FILE Pointer Return करता है, अन्यथा NULL Return करता है।
Syntax of fopen()
FILE *fopen(const char *filename, const char *mode);Example:
FILE *fp;fp = fopen("abc.txt", "r");
Here, abc.txt is the file name and r is the opening mode.
यहाँ abc.txt फाइल का नाम है और r Opening Mode है।
Types of File Opening Modes in C
C language provides six commonly used basic file opening modes:
C लैंग्वेज में सामान्यतः छह Basic File Opening Modes का उपयोग किया जाता है:
r– Read Modew– Write Modea– Append Moder+– Read and Write Modew+– Read and Write Modea+– Read and Append Mode
1. Read Mode (r)
The r mode is used to open an existing file for reading. If the specified file does not exist, fopen() returns NULL. The existing contents of the file remain unchanged.
r Mode का उपयोग पहले से मौजूद फाइल को पढ़ने के लिए किया जाता है। यदि दी गई फाइल मौजूद नहीं है, तो fopen() NULL Return करता है। इस Mode में फाइल का पुराना डेटा सुरक्षित रहता है।
Syntax:
fp = fopen("abc.txt", "r");Important Points:
Opens an existing file for reading.
Does not create a new file.
Does not delete existing data.
Writing is not permitted through this stream.
पहले से मौजूद फाइल को Read करने के लिए Open करता है।
नई फाइल Create नहीं करता है।
पुराने डेटा को Delete नहीं करता है।
इस Stream के माध्यम से Writing की अनुमति नहीं होती है।
2. Write Mode (w)
The w mode is used to open a file for writing. If the file does not exist, a new file is created. If the file already exists, its previous contents are deleted when it is successfully opened.
w Mode का उपयोग फाइल में डेटा लिखने के लिए किया जाता है। यदि फाइल मौजूद नहीं है, तो नई फाइल Create की जाती है। यदि फाइल पहले से मौजूद है, तो सफलतापूर्वक Open होने पर उसका पुराना डेटा हट जाता है।
Syntax:
fp = fopen("abc.txt", "w");Important Points:
Opens a file for writing.
Creates a new file if it does not exist.
Deletes previous contents if the file already exists.
Reading is not permitted through this stream.
फाइल को Write करने के लिए Open करता है।
फाइल मौजूद न होने पर नई फाइल Create करता है।
फाइल पहले से मौजूद होने पर पुराना डेटा हटा देता है।
इस Stream के माध्यम से Reading की अनुमति नहीं होती है।
3. Append Mode (a)
The a mode is used to open a file for appending new data at the end of the file. If the file does not exist, a new file is created. Existing contents are not deleted.
a Mode का उपयोग फाइल के अंत में नया डेटा जोड़ने के लिए किया जाता है। यदि फाइल मौजूद नहीं है, तो नई फाइल Create की जाती है। फाइल का पुराना डेटा Delete नहीं होता है।
Syntax:
fp = fopen("abc.txt", "a");Important Points:
Opens a file for appending.
Creates a new file if it does not exist.
Preserves existing contents.
Writing takes place at the end of the file.
Reading is not permitted through this stream.
फाइल को Append करने के लिए Open करता है।
फाइल मौजूद न होने पर नई फाइल Create करता है।
पुराने डेटा को सुरक्षित रखता है।
नया डेटा फाइल के अंत में लिखा जाता है।
इस Stream के माध्यम से Reading की अनुमति नहीं होती है।
4. Read and Write Mode (r+)
The r+ mode is used to open an existing file for both reading and writing. If the file does not exist, it returns NULL. Existing contents are not automatically deleted.
r+ Mode का उपयोग पहले से मौजूद फाइल को Reading और Writing दोनों के लिए Open करने हेतु किया जाता है। यदि फाइल मौजूद नहीं है, तो यह NULL Return करता है। फाइल का पुराना डेटा अपने आप Delete नहीं होता है।
Syntax:
fp = fopen("abc.txt", "r+");Important Points:
Opens an existing file for reading and writing.
Does not create a new file.
Does not automatically delete existing contents.
Writing can overwrite data at the current file position.
Appropriate positioning or flushing is required when switching between reading and writing.
पहले से मौजूद फाइल को Reading और Writing दोनों के लिए Open करता है।
नई फाइल Create नहीं करता है।
पुराने डेटा को अपने आप Delete नहीं करता है।
Writing करने पर Current File Position से डेटा Overwrite हो सकता है।
Reading और Writing के बीच Switch करने के लिए उचित Positioning या Flushing आवश्यक है।
5. Read and Write Mode (w+)
The w+ mode is used to open a file for both reading and writing. If the file does not exist, a new file is created. If it already exists, its previous contents are deleted.
w+ Mode का उपयोग फाइल को Reading और Writing दोनों के लिए Open करने हेतु किया जाता है। यदि फाइल मौजूद नहीं है, तो नई फाइल Create की जाती है। यदि फाइल पहले से मौजूद है, तो उसका पुराना डेटा Delete हो जाता है।
Syntax:
fp = fopen("abc.txt", "w+");Important Points:
Opens a file for reading and writing.
Creates a new file if it does not exist.
Deletes existing contents if the file already exists.
The initial file position is at the beginning.
फाइल को Reading और Writing दोनों के लिए Open करता है।
फाइल मौजूद न होने पर नई फाइल Create करता है।
फाइल पहले से मौजूद होने पर पुराना डेटा Delete कर देता है।
शुरुआत में File Position फाइल के प्रारंभ में होती है।
6. Read and Append Mode (a+)
The a+ mode is used to open a file for both reading and appending. If the file does not exist, a new file is created. Existing contents are preserved, and all writes are performed at the end of the file.
a+ Mode का उपयोग फाइल को Reading और Appending दोनों के लिए Open करने हेतु किया जाता है। यदि फाइल मौजूद नहीं है, तो नई फाइल Create की जाती है। पुराना डेटा सुरक्षित रहता है और सभी Write Operations फाइल के अंत में किए जाते हैं।
Syntax:
fp = fopen("abc.txt", "a+");Important Points:
Opens a file for reading and appending.
Creates a new file if it does not exist.
Preserves existing contents.
All writing takes place at the end of the file.
Reading can be performed using appropriate file positioning.
फाइल को Reading और Appending दोनों के लिए Open करता है।
फाइल मौजूद न होने पर नई फाइल Create करता है।
पुराने डेटा को सुरक्षित रखता है।
सभी Writing Operations फाइल के अंत में होते हैं।
उचित File Positioning के साथ Reading की जा सकती है।
Difference Between File Opening Modes
| Mode | File must exist? | Creates file? | Deletes old contents? | Permitted operations |
|---|---|---|---|---|
r | Yes | No | No | Read |
w | No | Yes, if needed | Yes, if it exists | Write |
a | No | Yes, if needed | No | Append |
r+ | Yes | No | No | Read and Write |
w+ | No | Yes, if needed | Yes, if it exists | Read and Write |
a+ | No | Yes, if needed | No | Read and Append |
Binary File Opening Modes
In C language, binary files are opened by adding the letter b to the file opening mode. Binary mode prevents text-specific translations performed by text streams.
C लैंग्वेज में Binary Files को Open करने के लिए File Opening Mode में b अक्षर जोड़ा जाता है। Binary Mode में Text Streams द्वारा किए जाने वाले Text-specific Translations नहीं होते हैं।
The commonly used binary modes are:
सामान्यतः उपयोग किए जाने वाले Binary Modes निम्नलिखित हैं:
| Mode | Description |
|---|---|
rb | Opens an existing binary file for reading. |
rb | पहले से मौजूद Binary File को पढ़ने के लिए Open करता है। |
wb | Opens a binary file for writing; creates or truncates it. |
wb | Binary File को लिखने के लिए Open करता है; नई बनाता है या पुराना डेटा हटाता है। |
ab | Opens a binary file for appending. |
ab | Binary File के अंत में डेटा जोड़ने के लिए Open करता है। |
rb+ | Opens an existing binary file for reading and writing. |
rb+ | पहले से मौजूद Binary File को Reading और Writing दोनों के लिए Open करता है। |
wb+ | Opens a binary file for reading and writing; creates or truncates it. |
wb+ | Binary File को Reading और Writing के लिए Open करता है; नई बनाता है या पुराना डेटा हटाता है। |
ab+ | Opens a binary file for reading and appending. |
ab+ | Binary File को Reading और Appending के लिए Open करता है। |
C Program to Demonstrate File Opening Modes
The following program demonstrates the use of w, a and r modes. First, it writes data into a file, then appends additional data and finally reads the complete contents.
निम्न प्रोग्राम में w, a और r Modes का उपयोग दर्शाया गया है। सबसे पहले फाइल में डेटा लिखा जाता है, फिर उसमें अतिरिक्त डेटा जोड़ा जाता है और अंत में पूरी फाइल को पढ़ा जाता है।
Program:
#include <stdio.h>int main(void){FILE *fp;int ch;/* Open file in write mode */fp = fopen("abc.txt", "w");if (fp == NULL){printf("Unable to create file.\n");return 1;}fprintf(fp, "Welcome to C Programming.\n");fclose(fp);/* Open file in append mode */fp = fopen("abc.txt", "a");if (fp == NULL){printf("Unable to open file.\n");return 1;}fprintf(fp, "File Handling in C Language.\n");fclose(fp);/* Open file in read mode */fp = fopen("abc.txt", "r");if (fp == NULL){printf("Unable to read file.\n");return 1;}printf("File Contents:\n");while ((ch = fgetc(fp)) != EOF){putchar(ch);}fclose(fp);return 0;}
Output:
File Contents:Welcome to C Programming.File Handling in C Language.
Explanation:
The file
abc.txtis opened inwmode, and the first line is written usingfprintf().abc.txtफाइल कोwMode में Open किया जाता है औरfprintf()की सहायता से पहली लाइन लिखी जाती है।The file is reopened in
amode, and another line is added at the end.फाइल को
aMode में दोबारा Open किया जाता है और अंत में एक दूसरी लाइन जोड़ी जाती है।The file is reopened in
rmode to read its contents.फाइल को उसके डेटा को पढ़ने के लिए
rMode में दोबारा Open किया जाता है।The
fgetc()function reads one character at a time untilEOFis reached.fgetc()Function एक बार में एक Character पढ़ता है और यह प्रक्रियाEOFमिलने तक चलती है।Finally,
fclose()closes the file.अंत में
fclose()फाइल को Close करता है।
Important Points About File Opening Modes
randr+require an existing file.wandw+create a file if it does not exist and truncate it if it does.aanda+preserve existing contents and write at the end.Adding
bopens a file in binary mode.Always check the return value of
fopen().Always close an opened file using
fclose().rऔरr+के लिए फाइल का पहले से मौजूद होना आवश्यक है।wऔरw+फाइल मौजूद न होने पर नई फाइल बनाते हैं और मौजूद होने पर पुराना डेटा हटा देते हैं।aऔरa+पुराने डेटा को सुरक्षित रखते हैं तथा नया डेटा फाइल के अंत में लिखते हैं।Mode में
bजोड़ने से फाइल Binary Mode में Open होती है।हमेशा
fopen()के Return Value को चेक करना चाहिए।Open की गई फाइल को
fclose()की सहायता से Close करना चाहिए।
When switching between input and output operations on an update stream (r+, w+ or a+), follow the C standard's positioning and flushing requirements. In particular, an output operation must not be followed directly by an input operation without an intervening fflush() or file-positioning operation.
Update Stream (r+, w+ या a+) पर Input और Output Operations के बीच Switch करते समय C Standard के Positioning और Flushing नियमों का पालन करना आवश्यक है। विशेष रूप से Output Operation के तुरंत बाद Input Operation करने से पहले fflush() या File Positioning Function का उपयोग करना चाहिए।
Comments
Post a Comment