File handling in c

18

description

 

Transcript of File handling in c

Page 1: File handling in c
Page 2: File handling in c

Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd

Page 3: File handling in c

Week Target Achieved

1 30 20

2 30 21

3 30

Typing Speed

Page 4: File handling in c

File handling in c

Yasir musthafa [email protected]/yasirmusthafatwitter.com/yasirmusthafa ppin.linkedin.com/in/

yasirmusthafapp8891396749

Page 5: File handling in c

Console application

• Traditional I/O system• Text based interface(black windows screen)• User executes the program and output

displayed on the screen

Page 6: File handling in c

Draw back of traditional I/O

• Data is temporary• Not available during re-execution

Page 7: File handling in c

Introduction to file handling

• Data is stored onto the disk• Retrieve whenever required• Output –stored on to the disk

Page 8: File handling in c

File

• Sequence of bytes where group of related data is stored

• Collection of bytes interpreted asSingle characterSingle wordSingle lineComplete structure

Page 9: File handling in c

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 fileEg:-FILE *fpFp=fopen();

Page 10: File handling in c

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

Page 11: File handling in c

Two kindes of files

• Text(ASCII)• Binary(non ASCII) -image,audio,vedio,executable,etc

Page 12: File handling in c

Fopen()

• To create a new file or to open an existing fileSyntaxFopen(filename,mode);Eg:-fp=fopen(“data.txt”,”r”);Return NULL-unable to open

Page 13: File handling in c

modes

1. r –opens file for reading2. 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”

Page 14: File handling in c

Fclose()

• Close an opened fileSyntaxFclose(pointer_name);Eg:-fclose(fp);• Return zero on success• Return EOF on error

Page 15: File handling in c

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 pointerFputs()&fgets()Fputc()&fgetc()

Page 16: File handling in c

Example #include<stdio.h>void main(){ char name[50]; FILE *fp; fp=fopen("yasir.txt","r+"); fprintf(fp,"hello_yasir\n"); rewind(fp); fscanf(fp,"%s",name); printf("%s",name); fclose(fp);}

Page 17: File handling in c

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

Page 18: File handling in c

Contact Us

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Start up VillageEranakulam,Kerala, India.

Email: [email protected]

NIT-TBI,NIT Campus, Kozhikode,Kerala, India.