Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 41

File Structures

Organizing file for better performance of disk

Topics under discussion


Introduction to File Structures
Applications of File Structures
File structures in software's
File organizer in windows
File organizing in ubuntu
Chapter 1 : File processing operations
Road map of Syllabus
Text book
B.Meena,Asst.Prof.,IT Dept,ANITS

File structure
Is a combination of representations for data in
files
Operations for accessing data
Allows applications to read, write, modify data
Very important in needs of applications and
tremendous variety of applications

B.Meena,Asst.Prof.,IT Dept,ANITS

Applications
For representing a city region telephone network.
store a set of fixed key words which are referenced very
frequently.
represent an image in the form of a bitmap.
implement back functionality in the internet browser.
implement printer spooler so that jobs can be printed in the
order of their arrival.
record the sequence of all the pages browsed in one session.
store information about the directories and files in a system.

B.Meena,Asst.Prof.,IT Dept,ANITS

File structures in software's

B.Meena,Asst.Prof.,IT Dept,ANITS

Programming files c ,cpp,java,html,xml.


Database files - dbase ,foxpro,oracle-log,dmp
Log data and software files
Text files doc,txt,docx
B.Meena,Asst.Prof.,IT Dept,ANITS

File organizer in windows

B.Meena,Asst.Prof.,IT Dept,ANITS

In ubuntu

B.Meena,Asst.Prof.,IT Dept,ANITS

Why Study File Structure Design?


I. Data Storage
Computer data can be stored in three kinds of
locations:
Our
Focus

Primary Storage ==> Memory


Secondary Storage

[Computer Memory]

[Online Disk/ Tape/ CDRom that can be accessed by the


computer]
Tertiary Storage ==> Archival Data
[Offline Disk/Tape/ CDRom not directly available to the
computer.]

B.Meena,Asst.Prof.,IT Dept,ANITS

Why Study File Structure Design?


II. Memory versus Secondary Storage
Secondary storage such as disks can pack thousands
of megabytes in a small physical location.
Computer Memory (RAM) is limited.
However, relative to Memory, access to secondary
storage is extremely slow [E.g., getting information from slow RAM
takes 120. 10-9 seconds (= 120 nanoseconds) while getting information from Disk
takes 30. 10-3 seconds (= 30 milliseconds)]

B.Meena,Asst.Prof.,IT Dept,ANITS

10

Chapter 1 : file processing


operations
Physical file and logical file
Opening files
Closing files
Reading and writing
Seeking
Special characters in unix
Unix directory structure
Physical devices and logical files
File related header files
Unix file system commands
B.Meena,Asst.Prof.,IT Dept,ANITS

11

Physical file and logical file

B.Meena,Asst.Prof.,IT Dept,ANITS

12

In c and cpp
C
FILE *fp
fp=fopen(student.txt,r);
student.txt
CPP
fstream f1;
f1.open(student.txt,ios::in);

B.Meena,Asst.Prof.,IT Dept,ANITS

13

Physical file :
Actually exists on the storage
File known by computer OS and appears in the directory
Logical file :
file seen by the program
Allows to describe the operations performed on physical
file

B.Meena,Asst.Prof.,IT Dept,ANITS

14

Opening Files
Once we have a logical file identifier hooked up to
a physical file or device,
need to declare what we intend to do with the
file:
Open an existing file
Create a new file
That makes the file ready to use by the program
We are positioned at the beginning of the file and
are ready to read or write.
B.Meena,Asst.Prof.,IT Dept,ANITS

15

Opening Files in C and C++ : unix


syntax
fd = open(filename, flags [, pmode]);
fd = file descriptor
filename = physical file name
flags = O_APPEND, O_CREAT, O_EXCL, O_RDONLY,
O_RDWR, O_TRUNC, O_WRONLY.
pmode = rwe
rwe
rwe
owner group world

B.Meena,Asst.Prof.,IT Dept,ANITS

16

B.Meena,Asst.Prof.,IT Dept,ANITS

17

FILE *fopen( const char * filename, const char * mode );

B.Meena,Asst.Prof.,IT Dept,ANITS

18

B.Meena,Asst.Prof.,IT Dept,ANITS

19

Closing Files

Makes the logical file name available for another


physical file (its like hanging up the telephone after a
call).
Ensures that everything has been written to the file
[since data is written to a buffer prior to the file].
Files are usually closed automatically by the operating
system (unless the program is abnormally interrupted).

B.Meena,Asst.Prof.,IT Dept,ANITS

20

Reading
Read(Source_file, Destination_addr, Size)
Source_file = location the program reads from,
i.e., its logical file name
Destination_addr = first address of the memory
block where we want to store the data.
Size = how much information is being brought
in from the file (byte count).
B.Meena,Asst.Prof.,IT Dept,ANITS

21

B.Meena,Asst.Prof.,IT Dept,ANITS

22

Writing
Write(Destination_file, Source_addr, Size)
Destination_file = the logical file name where the
data will be written.
Source_addr = first address of the memory block
where the data to be written is stored.
Size = the number of bytes to be written.
B.Meena,Asst.Prof.,IT Dept,ANITS

23

B.Meena,Asst.Prof.,IT Dept,ANITS

24

Seeking
A program does not necessarily have to read through a
file sequentially: It can jump to specific locations in the file
or to the end of file so as to append to it.
The action of moving directly to a certain position in a file
is often called seeking.
Seek(Source_file, Offset)
Source_file = the logical file name in which the seek will occur
Offset = the number of positions in the file the pointer is to be
moved from the start of the file.

B.Meena,Asst.Prof.,IT Dept,ANITS

25

B.Meena,Asst.Prof.,IT Dept,ANITS

26

B.Meena,Asst.Prof.,IT Dept,ANITS

27

B.Meena,Asst.Prof.,IT Dept,ANITS

28

B.Meena,Asst.Prof.,IT Dept,ANITS

29

Special Characters in Files I


Sometimes, the operating system attempts to
make regular users life easier by automatically
adding or deleting characters for them.
F6
Ctrl z

B.Meena,Asst.Prof.,IT Dept,ANITS

30

The Unix Directory Structure


The Unix File System is a tree-structured
organization of directories. With the root of the
tree represented by the character /.
Each directory can contain regular files or other
directories.
The file name stored in a Unix directory
corresponds to its physical name.

B.Meena,Asst.Prof.,IT Dept,ANITS

31

B.Meena,Asst.Prof.,IT Dept,ANITS

32

Physical Devices and Logical


Files
Magnetic disks or tapes can be thought of as files
and so can the keyboard and the console.
No matter what the physical form of a Unix file (real
file or device), it is represented in the same way in
Unix: by an integer.

B.Meena,Asst.Prof.,IT Dept,ANITS

33

Stdout, Stdin, Stderr


Stdout --> Console
fwrite(&ch, 1, 1, stdout);
Stdin --> Keyboard
fread(&ch, 1, 1, stdin);
Stderr --> Standard Error (again, Console)
[When the compiler detects an error, the error
message is written in this file]

B.Meena,Asst.Prof.,IT Dept,ANITS

34

I/O Redirection and Pipes


< filename [redirect stdin to filename]
> filename [redirect stdout to filename]
program1 | program2 [take any stdout output
from program1 and use it in place of any stdin
input to program2.
E.g., list | sort

B.Meena,Asst.Prof.,IT Dept,ANITS

35

Unix System Commands

cat filenames --> Print the content of the named textfiles.


tail filename --> Print the last 10 lines of the text file.
cp file1 file2 --> Copy file1 to file2.
mv file1 file2 --> Move (rename) file1 to file2.
rm filenames --> Remove (delete) the named files.
chmod mode filename --> Change the protection mode on
the named file.
ls --> List the contents of the directory.
mkdir name --> Create a directory with the given name.
rmdir name --> Remove the named directory.
B.Meena,Asst.Prof.,IT Dept,ANITS

36

B.Meena,Asst.Prof.,IT Dept,ANITS

37

B.Meena,Asst.Prof.,IT Dept,ANITS

38

Text book

B.Meena,Asst.Prof.,IT Dept,ANITS

39

Assignment
Look up the operations equivalent to
open,close,create,read,write, seek in
COBOL,Ada,Fortran. compare them with c and c++
How do you use fseek, illustrate
What is the difference between pmode,O_RDWR
A couple of years ago a company bought a new cobol
compiler . One difference between the new compiler
and old one is , it did not automatically close files
when program execution terminates , where as old
compiler did . What sorts of problems would this cause
?
B.Meena,Asst.Prof.,IT Dept,ANITS

40

Look up the unix command wc . Execute the


following in unix and explain why it gives no.of
files in ls|wc-c
In some typical environments such as unix , dos,
all the following represent to move the data from
one place to another : scanf, fgetc , read , cat ,
fscanf , gets , < ,main(argc,argv) , getc, fgets , |
Describe which are quite useful , which of them
belong to c++ and OS.
Implement tail n command in your own way
Date of submission : 07-07-14
B.Meena,Asst.Prof.,IT Dept,ANITS

41

You might also like