CM304
CM304
CM304
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
CM304.84 2
Objectives
CM304.84 3
Create or Open a File
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
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
CM304.84 8
Details
CM304.84 9
Example
CM304.84 10
Example
Contd..
Fig.1
CM304.84 11
File I/O functions
Read Write
functions functions
fputc()
Character I/O fgetc()
fputs()
String I/O fgets()
fprintf()
Formatted I/O fscanf()
fwrite()
Structured I/O fread()
Fig.2
CM304.84 12
Summary
CM304.84 13
Quiz
CM304.84 14
Quiz
CM304.84 15
Quiz
b)reading is possible
c)writing is possible
d)both b and c
CM304.84 16
Quiz
b)reading is possible
c)writing is possible
d)both b and c
CM304.84 17
Frequently Asked Questions
CM304.84 18