2. Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .
Ltd
4. File handling in c
Yasirmusthafapp
yasirmusthafapp@gmail.com
www.facebook.com/yasirmusthafa
twitter.com/yasirmusthafapp
in.linkedin.com/in/yasirmusthafapp
8891396749
5. Console application
• Traditional I/O system
• Text based interface(black windows screen)
• User executes the program and output
displayed on the screen
6. Draw back of traditional I/O
• Data is temporary
• Not available during re-execution
7. Introduction to file handling
• Data is stored onto the disk
• Retrieve whenever required
• Output –stored on to the disk
8. File
• Sequence of bytes where group of related
data is stored
• Collection of bytes interpreted as
Single character
Single word
Single line
Complete structure
9. File (conti)
• Last byte of file contains end of file character
(EOF)
• Can be used to check to know the end
• Structure pointer of file type –to declare a file
Eg:-FILE *fp
Fp=fopen();
10. File handling
• Fopen() -create or open a file
• Fclose() -close a file
• Getc() -reads a character from a file
• Putc() -writes a character to a file
• Fscanf() -reads a set of data from a file
• Fprintf() -writes a set of data to a file
• Getw() -reads an integer from a file
• Putw() -writes an integer to a file
• Fseek() -set the position to desire point
• Ftell() -gives current position in the file
• Rewind() -set the position to the begning
11. Two kindes of files
• Text(ASCII)
• Binary(non ASCII)
-image,audio,vedio,executable,etc
12. Fopen()
• To create a new file or to open an existing file
Syntax
Fopen(filename,mode);
Eg:-fp=fopen(“data.txt”,”r”);
Return NULL-unable to open
13. modes
1. r –opens file for reading
2. w-creates a file writing , over writes on previous
content(delete)
3. a-opens a file for appending-writing on the end
of file
• “+” character-read & write
• Can add a “b” character(non asci) –file is a binary
-”rb” , ”wb” or “ab”
14. Fclose()
• Close an opened file
Syntax
Fclose(pointer_name);
Eg:-fclose(fp);
• Return zero on success
• Return EOF on error
15. Writing to a file&Reading data from a
file
Fprintf() & fscanf()
• Just like printf() & scanf()
Eg:-fprintf(fp,”babtra”);
Eg:-fscanf(fp,”%s”,variable);
• Except-first argument:file pointer
Fputs()&fgets()
Fputc()&fgetc()
17. If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
18. Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com
NIT-TBI,
NIT Campus, Kozhikode,
Kerala, India.