Random Access File Handling in C Using fseek(), ftell() and rewind() with Examples
Random Access File Handling in C Language
In C language, files can be accessed sequentially or randomly. In Sequential File Access, data is read or written one after another from the beginning to the end of the file. In Random Access File Handling, we can directly move the file position indicator to a particular location and read or write data from that position.
सी लैंग्वेज में Files को Sequential या Random तरीके से Access किया जा सकता है। Sequential File Access में डेटा को फाइल के प्रारंभ से अंत तक एक के बाद एक Read या Write किया जाता है। Random Access File Handling में हम File Position Indicator को सीधे किसी विशेष स्थान पर ले जाकर वहां से डेटा Read या Write कर सकते हैं।
C language provides three important functions for random access file handling:
fseek()ftell()rewind()
ये तीनों Functions File Position Indicator को नियंत्रित करने में सहायता करते हैं।
1. fseek() Function in C
The fseek() function is used to move the file position indicator to a specified location in a file. It allows us to read or write data from a particular position instead of starting from the beginning.
fseek() Function का उपयोग File Position Indicator को फाइल में किसी निर्धारित स्थान पर ले जाने के लिए किया जाता है। इसकी सहायता से हम फाइल के प्रारंभ से पढ़ने के बजाय किसी विशेष Position से डेटा Read या Write कर सकते हैं।
Syntax:
fseek(file_pointer, offset, position);Parameters:
file_pointer– Pointer to the opened file.offset– Number of bytes to move relative to the specified position.position– Reference position from which the offset is calculated.
The third parameter can have the following values:
| Constant | Meaning |
|---|---|
SEEK_SET | Beginning of the file |
SEEK_CUR | Current file position |
SEEK_END | End of the file |
Example:
fseek(fp, 5, SEEK_SET);This statement moves the file position indicator to the position 5 bytes from the beginning of the file.
यह Statement File Position Indicator को फाइल के प्रारंभ से 5 Bytes आगे ले जाता है।
Return Value:
The fseek() function returns 0 on successful execution and a non-zero value if an error occurs.
सफलतापूर्वक Execute होने पर fseek() Function 0 Return करता है और Error होने पर Non-Zero Value Return करता है।
2. ftell() Function in C
The ftell() function is used to determine the current position of the file position indicator. It returns the position as a value of type long for a successfully positioned binary stream.
ftell() Function का उपयोग File Position Indicator की वर्तमान Position पता करने के लिए किया जाता है। Binary Stream में यह Position को long Value के रूप में Return करता है।
Syntax:
ftell(file_pointer);Example:
long int pos;pos = ftell(fp);
This statement stores the current file position in the variable pos.
यह Statement File की वर्तमान Position को pos Variable में Store करता है।
Return Value:
The ftell() function returns the current file position on success and -1L if an error occurs.
सफल होने पर ftell() Function वर्तमान File Position Return करता है तथा Error होने पर -1L Return करता है।
3. rewind() Function in C
The rewind() function is used to move the file position indicator back to the beginning of the file. It also clears the end-of-file indicator and the error indicator for the stream.
rewind() Function का उपयोग File Position Indicator को वापस फाइल के प्रारंभ में ले जाने के लिए किया जाता है। यह Stream के End-of-File Indicator और Error Indicator को भी Clear करता है।
Syntax:
rewind(file_pointer);Example:
rewind(fp);This statement moves the file position indicator to the beginning of the file.
यह Statement File Position Indicator को फाइल के प्रारंभ में ले जाता है।
Return Value:
The rewind() function does not return any value.
rewind() Function कोई Value Return नहीं करता है।
4. C Program to Demonstrate fseek(), ftell() and rewind()
The following program creates a binary file, writes a string into it, and demonstrates the use of fseek(), ftell() and rewind() functions.
निम्नलिखित प्रोग्राम एक Binary File बनाता है और उसमें एक String लिखता है। इसके बाद fseek(), ftell() और rewind() Functions के उपयोग को प्रदर्शित करता है।
Program:
#include <stdio.h>#include <string.h>int main(void){FILE *fp;char text[] = "C Programming";char ch;long int pos;/* Open binary file in write mode */fp = fopen("data.dat", "wb");if (fp == NULL){printf("Unable to create file.\n");return 1;}/* Write string into file */if (fwrite(text, sizeof(char),strlen(text), fp) != strlen(text)){printf("Error writing file.\n");fclose(fp);return 1;}fclose(fp);/* Open binary file in read mode */fp = fopen("data.dat", "rb");if (fp == NULL){printf("Unable to open file.\n");return 1;}/* Move to beginning and display current position */rewind(fp);pos = ftell(fp);if (pos == -1L){printf("Error getting file position.\n");fclose(fp);return 1;}printf("Initial position = %ld\n", pos);/* Move file pointer to position 5 */if (fseek(fp, 5L, SEEK_SET) != 0){printf("Error moving file position.\n");fclose(fp);return 1;}pos = ftell(fp);if (pos == -1L){printf("Error getting file position.\n");fclose(fp);return 1;}printf("Position after fseek = %ld\n", pos);/* Read character from current position */ch = fgetc(fp);if (ch == EOF){printf("Unable to read character.\n");fclose(fp);return 1;}printf("Character at position 5 = %c\n", ch);/* Move file pointer to beginning */rewind(fp);ch = fgetc(fp);if (ch == EOF){printf("Unable to read character.\n");fclose(fp);return 1;}printf("Character after rewind = %c\n", ch);fclose(fp);return 0;}
Output
Initial position = 0Position after fseek = 5Character at position 5 = oCharacter after rewind = C
Explanation
fopen("data.dat", "wb")creates a binary file in write mode. यह एक Binary File बनाता है जिसमें डेटा Write किया जाएगा।fwrite()writes the string into the file. यह String को Binary File में Store करता है।fopen("data.dat", "rb")opens the file in binary read mode. यह Binary File को Read करने के लिए खोलता है।rewind(fp)moves the file position indicator to the beginning of the file. यह File Position Indicator को प्रारंभ में ले जाता है।ftell(fp)returns the current file position. यह File Position Indicator की वर्तमान Position बताता है।fseek(fp, 5L, SEEK_SET)moves the file position indicator five bytes from the beginning of the file. यह File Position Indicator को प्रारंभ से 5 Bytes आगे ले जाता है।fgetc(fp)reads the character at the current file position. यह वर्तमान Position से एक Character Read करता है।fclose(fp)closes the opened file. यह File को बंद करता है।
5. Difference Between fseek(), ftell() and rewind()
| Function | Purpose |
|---|---|
fseek() | Moves the file position indicator to a specified position. |
ftell() | Returns the current file position. |
rewind() | Moves the file position indicator to the beginning. |
fseek() का उपयोग File Position बदलने के लिए, ftell() का उपयोग Current Position जानने के लिए तथा rewind() का उपयोग File Position को प्रारंभ में लाने के लिए किया जाता है।
6. Important Points About Random Access File Handling
fseek()is commonly used withSEEK_SET,SEEK_CURandSEEK_END. ये Constants File Position Indicator की Reference Position निर्धारित करते हैं।ftell()returns-1Lwhen an error occurs. Error Checking करना आवश्यक है।rewind()moves the file position indicator to the beginning and clears the stream's error and end-of-file indicators. यह केवल Position नहीं बदलता, बल्कि Error और EOF Indicators को भी Clear करता है।Random access is particularly useful when working with records in binary files. Binary File में किसी विशेष Record को सीधे Access करने के लिए इसका उपयोग किया जा सकता है।
For binary streams,
fseek()supports byte-based positioning. Binary File में किसी विशेष Byte Position पर जाने के लिए इसका उपयोग किया जाता है।For text streams, arbitrary byte offsets are not generally portable. Text File में किसी भी मनमाने Byte Offset पर जाने की सुविधा सभी Systems पर समान रूप से काम नहीं करती।
Always check the return value of
fseek()andftell()to detect errors. File Position से संबंधित Functions का उपयोग करते समय Error Checking अवश्य करें।
The example uses a binary file so that byte-based positioning is straightforward. The character at offset 5 is o because file positions start from zero.
यह उदाहरण Binary File का उपयोग करता है ताकि Byte-Based Positioning को आसानी से समझा जा सके। Offset 5 पर o Character मिलता है क्योंकि File Position की गणना Zero से शुरू होती है।
Comments
Post a Comment