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
| Function | Use |
|---|---|
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
FILE *fp;→ file pointer declare किया"w"mode में file open की गईfputs(str, fp);- पूरी string file में write कर दी
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 पढ़ने की limitfile_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
- file
"string.txt"read mode में open की गई fgets()का use करके string read की गई
fgets(str, sizeof(str), fp);
यह:
- एक पूरी line read करता है
- newline (
\n) भी include कर सकता है
- 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
fputs()से string file में लिखी गई- file close की गई
- फिर file read mode में open की गई
fgets()से string read की गई- 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()
| Feature | fputs() | fgets() |
|---|---|---|
| Work | Write | Read |
| Input Type | String | String |
| Safety | Safe | Safe (size limit) |
| Newline | Add नहीं करता | 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 है।