File handling in c

Post on 31-Oct-2014

222 views 0 download

Tags:

description

 

Transcript of 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

Week Target Achieved

1 30 20

2 30 21

3 30

Typing Speed

File handling in c

Yasir musthafa ppyasirmusthafapp@gmail.comwww.facebook.com/yasirmusthafatwitter.com/yasirmusthafa ppin.linkedin.com/in/

yasirmusthafapp8891396749

Console application

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

displayed on the screen

Draw back of traditional I/O

• Data is temporary• Not available during re-execution

Introduction to file handling

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

File

• Sequence of bytes where group of related data is stored

• Collection of bytes interpreted asSingle characterSingle wordSingle lineComplete structure

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();

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

Two kindes of files

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

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

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”

Fclose()

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

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()

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);}

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

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: info@baabtra.com

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