FILE HANDLING IN C:-
File & Directory:-
File is a mechanism which stores data and Directory is a mechanism which stores file.
Types of File:-
Regular File
Directory FIle
FIFO File
Socket File
Link File
Character special File
Block special File
The maximum size of the file depends upon the file system such as
Windows:-
FAT32-4GB
NTFS-more than 4GB
MS-DOS:-
FAT 16 -64kb
Different file operation and their standard library function:-
file operation Standard library function
————— ——————————
Open a file fopen()
Close a file fclose()
Remove a file fremove()
Rename a file frename()
Transfer control to the beginning of a file rewind()
Transfer control to any position of a file fseek()
Write a character into a file putc()
Read a character from a file getc()
Write a word into a file putw()
Read a word from a file getw()
Write a string into a file fputs()
Read a string in a file fgets()
Write a structure/records into a file fwrite()
Read a structure/records from a file fread()
Write a formatted data into a file fprintf()
Read formatted data from a file fscanf()
File Opening mode:-
Write mode “w”
Append mode “a”
Read mode “r”
Write Mode:-
Create a new file if not exists, if exists delete the contains of the file. It is also called truncated.
Append Mode:-
Create a new file if not exists, if exists then doesn’t delete the contains of the file.
Read Mode:-
Read mode only read the character.
Measure difference between write mode and append mode:-
Write mode file is truncated but append mode file is not truncated.
PROGRAM:- FILE IS PRESENT OF NOT PRESENT #include"stdio.h" main() { FILE *p,*q; char x; p=fopen("/home/litindia/Desktop/letter.txt","r"); if(p==0) { printf("file is not found"); return; } else { printf("File is present"); } }
PROGRAM:- READ THE CONTAINS OF THE FILE #include"stdio.h" main() { FILE *p,*q; char X; p=fopen("/home/litindia/Desktop/letter.txt","r"); if(p==0) { printf("file is not found"); return; } else { while((X=getc(p))!=EOF) { printf("%c",X); } } }
PROGRAM:- COPY THE CONTAINS OF FILE INTO ANOTHER FILE #include"stdio.h" main() { FILE *p,*q; char x; p=fopen("/home/litindia/Desktop/letter.txt","r"); q=fopen("/home/litindia/Desktop/report.txt","w"); if(p==0) { printf("file is not found"); return; } else { while((x=getc(p))!=EOF) { putc(x,p); } } } Also Read:-Pointer in C Also Read:-Structure Union in C Also Read:- Function in C
2 replies on “File Handling in C”
It?¦s actually a great and helpful piece of info. I am satisfied that you simply shared this helpful info with us. Please keep us informed like this. Thank you for sharing.
THank u.