File handling in C allows you to store and retrieve data from files. It is useful for saving program output, logs, and user data across program executions.
C में file handling का उपयोग डेटा को files में store और retrieve करने के लिए किया जाता है। यह user data या output को save रखने के लिए उपयोगी होता है।
Why Use Files in C?
In C programming, when you write output using printf()
or read input using scanf()
, that data only exists while the program is running. Once it ends, the data is lost.
Files allow data to be stored **permanently on disk**, so that the information can be reused later even after the program ends. This is very useful for storing:
C में File का उपयोग क्यों?
जब आप printf()
या scanf()
का उपयोग करते हैं, तो वो data केवल program के चलने तक memory में रहता है। Program बंद होते ही वह data खो जाता है।
Files का उपयोग करके हम data को permanently disk पर store कर सकते हैं ताकि उसे बाद में भी इस्तेमाल किया जा सके। इसका उपयोग अक्सर इन चीज़ों के लिए किया जाता है:
fopen()
- Opens a file in a specified mode (r, w, a, etc.).fclose()
- Closes the opened file.fprintf()
- Writes formatted output to a file.fscanf()
- Reads formatted input from a file.fgets()
- Reads a line or string from a file.fputs()
- Writes a string to a file.fgetc()
- Reads a single character from a file.fputc()
- Writes a single character to a file.feof()
- Checks if the end of a file is reached.fseek()
- Moves the file pointer to a specific position.ftell()
- Returns the current position of the file pointer.rewind()
- Moves the file pointer to the beginning.fread()
- Reads binary data from a file.fwrite()
- Writes binary data to a file.remove()
- Deletes a file.rename()
- Renames a file.perror()
- Prints an error message describing the last file error.#include
int main() {
FILE *fp = fopen("example.txt", "w");
if(fp) {
printf("File created successfully");
fclose(fp);
} else {
printf("File creation failed");
}
return 0;
}
Demonstrates: Create a File
fopen() का उपयोग करके नई फाइल बनाएं
#include
int main() {
FILE *fp = fopen("example.txt", "w");
if(fp) {
fprintf(fp, "Hello File Handling!\n");
fclose(fp);
}
return 0;
}
Demonstrates: Write to File
fprintf() से फाइल में लिखना
#include
int main() {
char buffer[100];
FILE *fp = fopen("example.txt", "r");
if(fp) {
while(fgets(buffer, 100, fp))
printf("%s", buffer);
fclose(fp);
}
return 0;
}
Demonstrates: Read from File
fgets() का उपयोग करके file पढ़ना
#include
int main() {
FILE *fp = fopen("example.txt", "a");
if(fp) {
fprintf(fp, "Appended Line\n");
fclose(fp);
}
return 0;
}
Demonstrates: Append to File
फाइल में डेटा जोड़ना (append)
#include
int main() {
FILE *fp = fopen("example.txt", "r");
char c;
if(fp) {
while((c = fgetc(fp)) != EOF)
putchar(c);
fclose(fp);
}
return 0;
}
Demonstrates: Read Char by Char
fgetc() से character पढ़ना
#include
int main() {
FILE *fp = fopen("char.txt", "w");
if(fp) {
fputc('A', fp);
fclose(fp);
}
return 0;
}
Demonstrates: Write Char by Char
fputc() से character लिखना
#include
int main() {
FILE *fp = fopen("example.txt", "r");
char c;
while(!feof(fp)) {
c = fgetc(fp);
putchar(c);
}
fclose(fp);
return 0;
}
Demonstrates: Check EOF
feof() से फाइल खत्म होने की जांच
#include
int main() {
FILE *fp = fopen("example.txt", "r+");
if(fp) {
fprintf(fp, "Modified Line\n");
rewind(fp);
char c;
while((c = fgetc(fp)) != EOF)
putchar(c);
fclose(fp);
}
return 0;
}
Demonstrates: Read & Write Both
r+ मोड से पढ़ना और लिखना
#include
int main() {
FILE *fp = fopen("nofile.txt", "r");
if(fp == NULL)
printf("File does not exist");
else
fclose(fp);
return 0;
}
Demonstrates: File Not Found
File न मिलने पर NULL चेक करें
#include
int main() {
FILE *fp = fopen("example.txt", "r");
char ch; int count = 0;
while((ch = fgetc(fp)) != EOF) {
if(ch == '\n') count++;
}
printf("Total lines = %d", count);
fclose(fp);
return 0;
}
Demonstrates: Count Lines
फाइल में कितनी lines हैं गिनना
#include
int main() {
FILE *fp = fopen("example.txt", "r");
fseek(fp, 0, SEEK_END);
long size = ftell(fp);
printf("Size = %ld bytes", size);
fclose(fp);
return 0;
}
Demonstrates: File Size
ftell() से फाइल का size पता करें
#include
int main() {
FILE *fp = fopen("data.bin", "wb");
int num = 1234;
fwrite(&num, sizeof(num), 1, fp);
fclose(fp);
return 0;
}
Demonstrates: Binary Write
fwrite() से binary लिखना
#include
int main() {
FILE *fp = fopen("data.bin", "rb");
int num;
fread(&num, sizeof(num), 1, fp);
printf("Value = %d", num);
fclose(fp);
return 0;
}
Demonstrates: Binary Read
fread() से binary पढ़ना
#include
int main() {
rename("example.txt", "renamed.txt");
return 0;
}
Demonstrates: Rename File
rename() से फाइल का नाम बदलें
#include
int main() {
remove("renamed.txt");
return 0;
}
Demonstrates: Delete File
remove() से फाइल delete करें