File Handling in C Language – Types of Files, FILE Pointer and File Operations (फाइल हैंडलिंग)
A file is used to store logically related data and information permanently in secondary storage devices such as hard disks, SSDs and USB drives. If a programmer wants to store records and program output permanently, file handling is used. In C language, file handling allows a programmer to create, open, read, write, append, close, rename and delete files.
फाइल का उपयोग तार्किक रूप से संबंधित डेटा एवं जानकारी को हार्ड डिस्क, SSD और USB ड्राइव जैसे सेकेंडरी स्टोरेज डिवाइस में स्थायी रूप से संग्रहित करने के लिए किया जाता है। यदि प्रोग्रामर किसी रिकॉर्ड या प्रोग्राम के आउटपुट को स्थायी रूप से सुरक्षित रखना चाहता है, तो File Handling का उपयोग किया जाता है। C लैंग्वेज में File Handling की सहायता से प्रोग्रामर फाइल को Create, Open, Read, Write, Append और Close कर सकता है। इसके अलावा फाइल का नाम बदलना और उसे Delete करना भी संभव है।
File Handling (फाइल हैंडलिंग)
The process of creating, accessing, reading, writing and manipulating data stored in files is called file handling.
फाइल में संग्रहित डेटा को Create, Access, Read, Write एवं Manipulate करने की प्रक्रिया फाइल हैंडलिंग कहलाती है।
Types of Data Files
Data files can be classified into two categories on the basis of their representation:
डेटा के Representation के आधार पर Data Files को दो प्रकारों में वर्गीकृत किया जा सकता है:
1. On the Basis of Data Representation
डेटा के Representation के आधार पर फाइल दो प्रकार की होती हैं:
A. Text File (टेक्स्ट फाइल)
A text file stores data as a sequence of characters organized into lines. Characters are represented using a character encoding such as ASCII or UTF-8. Text files are generally human-readable and can be opened using text editors.
Text File में डेटा को कैरेक्टर्स के क्रम के रूप में संग्रहित किया जाता है, जिन्हें सामान्यतः लाइनों में व्यवस्थित किया जाता है। कैरेक्टर्स को ASCII या UTF-8 जैसे Character Encoding के माध्यम से दर्शाया जाता है। Text Files को सामान्यतः Text Editor में खोलकर पढ़ा जा सकता है।
Example: abc.txt, student.txt, program.c
B. Binary File (बाइनरी फाइल)
A binary file stores data as a sequence of bytes. These bytes may represent numbers, characters, images, audio, video or other types of information. Binary files are generally interpreted by the program or application designed to read their particular format.
Binary File में डेटा को Bytes के क्रम के रूप में संग्रहित किया जाता है। ये Bytes संख्याओं, कैरेक्टर्स, इमेज, ऑडियो, वीडियो या अन्य प्रकार की जानकारी को दर्शा सकते हैं। Binary Files को सामान्यतः उसी प्रोग्राम या एप्लिकेशन द्वारा सही तरीके से समझा जाता है, जो उनके विशेष Format को पढ़ने के लिए बनाया गया हो।
Examples: abc.mp3, photo.jpeg, video.avi, data.dat
Difference Between Text File and Binary File
टेक्स्ट फाइल एवं बाइनरी फाइल में अंतर:
| Text File | Binary File |
|---|---|
| Data is represented as characters using a character encoding. | Data is stored as a sequence of bytes. |
| डेटा को Character Encoding के अनुसार कैरेक्टर्स के रूप में दर्शाया जाता है। | डेटा को Bytes के क्रम के रूप में संग्रहित किया जाता है। |
| Generally human-readable. | Generally requires a suitable program to interpret the data. |
| सामान्यतः इसे मनुष्य आसानी से पढ़ सकता है। | डेटा को समझने के लिए सामान्यतः उपयुक्त प्रोग्राम की आवश्यकता होती है। |
| Text mode may perform environment-dependent character translations. | Binary mode does not perform text-specific translations. |
| Text Mode में Environment के अनुसार कुछ Character Translations हो सकते हैं। | Binary Mode में Text-specific Translations नहीं होते हैं। |
Common examples are .txt and .c files. | Common examples include .jpg, .mp3 and .dat files. |
सामान्य उदाहरण .txt और .c फाइल हैं। | सामान्य उदाहरण .jpg, .mp3 और .dat फाइल हैं। |
केवल File Extension से यह निर्धारित नहीं होता कि फाइल Text है या Binary। फाइल का वास्तविक डेटा और एप्लिकेशन द्वारा उसे पढ़ने का तरीका उसका Format निर्धारित करता है।
2. On the Basis of Accessing Data
डेटा को Access करने के आधार पर फाइलों को निम्न दो प्रकारों में समझा जा सकता है:
A. Sequential Access File (क्रमिक एक्सेस फाइल)
In sequential access, data is accessed in sequence, generally from the beginning towards the end. To reach a particular record, earlier data may need to be processed first.
Sequential Access में डेटा को एक निश्चित क्रम में, सामान्यतः शुरुआत से अंत की ओर Access किया जाता है। किसी विशेष रिकॉर्ड तक पहुँचने के लिए उससे पहले के डेटा को पढ़ना पड़ सकता है।
Example: Reading a text file line by line.
उदाहरण: किसी Text File को एक-एक लाइन करके पढ़ना।
B. Random Access File (रैंडम एक्सेस फाइल)
In random access, a program can move to a particular position in a file and access data from that location without reading the entire file sequentially.
Random Access में प्रोग्राम फाइल की किसी विशेष Position पर जाकर वहाँ से डेटा Access कर सकता है। इसके लिए पूरी फाइल को क्रम से पढ़ना आवश्यक नहीं होता।
C provides file-positioning functions such as fseek(), ftell(), fgetpos() and fsetpos() for positioning and checking the position of a file stream.
C लैंग्वेज में फाइल की Position निर्धारित करने और जाँचने के लिए fseek(), ftell(), fgetpos() तथा fsetpos() जैसे Functions उपलब्ध हैं।
Sequential Access and Random Access describe methods of accessing data. Text and Binary describe how data is represented in a file. These are different classifications, not mutually exclusive file formats.
Sequential Access और Random Access डेटा को Access करने के तरीके बताते हैं, जबकि Text और Binary फाइल में डेटा के Representation को दर्शाते हैं। ये दोनों अलग-अलग आधार हैं और एक-दूसरे के विकल्प नहीं हैं।
FILE Pointer in C Language
In C language, FILE is a type defined by the standard input/output library in the <stdio.h> header file. A FILE pointer is used to refer to a file stream and manage file operations such as reading and writing.
C लैंग्वेज में FILE एक Type है, जिसे Standard Input/Output Library की <stdio.h> Header File में परिभाषित किया गया है। FILE Pointer का उपयोग File Stream को Refer करने तथा Reading और Writing जैसे File Operations को Manage करने के लिए किया जाता है।
Declaration of FILE Pointer
Syntax:
FILE *file_pointer;Example:
FILE *fp;Here, fp is a pointer to a FILE object. It is used to refer to a file stream.
यहाँ fp, FILE Object का Pointer है। इसका उपयोग File Stream को Refer करने के लिए किया जाता है।
Opening and Closing a File
The fopen() function is used to open a file, and the fclose() function is used to close an opened file.
fopen() Function का उपयोग फाइल को Open करने के लिए तथा fclose() Function का उपयोग Open की गई फाइल को Close करने के लिए किया जाता है।
Syntax:
FILE *fp;fp = fopen("abc.txt", "r");if (fp == NULL){printf("Unable to open file.\n");}else{/* Perform file operations */fclose(fp);}
In this example, abc.txt is opened in read mode. If the file cannot be opened, fopen() returns NULL.
इस उदाहरण में abc.txt फाइल को Read Mode में Open किया गया है। यदि फाइल Open नहीं हो पाती है, तो fopen() NULL Return करता है।
File Opening Modes in C
The fopen() function accepts two arguments: the file name and the opening mode.
fopen() Function में दो Arguments दिए जाते हैं: फाइल का नाम और फाइल को Open करने का Mode।
| Mode | Description |
|---|---|
r | Opens an existing text file for reading. |
r | पहले से मौजूद Text File को पढ़ने के लिए Open करता है। |
w | Opens a file for writing. Creates it if necessary and truncates an existing file. |
w | फाइल को लिखने के लिए Open करता है। फाइल मौजूद न होने पर बनाता है और मौजूद होने पर उसका पुराना डेटा हटाता है। |
a | Opens a file for appending. Creates it if it does not exist. |
a | फाइल में नया डेटा जोड़ने के लिए Open करता है। फाइल मौजूद न होने पर नई फाइल बनाता है। |
r+ | Opens an existing file for both reading and writing. |
r+ | पहले से मौजूद फाइल को पढ़ने और लिखने दोनों के लिए Open करता है। |
w+ | Opens a file for reading and writing. Creates it if necessary and truncates an existing file. |
w+ | फाइल को पढ़ने और लिखने दोनों के लिए Open करता है। फाइल मौजूद न होने पर बनाता है और पुराना डेटा हटा देता है। |
a+ | Opens a file for reading and appending. Creates it if necessary. |
a+ | फाइल को पढ़ने और नया डेटा जोड़ने के लिए Open करता है। फाइल मौजूद न होने पर बनाता है। |
For binary files, b is added to the mode, such as rb, wb, ab, rb+, wb+ and ab+.
Binary Files के लिए Mode में b जोड़ा जाता है, जैसे rb, wb, ab, rb+, wb+ और ab+।
Operations Performed on a File
The following operations can be performed on files in C language:
C लैंग्वेज में फाइल पर निम्नलिखित Operations किए जा सकते हैं:
Create a File: Creating a new file to store data.
फाइल बनाना: डेटा संग्रहित करने के लिए नई फाइल बनाना।
Open a File: Opening an existing file or creating a file using
fopen().फाइल Open करना:
fopen()की सहायता से पहले से मौजूद फाइल को Open करना या नई फाइल बनाना।Read Operation: Reading data from a file using functions such as
fgetc(),fgets(),fscanf()andfread().Read Operation:
fgetc(),fgets(),fscanf()तथाfread()जैसे Functions की सहायता से फाइल से डेटा पढ़ना।Write Operation: Writing data into a file using functions such as
fputc(),fputs(),fprintf()andfwrite().Write Operation:
fputc(),fputs(),fprintf()तथाfwrite()जैसे Functions की सहायता से फाइल में डेटा लिखना।Append Operation: Adding new data at the end of an existing file using append mode.
Append Operation: Append Mode का उपयोग करके पहले से मौजूद फाइल के अंत में नया डेटा जोड़ना।
Close a File: Closing an opened file using
fclose().फाइल Close करना:
fclose()Function की सहायता से Open की गई फाइल को बंद करना।Rename a File: Changing the name of a file using
rename().फाइल का नाम बदलना:
rename()Function की सहायता से फाइल का नाम बदलना।Delete a File: Removing a file using
remove().फाइल Delete करना:
remove()Function की सहायता से फाइल को हटाना।Copy a File: Reading data from one file and writing it into another file.
फाइल Copy करना: एक फाइल से डेटा पढ़कर दूसरी फाइल में लिखना।
Important File Handling Functions in C
Some commonly used file handling functions are:
C लैंग्वेज में उपयोग किए जाने वाले कुछ महत्वपूर्ण File Handling Functions निम्नलिखित हैं:
| Function | Purpose |
|---|---|
fopen() | Opens a file. |
fclose() | Closes a file. |
fgetc() | Reads one character. |
fputc() | Writes one character. |
fgets() | Reads a string or line. |
fputs() | Writes a string. |
fscanf() | Reads formatted data. |
fprintf() | Writes formatted data. |
fread() | Reads blocks of binary data. |
fwrite() | Writes blocks of binary data. |
fseek() | Changes file position. |
ftell() | Returns the current file position. |
rewind() | Moves the file position to the beginning. |
feof() | Tests the end-of-file indicator. |
ferror() | Tests the error indicator. |
rename() | Renames a file. |
remove() | Deletes a file. |
File handling is an important feature of C programming because it allows data to be stored permanently and accessed whenever required. The FILE pointer and standard library functions make it possible to perform different operations on text and binary files.
File Handling, C Programming का एक महत्वपूर्ण भाग है, क्योंकि इसकी सहायता से डेटा को स्थायी रूप से संग्रहित किया जा सकता है और आवश्यकता पड़ने पर Access किया जा सकता है। FILE Pointer और Standard Library Functions की सहायता से Text तथा Binary Files पर विभिन्न Operations किए जा सकते हैं।
Comments
Post a Comment