Reading and Writing Strings in a File in C Using fgets() and fputs()
Introduction to String Input and Output Functions
In C language, the fgets() and fputs() functions are used to read and write strings in a file. These functions are declared in the <stdio.h> header file. Unlike fgetc() and fputc(), which handle one character at a time, these functions work with strings.
C language में fgets() और fputs() functions का उपयोग किसी file में strings को read और write करने के लिए किया जाता है। ये functions <stdio.h> header file में declared होते हैं। fgetc() और fputc() एक बार में एक character पर कार्य करते हैं, जबकि ये functions strings के साथ कार्य करते हैं।
1. fputs() Function in C
The fputs() function is used to write a string into a file. It writes the characters of the given string up to, but not including, the terminating null character ('\0').
fputs() function का उपयोग किसी file में string लिखने के लिए किया जाता है। यह दी गई string के सभी characters को write करता है, लेकिन terminating null character ('\0') को file में write नहीं करता।
Syntax
int fputs(const char *str, FILE *fp);Parameters:
str: The string to be written into the file.fp: A pointer to the file opened in a suitable writing mode.
Return Value:
The fputs() function returns a non-negative value on success and EOF if a writing error occurs.
सफल होने पर fputs() function एक non-negative value return करता है। यदि writing के दौरान कोई error आती है, तो यह EOF return करता है।
Example: C Program to Write a String into a File Using fputs()
#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 (fputs("Welcome to C Programming.", fp) == EOF){printf("Error writing to file.\n");fclose(fp);return 1;}printf("String written successfully.\n");fclose(fp);return 0;}
Output:
String written successfully.File Content (abc.txt):
Welcome to C Programming.In this program, the file abc.txt is opened in write mode using fopen(). The fputs() function writes the string "Welcome to C Programming." into the file. Finally, fclose() closes the file.
इस program में fopen() function की सहायता से abc.txt file को write mode में open किया जाता है। fputs() function "Welcome to C Programming." string को file में write करता है। अंत में fclose() function file को close कर देता है।
2. fgets() Function in C
The fgets() function is used to read a string or a line of text from a file. It reads at most n-1 characters, where n is the specified buffer size. It stops when a newline character is read, the specified limit is reached, or the end of the file is encountered.
fgets() function का उपयोग किसी file से string या text की एक line read करने के लिए किया जाता है। यह अधिकतम n-1 characters read करता है, जहाँ n buffer का size है। यह newline character मिलने, निर्धारित सीमा पूरी होने या file के अंत तक पहुँचने पर reading रोक देता है।
Syntax
char *fgets(char *str, int n, FILE *fp);Parameters:
str: Character array in which the read string is stored.n: Maximum buffer size, including space for the terminating null character.fp: A pointer to the file opened in reading mode.
Return Value:
The fgets() function returns the pointer to the string on success. It returns NULL if an error occurs or if the end of the file is reached before any character is read.
सफल होने पर fgets() function string के pointer को return करता है। यदि कोई error आती है या कोई character read होने से पहले ही file का अंत आ जाता है, तो यह NULL return करता है।
Important Points About fgets()
fgets()reads at mostn-1characters.fgets()अधिकतमn-1characters read करता है।It adds a terminating null character (
'\0') at the end of the stored string.यह stored string के अंत में terminating null character (
'\0') जोड़ता है।If a newline character is read, it is stored in the string.
यदि newline character read होता है, तो वह भी string में store होता है।
It is a safer alternative to the old
gets()function because the buffer size is specified.यह पुराने
gets()function का safer alternative है, क्योंकि इसमें buffer का size निर्धारित किया जाता है।
Example: C Program to Read a String from a File Using fgets()
#include <stdio.h>int main(void){FILE *fp;char str[100];fp = fopen("abc.txt", "r");if (fp == NULL){printf("Unable to open file.\n");return 1;}if (fgets(str, sizeof(str), fp) != NULL){printf("String read from file:\n%s", str);}else{if (ferror(fp))printf("Error reading file.\n");elseprintf("File is empty.\n");}fclose(fp);return 0;}
Assume the file abc.txt contains:
Welcome to C Programming.Output:
String read from file:Welcome to C Programming.
In this program, the file abc.txt is opened in read mode. The fgets() function reads a line from the file and stores it in the character array str. The sizeof(str) expression provides the size of the array, helping prevent reading beyond its capacity.
इस program में abc.txt file को read mode में open किया जाता है। fgets() function file से एक line read करके character array str में store करता है। sizeof(str) expression array का size देता है, जिससे उसकी capacity से अधिक data read होने से बचता है।
3. C Program to Write and Read Multiple Lines Using fputs() and fgets()
The following program demonstrates how to write multiple lines into a file using fputs() and read them back line by line using fgets().
निम्न program में fputs() function की सहायता से file में कई lines लिखी जाती हैं तथा fgets() function की सहायता से उन्हें एक-एक line करके read किया जाता है।
Program
#include <stdio.h>int main(void){FILE *fp;char str[100];fp = fopen("abc.txt", "w");if (fp == NULL){printf("Unable to open file for writing.\n");return 1;}if (fputs("Welcome to C Programming.\n", fp) == EOF ||fputs("File Handling in C Language.\n", fp) == EOF ||fputs("Learn C with Examples.\n", 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("\nData read from file:\n");while (fgets(str, sizeof(str), fp) != NULL){printf("%s", str);}if (ferror(fp)){printf("\nError reading file.\n");fclose(fp);return 1;}fclose(fp);return 0;}
Output:
Data written successfully.Data read from file:Welcome to C Programming.File Handling in C Language.Learn C with Examples.
In this program, three lines of text are written into abc.txt using the fputs() function. Each string contains a newline character ('\n') to place the next string on a new line. The file is then reopened in read mode, and the while loop uses fgets() to read and display each line until the end of the file.
इस program में fputs() function की सहायता से abc.txt file में text की तीन lines लिखी जाती हैं। प्रत्येक string में newline character ('\n') दिया गया है, जिससे अगली string नई line में लिखी जाती है। इसके बाद file को read mode में दोबारा open किया जाता है और while loop में fgets() function की सहायता से file की प्रत्येक line को read करके display किया जाता है।
Difference Between fgets() and fputs()
| fgets() | fputs() |
|---|---|
| Used to read a string from a file. | Used to write a string into a file. |
Reads at most n-1 characters. | Writes characters until '\0'. |
| Requires a character array, its size and a file pointer. | Requires a string and a file pointer. |
Returns a pointer to the array on success, or NULL on failure. | Returns a non-negative value on success, or EOF on failure. |
fgets() | fputs() |
|---|---|
| File से string read करने के लिए उपयोग होता है। | File में string write करने के लिए उपयोग होता है। |
अधिकतम n-1 characters read करता है। | '\0' तक के characters write करता है। |
| Character array, उसका size और file pointer आवश्यक हैं। | String और file pointer आवश्यक हैं। |
सफल होने पर array का pointer और असफल होने पर NULL return करता है। | सफल होने पर non-negative value और असफल होने पर EOF return करता है। |
Important Points About fgets() and fputs()
Both functions are declared in the
<stdio.h>header file.दोनों functions
<stdio.h>header file में declared होते हैं।fgets()can read spaces within a line, unlike a typical%sconversion withscanf().fgets()एक line में spaces सहित text read कर सकता है, जबकिscanf()में सामान्य%sconversion whitespace पर रुक जाता है।fputs()does not automatically add a newline character at the end of a string.fputs()string के अंत में अपने आप newline character नहीं जोड़ता है।fgets()may retain the newline character ('\n') if it reads one before reaching its character limit.यदि
fgets()अपनी character limit पूरी होने से पहले newline character ('\n') read करता है, तो वह string में store हो सकता है।Always check the return value of these functions to handle file errors properly.
File errors को सही तरीके से handle करने के लिए हमेशा इन functions के return value को check करना चाहिए।
Always close the opened file using
fclose()after completing file operations.File operations complete होने के बाद हमेशा
fclose()function की सहायता से opened file को close करना चाहिए।
The fgets() and fputs() functions are useful for reading and writing strings in C file handling. They make it easier to work with text files containing words, sentences and multiple lines.
C language में fgets() और fputs() functions file handling के अंतर्गत strings को read और write करने के लिए उपयोगी हैं। इनकी सहायता से words, sentences और multiple lines वाली text files पर आसानी से कार्य किया जा सकता है।
Comments
Post a Comment