Reading and Writing Characters in a File in C Using fgetc() and fputc()

Introduction to Character Input and Output Functions

In C language, the fgetc() and fputc() functions are used to read and write individual characters in a file. These functions are declared in the <stdio.h> header file.

C language में fgetc() और fputc() functions का उपयोग किसी file में एक-एक character को read और write करने के लिए किया जाता है। ये functions <stdio.h> header file में declared होते हैं।

1. fputc() Function in C

The fputc() function is used to write a single character into a file. It writes the specified character at the current file position.

fputc() function का उपयोग किसी file में एक character लिखने के लिए किया जाता है। यह दिए गए character को file की current position पर लिखता है।

Syntax

int fputc(int character, FILE *fp);

Parameters:

  • character: The character to be written into the file.

  • fp: A pointer to the file opened in a suitable writing mode.

Return Value:

The fputc() function returns the written character (as an unsigned char converted to int) on success. It returns EOF if a writing error occurs.

सफल होने पर fputc() लिखे गए character को return करता है। यदि character लिखते समय कोई error आती है, तो यह EOF return करता है।

Example: C Program to Write a Character into a File Using fputc()

#include <stdio.h>
int main(void)
{
FILE *fp;
fp = fopen("abc.txt", "w");
if (fp == NULL)
{
printf("Unable to open file.\n");
return 1;
}
if (fputc('A', fp) == EOF)
{
printf("Error writing character.\n");
fclose(fp);
return 1;
}
printf("Character written successfully.\n");
fclose(fp);
return 0;
}

Output:

Character written successfully.

File Content (abc.txt):

A

In this program, the file abc.txt is opened in write mode using fopen(). The fputc() function writes the character 'A' into the file. Finally, fclose() closes the file.

इस program में fopen() function की सहायता से abc.txt file को write mode में open किया जाता है। fputc() function file में 'A' character लिखता है। अंत में fclose() function file को close कर देता है।

2. fgetc() Function in C

The fgetc() function is used to read a single character from a file. It reads the character at the current file position and advances the position to the next character.

fgetc() function का उपयोग किसी file से एक character read करने के लिए किया जाता है। यह file की current position से character read करता है और अगली position पर चला जाता है।

Syntax

int fgetc(FILE *fp);

Parameter:

  • fp: A pointer to the file opened in a suitable reading mode.

Return Value:

The fgetc() function returns the character read from the file as an unsigned char converted to int. It returns EOF when the end of the file is reached or a reading error occurs.

fgetc() function file से पढ़े गए character को int के रूप में return करता है। जब file का अंत (End of File) आ जाता है या reading error होती है, तो यह EOF return करता है।

Example: C Program to Read a Character from a File Using fgetc()

#include <stdio.h>
int main(void)
{
FILE *fp;
int ch;
fp = fopen("abc.txt", "r");
if (fp == NULL)
{
printf("Unable to open file.\n");
return 1;
}
ch = fgetc(fp);
if (ch == EOF)
{
if (ferror(fp))
printf("Error reading file.\n");
else
printf("File is empty.\n");
}
else
{
printf("Character read from file: %c\n", ch);
}
fclose(fp);
return 0;
}

Assume the file abc.txt contains:

A

Output:

Character read from file: A

In this program, the file abc.txt is opened in read mode. The fgetc() function reads one character from the file and stores it in the integer variable ch. The character is displayed using %c.

इस program में abc.txt file को read mode में open किया जाता है। fgetc() function file से एक character read करके integer variable ch में store करता है। इसके बाद %c format specifier की सहायता से character को display किया जाता है।

3. C Program to Write and Read Characters Using fputc() and fgetc()

The following program demonstrates how to write multiple characters into a file using fputc() and read them back using fgetc().

निम्न program में fputc() function की सहायता से file में कई characters लिखे जाते हैं तथा fgetc() function की सहायता से उन्हें दोबारा read किया जाता है।

Program

#include <stdio.h>
int main(void)
{
FILE *fp;
char str[] = "C Programming";
int i, ch;
fp = fopen("abc.txt", "w");
if (fp == NULL)
{
printf("Unable to open file for writing.\n");
return 1;
}
for (i = 0; str[i] != '\0'; i++)
{
if (fputc(str[i], fp) == EOF)
{
printf("Error writing to file.\n");
fclose(fp);
return 1;
}
}
fclose(fp);
printf("Data written successfully.\n");
fp = fopen("abc.txt", "r");
if (fp == NULL)
{
printf("Unable to open file for reading.\n");
return 1;
}
printf("Data read from file: ");
while ((ch = fgetc(fp)) != EOF)
{
putchar(ch);
}
if (ferror(fp))
{
printf("\nError reading file.\n");
fclose(fp);
return 1;
}
fclose(fp);
return 0;
}

Output:

Data written successfully.
Data read from file: C Programming

In this program, the string "C Programming" is written into abc.txt one character at a time using a for loop and fputc(). The file is then reopened in read mode. The while loop reads characters using fgetc() until EOF is reached.

इस program में "C Programming" string को for loop और fputc() function की सहायता से एक-एक character करके abc.txt file में लिखा जाता है। इसके बाद file को read mode में दोबारा open किया जाता है। while loop में fgetc() function की सहायता से characters को read किया जाता है, जब तक EOF प्राप्त नहीं हो जाता।

Important Points About fgetc() and fputc()

  1. Both fgetc() and fputc() functions are declared in the <stdio.h> header file.

    fgetc() और fputc() दोनों functions <stdio.h> header file में declared होते हैं।

  2. fgetc() reads one character at a time from a file.

    fgetc() file से एक बार में एक character read करता है।

  3. fputc() writes one character at a time into a file.

    fputc() file में एक बार में एक character write करता है।

  4. The fgetc() function returns an int, not a char, so that it can represent all possible character values as well as EOF.

    fgetc() function int return करता है, char नहीं, ताकि यह सभी character values के साथ-साथ EOF को भी represent कर सके।

  5. Always check the return value of fputc() to detect writing errors.

    Writing errors का पता लगाने के लिए हमेशा fputc() के return value को check करना चाहिए।

  6. Use while ((ch = fgetc(fp)) != EOF) to read a file character by character. Do not use while (!feof(fp)).

    File को एक-एक character करके read करने के लिए while ((ch = fgetc(fp)) != EOF) का उपयोग करें। while (!feof(fp)) का उपयोग नहीं करना चाहिए।

  7. Always close an opened file using fclose() after completing file operations.

    File operations complete होने के बाद हमेशा fclose() function की सहायता से opened file को close करना चाहिए।

The fgetc() and fputc() functions are useful for character-by-character file handling in C. They can be used to read and write text files and are also helpful in understanding how file input and output operations work.

C language में fgetc() और fputc() functions character-by-character file handling के लिए उपयोगी हैं। इनकी सहायता से text files को read और write किया जा सकता है तथा file input-output operations को आसानी से समझा जा सकता है।

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