Anda di halaman 1dari 20

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 :File I/O functions
Teaching Aids :PPTs, Animations
CM304.85 1
Recap

 Explain how to create a file or opening a file.


 Explain how to close a file.
 What are the different file modes?
 What are the different file I/O functions?
 What is the use of feof()?

CM304.85 2
Objectives

On completion of this period, you would be able

to know …

 Read character data from file or write to file.

 Read integer data from file or write to file.

 Read strings from file or write to file.

CM304.85 3
Character file I/O

 fgetc(): To read a character from the file.


 Syntax: char fgetc (FILE *);
Example:
 char ch;
 FILE *fp;
 ch=fgetc(fp);
 get a character from the file pointed by fp into
the variable ch.

CM304.85 4
Character file I/O
Contd..
 fputc(): To write a character into the file.
 Syntax:int fputc (char, FILE *);
Example
 char ch;
 FILE *fp;
 fputc(ch,fp);
 Put the content of the variable ch into the file
pointed by fp.

CM304.85 5
Ex1:

CM304.85 6
Example2

CM304.85 7
Integer file I/O
 getw() :To read integer values from the file
 Syntax:int getw (FILE *);
Example:
 int n;
 FILE *fp;
 n=getw(fp);
 get an integer value from the file pointed by fp
into the variable n.

CM304.85 8
Integer file I/O

 putw() :To write integer values into the file


 Syntax:int putw (n,FILE *);
Example
 int n;
 FILE *fp;
 putw(n,fp);
 put the content of n into the file pointed by fp.

CM304.85 9
Example:

CM304.85 10
String File I/O
 fgets(): To read a string from a file.
 Syntax:
 char * fgets(char [], int, FILE *);
Example:
 char * fgets(a, n, fp);
 a is a character array.
 n is no. of characters to be read.
 fp-file pointer of type FILE.

CM304.85 11
Example

CM304.85 12
Example
 fputs(): To write a string into the file.
 Syntax
 int fputs(char [], FILE *);
Example:
 char a[25];
 FILE *fp;
 int fputs(a,fp);
 a is a character array.
 fp-file pointer of type FILE.
 Writes string ‘a’ into file.
CM304.85 13
Example

CM304.85 14
Summary

In this period, you have learnt about …


 Different file I/O functions.
 The fgetc() reads character data from the file.
 The fputc() writes character data into the file.
 The getw() reads interger values from the file.
 The putw() writes integer values into the file.
 The fgets() reads string from the file.
 The fputs() writes string into the file.

CM304.85 15
Quiz

1)The function used for writing a character to a


file is
a)fgetc()
b)fputs()
c)fputc()
d)none

CM304.85 16
Quiz

1)The function used for writing a character to a


file is
a)fgetc()
b)fputs()
c)fputc()
d)none

CM304.85 17
Quiz

2)Which of the following function is used to read


string from the file.
a)fgetc()
b)fputs()
c)fgets()
d)none

CM304.85 18
Quiz

2)Which of the following function is used to read


string from the file.
a)fgetc()
b)fputs()
c)fgets()
d)none

CM304.85 19
Frequently Asked Questions

1. Explain character file I/O functions with their


syntax.
2. Explain integer file I/O functions with their
syntax.
3. Explain string file I/O functions with their
syntax.

CM304.85 20

Anda mungkin juga menyukai