File Handling
Understanding File Operations in C Programming
Introduction to File Handling (फाइल हैंडलिंग का परिचय)
File handling C programming में external files के साथ काम करने का तरीका है। ये data को permanently store करने और retrieve करने में help करता है।
File handling is the way to work with external files in C programming. It helps in storing and retrieving data permanently.
Key Points (मुख्य बिंदु):
- Files data को permanently store करती हैं
- Files store data permanently
- File operations के लिए FILE pointer का use किया जाता है
- FILE pointer is used for file operations
- Files को open और close करना important है
- Opening and closing files is important
File Operations (फाइल ऑपरेशन्स)
Basic file operations और उनके functions:
Basic file operations and their functions:
#include
// Opening a file
FILE *fp = fopen("example.txt", "r");
// Reading from file
char ch = fgetc(fp);
char str[100];
fgets(str, 100, fp);
// Writing to file
fputc('A', fp);
fputs("Hello World", fp);
// Closing a file
fclose(fp);
Operation (ऑपरेशन) | Function (फंक्शन) | Description (विवरण) |
---|---|---|
Open File | fopen() | फाइल को open करता है |
Open File | fopen() | Opens a file |
Read File | fgetc(), fgets() | फाइल से data read करता है |
Read File | fgetc(), fgets() | Reads data from file |
File Modes (फाइल मोड्स)
Different file modes और उनका use:
Different file modes and their usage:
// Different file modes
FILE *fp1 = fopen("file.txt", "r"); // Read mode
FILE *fp2 = fopen("file.txt", "w"); // Write mode
FILE *fp3 = fopen("file.txt", "a"); // Append mode
FILE *fp4 = fopen("file.txt", "r+"); // Read and write mode
FILE *fp5 = fopen("file.txt", "wb"); // Binary write mode
Text File
Binary File
Mode (मोड) | Description (विवरण) |
---|---|
"r" | फाइल को read के लिए open करता है |
"r" | Opens file for reading |
"w" | फाइल को write के लिए open करता है |
"w" | Opens file for writing |
Error Handling (एरर हैंडलिंग)
File operations में error handling के तरीके:
Ways to handle errors in file operations:
// Error handling example
FILE *fp = fopen("file.txt", "r");
if (fp == NULL) {
printf("Error opening file\n");
perror("Error");
return 1;
}
// Check for end of file
while (!feof(fp)) {
// Read file content
}
// Check for errors
if (ferror(fp)) {
printf("Error reading file\n");
}
fclose(fp);
Best Practices (सर्वोत्तम प्रथाएं)
File handling के best practices:
Best practices for file handling: