Introduction:
File handling ek vital part hai programming mein, jo applications ko external files se data read aur write karne ki permission deta hai. C programming mein, file handling hoti hai ek set ke functions ke through jo aapko system par files ke saath interact karne mein help karte hain. Is detailed guide mein, hum explore karenge C mein file handling ke basic principles, jisme file open, read, write, aur close operations shamil hain.
file handling functions in C:
1. fopen(): Open a file.
2. fclose(): Close a file.
3. fread(): Read data from a file.
4. fwrite(): Write data to a file.
5. fseek(): Set the file position.
6. ftell(): Get the current file position.
7. fprintf(): Write formatted data to a file.
8. fscanf(): Read formatted data from a file.
9. rewind(): Set the file position to the beginning.
10. remove(): Delete a file.
11. rename(): Rename a file.
These functions are fundamental for file-handling operations in the C programming language.
fopen()
fopen() function C mein ek file ko khulne ke liye istemal hoti hai. Ye file handling mein ek important function hai jo ek file aur program ke beech ek connection banane mein ka karta hai.
fopen() function ka syntax hai:
FILE *fopen(const char *filename, const char *mode);
filename: Ek string jo file ka naam contain karta hai, jo khulni hai.
mode: Ek string jo batata hai ki file kis mode mein khulni chahiye (jaise read, write, append).
`fopen()` function ek pointer return karta hai jo `FILE` structure ko point karta hai, jo ki aage ke file operations ke liye istemal hota hai.
Yahan ek `fopen()` ka istemal ek file ko read karne ke liye ka example hai:
#include <stdio.h>
int main() {
FILE *filePointer;
// File ko read karne ke liye kholein
filePointer = fopen("example.txt", "r");
if (filePointer == NULL) {
printf("File nahi mili ya khulne mein asamarth.\n");
return 1;
}
printf("File successfully khola gaya.\n");
fclose(filePointer);
return 0;
}
Is example mein program "example.txt" naam ki ek file ko read karne ke liye koshish karta hai. Agar file nahi milti ya khulne mein asamarth hoti hai, toh ek error message display hota hai. Agar safalta purvak, program file ke saath aur karyavahi karti hai. Ant mein, `fclose()` ka istemal karke file ko band kiya jata hai.
fopen() function ek mode parameter hai, jo batata hai ki file kis mode mein khuli jani chahiye. Yahaan kuch aam istemaal hone wale file modes hain:
"r" (Read): File ko padhne ke liye kholti hai. File ko maujood hona chahiye; nahi toh fopen NULL return karegi.
FILE *filePointer = fopen("example.txt", "r");
"w" (Write): File ko likhne ke liye kholti hai. Agar file pehle se maujood hai, toh uski puri content delete ho jaati hai. Agar file maujood nahi hai, toh nayi file ban jaati hai.
FILE *filePointer = fopen("output.txt", "w");
"a" (Append): File ko likhne ke liye kholti hai, lekin data ko file ke end mein add karti hai agar file pehle se maujood hai. Agar file maujood nahi hai, toh nayi file ban jaati hai.
FILE *filePointer = fopen("log.txt", "a");
"r+" (Read aur Write):File ko padhne aur likhne ke liye kholti hai. File ko maujood hona chahiye.
FILE *filePointer = fopen("data.txt", "r+");
"w+" (Write aur Read): File ko padhne aur likhne ke liye kholti hai. Agar file pehle se maujood hai, toh uski puri content delete ho jaati hai. Agar file maujood nahi hai, toh nayi file ban jaati hai.
FILE *filePointer = fopen("example.txt", "w+");
"a+" (Append aur Read): File ko padhne aur likhne ke liye kholti hai. Data ko file ke end mein add karti hai. Agar file maujood nahi hai, toh nayi file ban jaati hai.
FILE *filePointer = fopen("log.txt", "a+");
Ye modes batate hain ki file ka shuruaati sthiti aur allowed operations kya-kya hain. Chunein sahi mode ko base par aapko padhna hai, likhna hai, ya dono ke liye, aur kya file ko truncate karna hai ya nahi.
f close :-
fclose() function C programming mein ek file ko band karne ke liye istemal hoti hai. Ye function ek khuli hui file ko band karne mein madad karta hai aur system resources ko release karta hai.
fclose() function ka syntax hai:
int fclose(FILE *stream);
- stream: File ko point karne wala FILE pointer.
Yeh function ek integer value return karta hai jo kisi bhi error ko darust karta hai. Agar file bandhane mein koi problem hoti hai, toh yeh function EOF (End of File) ya koi specific error code return karta hai.
Yahan ek fclose() ka istemal ka example hai:
#include <stdio.h>
int main() {
FILE *filePointer;
// File ko read mode mein kholein
filePointer = fopen("example.txt", "r");
if (filePointer == NULL) {
printf("File nahi mili ya khulne mein asamarth.\n");
return 1;
}
printf("File successfully khola gaya.\n");
if (fclose(filePointer) == 0) {
printf("File successfully band ho gaya.\n");
} else {
printf("Error in closing the file.\n");
}
return 0;
}
Is example mein, fclose() function ka istemal kiya gaya hai file ko band karne ke liye. Agar file successfully band hoti hai, toh ek success message print hota hai, varna ek error message print hoti hai.
fread--
fread() function C programming mein ek file se data padhne ke liye istemal hoti hai. Ye function ek buffer mein data padhkar use pointer ke madhyam se pradan karta hai.
fread() function ka syntax hai:
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
- ptr: Pointer jo buffer ko point karta hai jisme data padha jayega.
- size: Har element ka size.
- nmemb: Padhne wale elements ki sankhya.
- stream: File stream ka pointer.
Yeh function ek size_t type ki value return karta hai, jo padhe gaye elements ki sahi sankhya ko darust karti hai.
Yahan ek fread() function ka istemal ka example hai:
#include <stdio.h>
int main() {
FILE *filePointer;
char buffer[100];
// File ko read mode mein kholein
filePointer = fopen("example.txt", "r");
if (filePointer == NULL) {
printf("File nahi mili ya khulne mein asamarth.\n");
return 1;
}
size_t bytesRead = fread(buffer, sizeof(char), 100, filePointer);
printf("Padha gaya data: %s\n", buffer);
fclose(filePointer);
return 0;
}
Is example mein, fread() ka istemal kiya gaya hai "example.txt" file se data padhne ke liye. Padha gaya data fir console par print kiya gaya hai.
f write
fwrite() function C programming mein ek file mein data likhne ke liye istemal hoti hai. Ye function ek buffer mein se data likhkar use file pointer ke madhyam se pradan karta hai.
fwrite() function ka syntax hai:
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
- ptr: Pointer jo buffer ko point karta hai jisme se data likha jayega.
- size: Har element ka size.
- nmemb: Likhne wale elements ki sankhya.
- stream: File stream ka pointer.
Yeh function ek size_t type ki value return karta hai, jo likhe gaye elements ki sahi sankhya ko darust karta hai.
Yahan ek fwrite() function ka istemal ka example hai:
#include <stdio.h>
int main() {
FILE *filePointer;
char data[] = "Hello, File Handling!";
filePointer = fopen("output.txt", "w");
if (filePointer == NULL) {
printf("File banane ya khulne mein asamarth.\n");
return 1;
}
size_t bytesWritten = fwrite(data, sizeof(char), strlen(data), filePointer);
printf("Likha gaya data: %s\n", data);
fclose(filePointer);
return 0;
}
Is example mein, fwrite() ka istemal kiya gaya hai "output.txt" file mein data likhne ke liye. Likha gaya data fir console par print kiya gaya hai.
fseek
seek() function C programming mein ek file mein kisi bhi position par jaane ke liye istemal hoti hai. Ye function file stream ki position ko set karne mein madad karta hai.
fseek() function ka syntax hai:
int fseek(FILE *stream, long offset, int whence);
- stream: File stream ka pointer.
- offset: Position ka offset (kis position par jaana hai).
- whence: Position ka base point (shuruaat, vartaman, ya ant).
Yeh function ek integer value return karta hai, jo kisi bhi error ko darust karta hai. Agar file mein position set karne mein koi problem hoti hai, toh yeh function EOF (End of File) ya koi specific error code return karta hai.
Yahan ek fseek() function ka istemal ka example hai:
#include <stdio.h>
int main() {
FILE *filePointer;
filePointer = fopen("example.txt", "r+");
if (filePointer == NULL) {
printf("File nahi mili ya khulne mein asamarth.\n");
return 1;
}
fseek(filePointer, 10, SEEK_SET);
long currentPosition = ftell(filePointer);
printf("Vartaman position: %ld\n", currentPosition);
fclose(filePointer);
return 0;
}
Is example mein, fseek() ka istemal kiya gaya hai "example.txt" file mein position set karne ke liye. Position set karne ke baad, vartaman position ko ftell() function se prapt kiya gaya hai aur console par print kiya gaya hai.
ftell
ftell() function C programming mein ek file ki vartaman position ko pata karne ke liye istemal hoti hai. Ye function file stream ki current position ko byte offset mein bataata hai.
ftell() function ka syntax hai:
long ftell(FILE *stream);
- stream: File stream ka pointer.
Yeh function ek long type ki value return karta hai, jo file ki vartaman position ko darust karti hai. Agar kisi bhi error ki sthiti mein ho, toh yeh function -1L return karegi.
Yahan ek ftell() function ka istemal ka example hai:
#include <stdio.h>
int main() {
FILE *filePointer;
filePointer = fopen("example.txt", "r+");
if (filePointer == NULL) {
printf("File nahi mili ya khulne mein asamarth.\n");
return 1;
}
long currentPosition = ftell(filePointer);
printf("Vartaman position: %ld\n", currentPosition);
fclose(filePointer);
return 0;
}
Is example mein, ftell() ka istemal kiya gaya hai "example.txt" file ki vartaman position ko prapt karne ke liye. Vartaman position ko console par print kiya gaya hai.
f printf
fprintf() function C programming mein ek file mein formatted data likhne ke liye istemal hoti hai. Ye function printf() ki tarah kaam karta hai, lekin iska output file mein jata hai.
fprintf() function ka syntax hai:
int fprintf(FILE *stream, const char *format, ...);
- stream: File stream ka pointer.
- format: Format specifier jo data ko kaise likha jayega, waisa bataata hai.
...: Additional arguments jo format specifier ke mutabiq hote hain.
Yeh function ek integer value return karta hai, jo likhe gaye characters ki sankhya ko darust karta hai. Agar koi error hoti hai, toh yeh EOF (End of File) ya koi specific error code return karta hai.
Yahan ek fprintf() function ka istemal ka example hai:
#include <stdio.h>
int main() {
FILE *filePointer;
filePointer = fopen("output.txt", "w");
if (filePointer == NULL) {
printf("File banane ya khulne mein asamarth.\n");
return 1;
}
int num = 42;
float pi = 3.14;
fprintf(filePointer, "Integer: %d, Float: %f\n", num, pi);
fclose(filePointer);
return 0;
}
Is example mein, fprintf() ka istemal kiya gaya hai "output.txt" file mein formatted data likhne ke liye. Likha gaya data console par print nahi hota, balki file mein chala jata hai.
fscanf
fscanf() function C programming mein ek file se formatted data padhne ke liye istemal hoti hai. Ye function scanf() ki tarah kaam karta hai, lekin iska input file se hota hai.
fscanf() function ka syntax hai:
int fscanf(FILE *stream, const char *format, ...);
- stream: File stream ka pointer.
- format: Format specifier jo data ko kaise padha jayega, waisa bataata hai.
...: Additional arguments jo format specifier ke mutabiq hote hain.
Yeh function ek integer value return karta hai, jo padhe gaye items ki sahi sankhya ko darust karta hai. Agar koi error hoti hai, toh yeh EOF (End of File) ya koi specific error code return karta hai.
Yahan ek fscanf() function ka istemal ka example hai:
#include <stdio.h>
int main() {
FILE *filePointer;
int num;
float pi;
filePointer = fopen("input.txt", "r");
if (filePointer == NULL) {
printf("File nahi mili ya khulne mein asamarth.\n");
return 1;
}
fscanf(filePointer, "Integer: %d, Float: %f", &num, &pi);
printf("Padha gaya data: Integer - %d, Float - %f\n", num, pi);
fclose(filePointer);
return 0;
}
Is example mein, fscanf() ka istemal kiya gaya hai "input.txt" file se formatted data padhne ke liye. Padha gaya data console par print kiya gaya hai.
rewind
rewind() function C programming mein ek file stream ka position pointer shuruaati position par reset karne ke liye istemal hoti hai.
rewind() function ka syntax hai:
void rewind(FILE *stream);
- stream: File stream ka pointer.
Yeh function koi value return nahi karta hai. Iska kaam hai file stream ka position pointer shuruaati position par le jaana, jisse aap phir se file ke shuruaat se data padh sakein.
Yahan ek rewind() function ka istemal ka example hai:
#include <stdio.h>
int main() {
FILE *filePointer;
filePointer = fopen("example.txt", "r");
if (filePointer == NULL) {
printf("File nahi mili ya khulne mein asamarth.\n");
return 1;
}
rewind(filePointer);
fclose(filePointer);
return 0;
}
Is example mein, rewind() function ka istemal kiya gaya hai "example.txt" file ke stream ka position pointer shuruaati position par reset karne ke liye.
remove():
remove() function C programming mein ek file ko delete karne ke liye istemal hoti hai. Ye function ek file ka naam leti hai aur usse disk par se hamesha ke liye hata deti hai.
remove() function ka syntax hai:
int remove(const char *filename);
- filename: Ek string jo file ka naam contain karta hai jo delete karna hai.
Yeh function ek integer value return karta hai, jo kisi bhi error ko darust karta hai. Agar file delete karne mein koi problem hoti hai, toh yeh -1 ya koi specific error code return karta hai.
Yahan ek remove() function ka istemal ka example hai:
#include <stdio.h>
int main() {
// File ko delete karne ke liye
int result = remove("example.txt");
if (result == 0) {
printf("File successfully delete ho gayi.\n");
} else {
printf("Error in deleting the file.\n");
}
return 0;
}
Is example mein, remove() function ka istemal kiya gaya hai "example.txt" file ko delete karne ke liye. Agar file successfully delete hoti hai, toh ek success message print hota hai, varna ek error message print hoti hai.
rename()
rename() function C programming mein ek file ka naam badalne ke liye istemal hoti hai. Iska istemal kisi file ka naam change karne ya file ko kisi doosre naam se rename karne ke liye kiya jata hai.
rename() function ka syntax hai:
int rename(const char *oldfilename, const char *newfilename);
- oldfilename: Pehle ka file name jo badalna hai.
- newfilename: Naya file name jise assign karna hai.
Yeh function ek integer value return karta hai, jo kisi bhi error ko darust karta hai. Agar file ka naam badalne mein koi problem hoti hai, toh yeh -1 ya koi specific error code return karta hai.
Yahan ek rename() function ka istemal ka example hai:
#include <stdio.h>
int main() {
// File ka naam badalne ke liye
int result = rename("oldfile.txt", "newfile.txt");
if (result == 0) {
printf("File ka naam successfully badal gaya.\n");
} else {
printf("Error in changing the file name.\n");
}
return 0;
}
Is example mein, rename() function ka istemal kiya gaya hai "oldfile.txt" ko "newfile.txt" mein badalne ke liye. Agar file ka naam successfully badal jata hai, toh ek success message print hota hai, varna ek error message print hoti hai.
Subscribe Our Newsletter
0 Comment
Post a Comment