Sunteți pe pagina 1din 17

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 Processing facilities
Teaching Aids :PPTs, Animation
CM304.83 1
Recap

 Can you store data permanently in variables.

 Can you store data permanently in arrays.

 Are the variables temporary or permanent.

 Are the variables created in RAM or Disk.

CM304.83 2
Objectives

On completion of this period, you would be able


to know…
 Program file
 Data files
 Data hierarchy
 Interaction between console and program
 Interaction between file and program

CM304.83 3
Introduction to Files

Records
Program file
Data file

a,b & c are


temporary variables

Data file contains only data

Program file contains only code,


CM304.83 4
Data files

Data files
 Can be created, updated, and processed by
C programs.
 Storage of data in variables and arrays is only
temporary.
 Data files are used for permanent storage of
large amounts of data.

CM304.83 5
Data Hierarchy

 Bit – smallest data item


 Value of 0 or 1
 Byte – 8 bits
 Used to store a character
 Decimal digits, letters, and special
symbols

CM304.83 6
Data Hierarchy
Contd..

 Field – group of characters conveying


meaning.
Example: your name
 Record – group of related fields.
 Represented by a struct.
 Example: In a payroll system, a record for a
particular employee that contained his/her
identification number, name, address, etc.,

CM304.83 7
Data Hierarchy
Contd..

 Data Hierarchy

 File – group of related records.

Example: employee file

 Database – group of related files.

CM304.83 8
Example

Fig.1

CM304.83 9
Steps for File operations
Steps Operation Function(s)
Step1 Opening a file or fopen()
creating a file

Step2 Read data from file or fgetc(), fputc(), fgets(),


write data to file fputs(), getw(), putw(),
fscanf(), fprintf(),
fread(), fwrite()
Step3 Save and Close the fclose()
file

Fig.2

CM304.83 10
Data Communication
Interaction between OUTPUT
INPUT program and file

the file
Write data to
file
Read data from

fputc()
getw()
fgetc() files fputs()
putw()
fgets()
fscanf() Disk fprintf()
fread() fwrite()
------

output device
Send data to the
printf()
from the KB
Read data

scanf()
gets()
RAM( Program+data) puts()
putchar()
getchar() etc
etc Interaction
between
Keyboard program and Monitor
console
Fig.3

CM304.83 11
Summary

In this period, you have learnt about…


 Text file contains only text.
 Data file contains data.
 The hierarchy of bit, byte, field, record and file.
 Steps to perform file operations.
 Interaction between console and program.
 Interaction between file and program.
CM304.83 12
Quiz

1)The fscanf() reads data from


a) file
b) keyboard
c) both 1 and 2
d) none

CM304.83 13
Quiz

1)The fscanf() reads data from


a) file
b) keyboard
c) both 1 and 2
d) none

CM304.83 14
Quiz

2)The printf() writes data to


a) file
b) monitor
c) keyboard
d) none

CM304.83 15
Quiz

2)The printf() writes data to


a) file
b) monitor
c) keyboard
d) none

CM304.83 16
Frequently Asked Questions

1. Write the steps to perform file operations.

2. List different file I/O functions.

CM304.83 17

S-ar putea să vă placă și