String I/O Functions in C

Introduction

C language में जब हम files के साथ काम करते हैं, तो कभी-कभी हमें पूरी string (line) को एक साथ read या write करना होता है।

इस काम के लिए String I/O Functions का उपयोग किया जाता है।

Character I/O में एक-एक character handle होता है
String I/O में पूरी line (string) handle होती है

String I/O Functions

FunctionUse
fputs()File में string लिखने के लिए
fgets()File से string पढ़ने के लिए

1. Writing String using fputs()

Syntax

fputs(string, file_pointer);

Example Program:

#include <stdio.h>

int main() {

FILE *fp;
char str[] = "Hello C File Handling";

fp = fopen("string.txt", "w");

if (fp == NULL) {
printf("Error opening file\n");
return 1;
}

fputs(str, fp);
fclose(fp);
printf("String written successfully\n");

return 0;
}

Program Explanation

  1. FILE *fp; → file pointer declare किया
  2. "w" mode में file open की गई
  3. fputs(str, fp);
    • पूरी string file में write कर दी
  4. fclose(fp); → file close

File Content (string.txt):

Hello C File Handling

Output:

String written successfully

2. Reading String using fgets()

Syntax

fgets(string, size, file_pointer);

Parameters

  • string → जहाँ data store होगा
  • size → maximum characters पढ़ने की limit
  • file_pointer → file pointer

Example Program:

#include <stdio.h>

int main() {

FILE *fp;
char str[50];

fp = fopen("string.txt", "r");

if (fp == NULL) {
printf("File not found\n");
return 1;
}

fgets(str, sizeof(str), fp);

printf("String read: %s\n", str);

fclose(fp);

return 0;
}

Program Explanation

  1. file "string.txt" read mode में open की गई
  2. fgets() का use करके string read की गई
fgets(str, sizeof(str), fp);

यह:

  • एक पूरी line read करता है
  • newline (\n) भी include कर सकता है
  1. string को print किया गया

Output:

String read: Hello C File Handling

3. Complete Program: Write + Read String

#include <stdio.h>

int main() {

FILE *fp;
char str[50];

// Write
fp = fopen("file.txt", "w");
fputs("Welcome to C Programming", fp);
fclose(fp);

// Read
fp = fopen("file.txt", "r");
fgets(str, sizeof(str), fp);

fclose(fp);

printf("Data from file: %s\n", str);

return 0;
}

Program Explanation

  1. fputs() से string file में लिखी गई
  2. file close की गई
  3. फिर file read mode में open की गई
  4. fgets() से string read की गई
  5. output print किया गया

Output:

Data from file: Welcome to C Programming

Important Points

  • fputs() → string write करता है
  • fgets() → string read करता है
  • fgets() size limit के साथ आता है (safe है)
  • newline (\n) handle करना जरूरी हो सकता है

Difference: fputs() vs fgets()

Featurefputs()fgets()
WorkWriteRead
Input TypeStringString
SafetySafeSafe (size limit)
NewlineAdd नहीं करताRead कर सकता है

Advantages

1. Efficient

Character I/O से fast है

2. Easy

पूरी line एक बार में handle

3. Safe

fgets() buffer overflow से बचाता है

Limitations

  • newline handling tricky हो सकता है
  • बहुत large data के लिए limit set करनी पड़ती है

Exam / Practical Tips

  • fputs() और fgets() बहुत important हैं
  • size parameter (sizeof) जरूर लिखें
  • write + read combined program practice करें

Conclusion

String I/O functions (fputs() और fgets()) file handling को आसान और efficient बनाते हैं।

इनकी मदद से हम पूरी string को आसानी से file में store और retrieve कर सकते हैं।
यह character I/O से ज्यादा practical और commonly used approach है।

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top