File Handling
BENEFITS OF FILE HANDLING
- give permanent storage facility
- can access huge chunk of data in instant (bcz RAM is of small size -> can't store huge data)
- easy to transfer data as files
Text files - easy to read file (like simple text editor)
Binary files - hard to read file => sth like compiled form of text files - > (encrypted in binary form)
Can have image, audio, video,etc. {TO check EOF here => FILE size value needs to be checked}
Data - bits of storage
Information - when we make that data meaningful after processing
We use FILE as data type (eg.- FILE* ptr =NULL)
MODE -
- read (r) , write(w), read/write in binary (rb/wb), append(a)
- r+ => open for both reading and writing
- w+ =>
- w , a ,w+, a+ =>If file don't exist -> create new file then do operation on it
Opening of file => ptr = fopen("test.txt", "r")
-> if file don't exist -> ptr = = NULL
file first get copied in RAM then only we can do further operations => ptr store address of file so that it can be accessed easily in RAM
Closing a file => fclose(ptr)
Reading a file => ptr = fscanf(ptr, "%d", &a) => FILE should be in READ mode
{ check EOF to know the end }
{ check EOF to know the end }
Writing to a file => ptr = fprintf(ptr, "%d", a) => FILE should be in WRITE mode
-> always written from scratch (remove earlier written thing and overwrite new stuff)
Appending a file => ptr = fprintf(ptr, "%d", a) => FILE should be in Append mode
-> add new thing in earlier file (DON'T overwrite new stuff)
Last byte of file contain EOF (end of file character)
- fgets(name, 5, ptr) => 5 denotes maximum number of characters to be read
- It returns NULL if there is an error (such as EOF).
Three special file streams are defined in the <stdio.h> :
a) stdin => reads input from the keyboard
b) stdout => send output to the screen
c) stderr => prints errors to the screen
Comments
Post a Comment