Opening and Closing a File in C Using fopen() and fclose()

In C language, file handling is used to store data permanently in secondary storage. Before performing any read or write operation, a file must be opened. After completing the required operations, the file should be closed. C language provides two important functions for opening and closing files: fopen() and fclose(). These functions are declared in the <stdio.h> header file.

C लैंग्वेज में File Handling का उपयोग डेटा को सेकेंडरी स्टोरेज में स्थायी रूप से संग्रहित करने के लिए किया जाता है। किसी भी Read या Write Operation को करने से पहले फाइल को Open करना आवश्यक है। सभी आवश्यक Operations पूरे होने के बाद फाइल को Close कर देना चाहिए। C लैंग्वेज में फाइल को Open और Close करने के लिए दो महत्वपूर्ण Functions उपलब्ध हैं: fopen() और fclose()। ये Functions <stdio.h> Header File में उपलब्ध हैं।

fopen() Function:

The fopen() function is used to open an existing file or create a new file according to the specified opening mode. It 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.

fopen() Function का उपयोग किसी मौजूदा फाइल को Open करने या दिए गए Opening Mode के अनुसार नई फाइल बनाने के लिए किया जाता है। यह दो Arguments ग्रहण करता है: फाइल का नाम और Opening Mode। फाइल सफलतापूर्वक Open होने पर यह FILE Pointer Return करता है, अन्यथा NULL Return करता है।

Syntax:

FILE *fopen(const char *filename, const char *mode);

Example:

FILE *fp;
fp = fopen("xyz.txt", "w");

In this example, xyz.txt is opened in write mode. If the file does not exist, it is created. If it already exists, its previous contents are removed. Always check whether fopen() returns NULL before performing file operations.

इस उदाहरण में xyz.txt को Write Mode में Open किया गया है। यदि फाइल मौजूद नहीं है, तो नई फाइल बनाई जाती है। यदि फाइल पहले से मौजूद है, तो उसका पुराना डेटा हट जाता है। File Operations करने से पहले हमेशा चेक करें कि fopen() ने NULL तो Return नहीं किया है।

FILE *fp;
fp = fopen("xyz.txt", "r");
if (fp == NULL)
{
printf("Unable to open file.\n");
}

fclose() Function:

The fclose() function is used to close an opened file. It flushes buffered output associated with the stream and releases the resources associated with the file stream. It returns 0 on successful closing and EOF if an error occurs.

fclose() Function का उपयोग Open की गई फाइल को Close करने के लिए किया जाता है। यह फाइल से संबंधित Buffered Output को Flush करता है तथा File Stream से संबंधित Resources को Release करता है। सफलतापूर्वक फाइल Close होने पर यह 0 Return करता है और Error होने पर EOF Return करता है।

Syntax:

int fclose(FILE *fp);

Example:

FILE *fp;
fp = fopen("xyz.txt", "w");
if (fp != NULL)
{
/* Perform file operations */
fclose(fp);
}

In this example, the file is opened using fopen() and closed using fclose() after completing the file operations.

इस उदाहरण में fopen() की सहायता से फाइल Open की जाती है और File Operations पूरे होने के बाद fclose() की सहायता से फाइल Close कर दी जाती है।

Example: C Program to Demonstrate fopen() and fclose()

The following C program demonstrates how to open a file, write a character into it, close the file, reopen it in read mode, read the stored character and close the file again.

निम्न C प्रोग्राम में फाइल को Open करना, उसमें एक Character लिखना, फाइल को Close करना, उसे Read Mode में दोबारा Open करना, संग्रहित Character को पढ़ना तथा फाइल को फिर से Close करना दर्शाया गया है।

Program:

#include <stdio.h>
int main(void)
{
FILE *fp;
char ch = 'J';
int ch1;
/* Open file in write mode */
fp = fopen("abc.txt", "w");
if (fp == NULL)
{
printf("Unable to create file.\n");
return 1;
}
/* Write a character into file */
if (fputc(ch, fp) == EOF)
{
printf("Error writing to file.\n");
fclose(fp);
return 1;
}
printf("%c is written into the file.\n", ch);
/* Close the file */
if (fclose(fp) == EOF)
{
printf("Error closing file.\n");
return 1;
}
/* Open file in read mode */
fp = fopen("abc.txt", "r");
if (fp == NULL)
{
printf("Unable to open file.\n");
return 1;
}
/* Read a character from file */
ch1 = fgetc(fp);
if (ch1 == EOF)
{
printf("Unable to read character.\n");
fclose(fp);
return 1;
}
printf("%c is read from the file.\n", ch1);
/* Close the file */
fclose(fp);
return 0;
}

Output:

J is written into the file.
J is read from the file.

Explanation:

  1. FILE *fp; declares a file pointer used to refer to the file stream.

    FILE *fp; एक File Pointer Declare करता है, जिसका उपयोग File Stream को Refer करने के लिए किया जाता है।

  2. fopen("abc.txt", "w") opens the file in write mode. If the file does not exist, it creates a new file.

    fopen("abc.txt", "w") फाइल को Write Mode में Open करता है। यदि फाइल मौजूद नहीं है, तो नई फाइल बनाई जाती है।

  3. fputc(ch, fp) writes the character J into the file.

    fputc(ch, fp) फाइल में J Character लिखता है।

  4. fclose(fp) closes the file after writing the character.

    fclose(fp) Character लिखने के बाद फाइल को Close करता है।

  5. fopen("abc.txt", "r") reopens the same file in read mode.

    fopen("abc.txt", "r") उसी फाइल को Read Mode में दोबारा Open करता है।

  6. fgetc(fp) reads the character stored in the file.

    fgetc(fp) फाइल में संग्रहित Character को पढ़ता है।

  7. Finally, fclose(fp) closes the file after reading.

    अंत में fclose(fp) पढ़ने के बाद फाइल को Close करता है।

Important Points

  • fopen() is used to open or create a file.

  • fclose() is used to close an opened file.

  • Both functions are declared in <stdio.h>.

  • fopen() returns a FILE pointer on success and NULL on failure.

  • fclose() returns 0 on success and EOF on failure.

  • Always check the return value of fopen() before using the file pointer.

  • Close every successfully opened file when it is no longer needed.

  • fopen() का उपयोग फाइल को Open या Create करने के लिए किया जाता है।

  • fclose() का उपयोग Open की गई फाइल को Close करने के लिए किया जाता है।

  • दोनों Functions <stdio.h> Header File में उपलब्ध हैं।

  • fopen() सफल होने पर FILE Pointer और असफल होने पर NULL Return करता है।

  • fclose() सफल होने पर 0 और असफल होने पर EOF Return करता है।

  • File Pointer का उपयोग करने से पहले fopen() का Return Value अवश्य चेक करना चाहिए।

  • सफलतापूर्वक Open की गई फाइल का उपयोग पूरा होने पर उसे Close करना चाहिए।

The file abc.txt is created in the program's current working directory, which may be different from the folder containing the source code.

abc.txt फाइल प्रोग्राम की Current Working Directory में बनाई जाती है। यह Directory उस Folder से अलग भी हो सकती है जिसमें C Source Code मौजूद है।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025