You are on page 1of 18

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRA PRADESH
Name :M.Subramanyam
Designation :Senior Lecturer
Branch :Computer Engg.
Institute :Q.Q.Govt.Polytechnic,Hyderabad.
Year/Semester :III Semester
Subject :UNIX & C
Subject Code :CM-304
Topic :Files
Duration :50 Min
Sub Topic :fopen(),fclose()&File I/O functions
Teaching Aids :PPTs, Animation
CM304.84 1
Recap

 What is program file?


 What is data file?
 Explain data hierarchy.
 Explain interaction between program and
console.
 Explain interaction between program and file.

CM304.84 2
Objectives

On completion of this period, you would be able


to know…
 Create or open a file
 Close a file
 File modes
 File I/O functions

CM304.84 3
Create or Open a File

 fopen(): To open or create a file


 Syntax:
Predefined Name of the Read, Write or
structure file Append
 FILE *fopen(<filename>,<mode>);
 Opening a file returns a pointer to a FILE
structure
 FILE is a predefined structure.
 FILE structure is defined in stdio.h file.
 If fopen() fails, NULL is returned.

CM304.84 4
Create or Open a File
Contd..
Example
 FILE *myPtr;
 Creates a FILE pointer called myPtr
 myPtr = fopen ("myFile.dat" , "r");
 If "myFile.dat" exists fopen function
returns reference of "myFile.dat".
 "myFile.dat" is opened in read mode.
 If "myFIle.dat" is not existing fopen()
returns NULL.
CM304.84 5
File Open Modes

Sno Mode Description


1 r Open a file for reading.

2 w Open a file for writing. If the file already exists,


discard the current contents.
3 a Append:open or create a for writing at the end of
the file.
4 r+ Open a file for update(reading and writing).

5 w+ Open a file for update. If the file already exists


discard current contents.
6 a+ Append:open or create a file for update, write is
done only at the end of the file.

CM304.84 6
fclose() function
 To close the file.
 Syntax: fclose(FILE *pointer);
 Closes specified file.
 It will place “eof ” at the end of the file.
 File pointer is moved to the beginning of
the file.
 Performed automatically when program
ends.
 Good practice to close files explicitly.

CM304.84 7
feof() function

feof(): stands for file end of file.

Syntax:feof( FILE *pointer )


 Returns true if file pointer is reached end-
of-file (no more data to process).
 Otherwise returns false.

CM304.84 8
Details

 Programs may process no files, one file, or


many files.

 Each file must have a unique name and


should have its own pointer.

CM304.84 9
Example

CM304.84 10
Example
Contd..

Test.dat file is opened


in the current working
directory

Fig.1
CM304.84 11
File I/O functions
Read Write
functions functions

fputc()
Character I/O fgetc()

fputs()
String I/O fgets()

File I/O putw()


Integer I/o getw()

fprintf()
Formatted I/O fscanf()

fwrite()
Structured I/O fread()

Fig.2
CM304.84 12
Summary

In this period, you have learnt about ….


 file can be created or opened using fopen().
 fclose() is used to close the file.
 Different file modes like read, write, append etc.,
 Different file I/O functions.

CM304.84 13
Quiz

1)If fopen() fails, it returns


a) -1
b) 1
c) NULL
d) a file pointer

CM304.84 14
Quiz

1)If fopen() fails, it returns


a) -1
b) 1
c) NULL
d) a file pointer

CM304.84 15
Quiz

2)If a file is opened in w+ mode then


a)appending is possible

b)reading is possible

c)writing is possible

d)both b and c

CM304.84 16
Quiz

2)If a file is opened in w+ mode then


a)appending is possible

b)reading is possible

c)writing is possible

d)both b and c

CM304.84 17
Frequently Asked Questions

1. Explain the function fopen() with its syntax.

2. What are the various file operating modes in C.

3. Explain the function fclose() with its syntax.

CM304.84 18