SlideShare una empresa de Scribd logo
1 de 30
 File Organization uses as storage of data, organization of data and
access to data, this will be built on your knowledge of Data
Structures.
 File Structure Design is fast access to great capacity, reduce the
number of disk accesses, by collecting data into buffer blocks or
buckets and manage growth by splitting these collections.
 Physical file is physically exists on secondary storage, known by
the operating system, appears in its file directory.
 Logical file is what your program actually uses, a ‘pipe’ though
which information can be extracted, or sent.
2 - File Organization
 The database is stored as a collection of files, each file is a
sequence of records, a record is a sequence of fields.
 In sequential file organization each relation is stored in a
file, one may rely in the file system of the underlying
operating system
 Multitable clustering may have significant gains in
efficiency, but this may not be compatible with the file
system of the operating system
 First approach is assume record size is fixed, each file has
records of one particular type only and different files are
used for different relations
Fig: File Organization
History of File Structures Design
1. In the beginning it was the tape: Sequential access: Access cost
proportional to size of file.
2. Disks became more common: Direct access: Analogy to access to
position in array and Indexes were invented: list of keys and points
stored in small file allows direct access to a large primary file.
3. Tree structures emerged for main memory (1960`s): Binary
Search Trees (BST`s) and Balanced, Self adjusting BST`s: e.g.
AVL trees (1963).
4. A tree structure suitable for files was invented: B trees (1979)
and B+ trees: good for accessing millions of records with 3 or 4
disk accesses.
5. What about getting info with a single request: Hashing
Tables(1970).
File Organization: FILE is a predefined data type.
 It is defined in stdio.h is a data type.
 To reference a data file in C, we need to declare a pointer
variable of type FILE.
Syntax: FILE *<variable-name>;
Where FILE is a special type data structure contained in
the standard header file stdio.h that contains information
about the current status of the file.
 The variable-name refers to the name of the pointer
variable.
 A file is preferable to an array of structures because
i. File is permanent even after the program that created it
terminates.
ii. We can store huge amount of data in a file
iii. Other languages and system features can deal with files.
 File is a data type and a region of storage.
 The structure FILE is defined in stdio.h header file
 We declare file pointer as: FILE *fptr;
 FILE is a struct data type defined in stdio.h
File pointer: A file pointer is a pointer to FILE data type.
 A file pointer is a stream pointer.
 A file pointer is a buffer pointer.
Syntax: FILE *fp1,*fp2;
 A pointer to FILE data type.
 File pointer is a pointer to FILE data type is called as a
stream pointer or a file pointer.
 A file pointer points to the block of information of the
stream that has just been opened.
 A stream is source or destination of data that may be
associated with a disk or other I/O devices.
The statement:
FILE *fptr1, *fptr2 ;
Where fptr1 and fptr2 are pointer variables of type FILE.
 They will be assigned the address of a file descriptor, that
is, an area of memory that will be associated with an
input or output stream.
 Whenever you are to read from or write to the file, you
must first open the file and assign the address of its file
descriptor (or structure) to the file pointer variable.
/*Write a program declare, open, read and close use FILES*/
#include<stdio.h>
/* writing the data onto the file */
main()
{
FILE *fptr; /* declare file pointer */
char ch;
fptr=fopen("TEXT.DATA", "w");
/* open file for writing */
while((ch=getch())!=EOF)
/* read a character from keyboard*/
putc(ch,fptr);
/* write a character to file thru pointer */
fclose(fptr); /* close a file */
}
/*program to read and write some characters on files */
#include<stdio.h>
#include<conio.h>
void main()
{ FILE *fp;
char ch;
clrscr();
fp=fopen("cds.it","w");
printf("enter some characters on to the file");
while((ch=getchar())!=EOF)
fputc(ch,fp);
fclose(fp);
fp=fopen("cds.it","r");
while((ch=getchar())!=EOF)
putchar(ch);
fclose(fp);
}
File operation functions in C
Function Name Operation
fopen() Creates a new file for use(Opens a new existing file for use)
fclose Closes a file which has been opened for use
getc() Reads a character from a file
putc() Writes a character to a file
fprintf() Writes a set of data values to a file
fscanf() Reads a set of data values from a file
getw() Reads a integer from a file
putw() Writes an integer to the file
fseek() Sets the position to a desired point in the file
ftell() Gives the current position in the file
rewind() Sets the position to the beginning of the file
Text files: A file that holds text. The term text file is often used
as a synonym for ASCII file, a file in which characters are
represented by their ASCII codes.
 Common functions used for reading from a text stream and used
more memory
– fgetc()
– getc()
– fgets()
– fscanf()
 Common functions used for writing from a text stream used
memory.
– fputc()
– putc()
– fputs()
– fprintf()
Binary files: Files containing non textual information.
 A binary file is a file whose content must be interpreted by a
program or a hardware processor that understands in advance
exactly how it is formatted.
 Common functions used for reading from a binary stream and
used less memory
fgetw()
getw()
 Common functions used for writing from a binary stream and used
less memory
fputw()
putw()
/* Reading and Writing characters in Text files*/
#include<stdio.h>
void main()
{ FILE *fp; char a;
fp = fopen("data1","w");
printf("Enter characters into the file , press $ to quitn");
do
{ scanf("%c",&a);
putc(a,fp);
}while(a!=‘$');
fclose(fp);
fp= fopen("data1","r");
while((a=getc(fp))!='$')
printf(" %c",a);
fclose(fp); getch();
}
Text files: A file that holds text. The term text file is
often used as a synonym for ASCII file, a file in which
characters are represented by their ASCII codes.
 Text Files means collection of characters stores in a file.
 File functions are getc() and putc()
 In text files characters or integers are readable
 New line takes 2 bytes
 Each character takes1 byte
 Space takes 1 byte
 Memory consumption more
/* Program to reading and writing characters in text files*/
#include<stdio.h>
void main()
{ FILE *fp; char a;
clrscr();
fp = fopen("data1","w");
printf("Enter characters into the file , press $ to quitn");
do
{ scanf("%c",&a);
putc(a,fp);
}while(a!=‘$');
fclose(fp);
fp= fopen("data1","r");
while((a=getc(fp))!='$')
printf(" %c",a);
fclose(fp); getch();
}
Binary files: Files containing non textual information.
 A binary file is a file whose content must be interpreted
by a program or a hardware processor that understands in
advance exactly how it is formatted.
 Binary files means collection of ASCII values stores in a
file
 File functions are getw() and putw().
 In binary files characters or integers are not readable
 New line takes zero bytes
 Each integer takes 2 bytes
 Space takes zero bytes
 Memory consumption less.
/* Program to Reading and Writing integers in binary files*/
#include<stdio.h>
void main()
{ FILE *fp; int a,i,n;
clrscr();
fp = fopen("data1","w");
printf("Enter integers into the file , press -1 to quitn");
do
{ scanf("%d",&a);
putw(a,fp);
}while(a!=-1);
fclose(fp);
fp= fopen("data1","r");
while((a=getw(fp))!=-1)
printf(" %d",a);
fclose(fp);
getch();
}
Text Files vs. Binary Files
1. Text Files means collection of
characters stores in a file.
2. The I/O stream can be a text stream
or binary stream.
3. A text stream is a sequence of text. In
a text stream, each line consists of
zero or more characters, terminated
with new line character.
4. File functions are getc() and putc()
5. In text files characters or
integers are readable
6. New line takes 2 bytes
7. Each character takes1 byte
8. Space takes 1 byte
9. Memory consumption more
1. Binary files means collection of
ASCII values stores in a file.
2. A binary stream is a sequence of
bytes.
3. In a binary stream, it represents
raw data without any conversion.
For example, the new line
character ‘n’ is not mapped to
carriage return while using the
binary streams.
4. File functions are getw() and
putw().
5. In binary files characters or
integers are not readable
6. New line takes zero bytes
7. Each integer takes 2 bytes
8. Space takes zero bytes
9. Memory consumption less.
Writing and reading integers: C provides to write integers
to the files and read integers from the files.
 C provides the following I/O functions
1. putw() – putw function outputs the integer w to the given
stream.
2. getw() – getw function returns the next integer in the
named input stream.
1. putw() function: putw() – putw function outputs the
integer w to the given stream.
 putw() neither expects nor causes special alignment in the
file.
 On success, putw() returns the integer w.
 On error, putw() returns EOF
Syntax: putw(int w,fptr);
Where fptr is a pointer to type FILE.
2. getw() function: getw() – getw function returns the next
integer in the named input stream.
Syntax: getw(fptr);
 It assumes no special alignment in the file.
 getw() should not be used when the stream is opened in
text mode.
 getw() returns the next integer on the input stream.
 On end-of-file or error, getw() returns EOF.
FILE MODE OPERATIONS
“r” - Reading mode
“w” - Writing mode
“a” - Appending mode
“r+” - both reading and writing mode
“w+” - both reading and writing mode
“a+” - both reading and appending mode
“rb” - Same as mode “r”, but for binary file
“wb” - Same as mode “w”, but for binary file.
/*Wap to read integers from the keyboard and writes them
to the file(“INTEGERS.DAT”).*/
#include<stdio.h>
#include<stdlib.h>
main()
{
FILE *fp;
int i,numb;
clrscr();
fp=fopen("INTEGERS.DAT","w");
if(fp==NULL)
{
printf("Error opening filen");
exit(1);
}
for(i=0;i<100;i++)
{ scanf("%d",&numb);
if(numb==-99) break;
putw(numb,fp);
}
fclose(fp);
fp=fopen("INTEGERS.DAT","r");
if(fp==NULL)
{ printf("Error opening file");
exit(1);
}
while((numb=getw(fp))!=EOF)
printf(" %d", numb);
fclose(fp); getch();
}
File Handling Functions
 FILE is a structure that holds the description of a file and is defined in
stdio.h.
1. Naming a file – Ex. “Text.dat”, “cse.it”
2. Opening a file - fp = fopen(“filename”, ”accessmode”);
3. Reading from or writing into a File
fgetc(),fputc() – reading , writing characters
fgets(),fputs() – reading , writing strings
fread(),fwrite() – reading , writing blocks of data
fprintf(),fscanf() – for formatted I/O functions
4. Closing a file: syntax: fclose(fp);
5. fseek(FILE * , int Offset , int origin) –
makes the file pointer at a desired location.
6. ftell(fp) – which tells the position of file pointer in the file
7. rewind(fp) – places the file pointer at beginning of the file
 Organization of Records in Files: The choice of a proper
organization of records in a file is important for the efficiency of
real databases.
i. Heap – A record can be placed anywhere in the file where there is
space.
ii. Sequential – Store records in sequential order based on the value
of the search key of each record.
iii. Hashing – A hash function computed on some attribute of each
record, the result specifies in which block of the file the record
should be placed.
iv. Multitable clustering file organization - Records of several
different relations can be stored in the same file and motivation is
to store related records on the same block to minimize I/O.
 File Organization is two types
1. Sequential File Organization
2. Multitable Clustering File Organization
1. Sequential File Organization: Suitable for applications
that require sequential processing of the entire file. The
records in the file are ordered by a search-key
2. Multitable Clustering File Organization: Store several
relations in one file using a multitable clustering file
organization.
Files Uses
1. Real life situations involve large volume of data and in such
cases, the console oriented I/O operations pose two major
problems.
2. It becomes cumbersome and time consuming to handle large
volumes of data through terminals.
3. The entire data is lost when either the program is terminated or
computer is turned off therefore it is necessary to have more
flexible approach where data can be stored on the disks and read
whenever necessary, without destroying the data, this method
employs the concept of files to store data.
Data Structures vs. File Structures
1. A Systematic way of organizing and
accessing data is called Data
Structure.
2. It involves Representation of Data
and Operations for accessing data.
3. Data Structures deal with data in
main memory.
4. Data structures study how data are
stored in a computer so that
operations can be implemented
efficiently, Conceptual and concrete
ways to organize data for efficient
storage and manipulation.
5. Data structure requires
i. Space for each data item it
stores.
ii. Time to perform each basic
operation.
iii. Programming Effort.
1. Data processing from a computer
science perspective is Storage of
data, Organization of data and
Access to data.
2. It involves Representation of Data
and Operations for accessing data.
3. File Structures deal with data in
secondary storage device (File).
4. Good File Structure Design is Fast
access to great capacity, Reduce the
number of disk accesses, By
collecting data into buffers / blocks
or buckets and Manage growth by
splitting these collections.
5. A file can be treated as
i. a stream of bytes
ii. a collection of records with
fields (we will discuss it know )

Más contenido relacionado

La actualidad más candente

12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS
koolkampus
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
Aakash deep Singhal
 

La actualidad más candente (20)

Metadata ppt
Metadata pptMetadata ppt
Metadata ppt
 
Distributed database management system
Distributed database management  systemDistributed database management  system
Distributed database management system
 
12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS
 
Chapter 11 - File System Implementation
Chapter 11 - File System ImplementationChapter 11 - File System Implementation
Chapter 11 - File System Implementation
 
FIle Organization.pptx
FIle Organization.pptxFIle Organization.pptx
FIle Organization.pptx
 
File organization
File organizationFile organization
File organization
 
Indexing
IndexingIndexing
Indexing
 
Indexing and Hashing
Indexing and HashingIndexing and Hashing
Indexing and Hashing
 
Hashing
HashingHashing
Hashing
 
File Management in Operating System
File Management in Operating SystemFile Management in Operating System
File Management in Operating System
 
Linked list
Linked listLinked list
Linked list
 
Relational model
Relational modelRelational model
Relational model
 
Data indexing presentation
Data indexing presentationData indexing presentation
Data indexing presentation
 
Dbms architecture
Dbms architectureDbms architecture
Dbms architecture
 
Swap-space Management
Swap-space ManagementSwap-space Management
Swap-space Management
 
Shared memory
Shared memoryShared memory
Shared memory
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
 
File organization and introduction of DBMS
File organization and introduction of DBMSFile organization and introduction of DBMS
File organization and introduction of DBMS
 
Hashing
HashingHashing
Hashing
 
Data Structure and Algorithms Hashing
Data Structure and Algorithms HashingData Structure and Algorithms Hashing
Data Structure and Algorithms Hashing
 

Similar a File Organization

INput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptxINput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptx
AssadLeo1
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10
patcha535
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handling
pinkpreet_kaur
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
pinkpreet_kaur
 

Similar a File Organization (20)

PPS-II UNIT-5 PPT.pptx
PPS-II  UNIT-5 PPT.pptxPPS-II  UNIT-5 PPT.pptx
PPS-II UNIT-5 PPT.pptx
 
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdfAdvance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
 
File Management in C
File Management in CFile Management in C
File Management in C
 
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfEASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
 
Concept of file handling in c
Concept of file handling in cConcept of file handling in c
Concept of file handling in c
 
Unit 8
Unit 8Unit 8
Unit 8
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
 
File Handling
File HandlingFile Handling
File Handling
 
Module 5 file cp
Module 5 file cpModule 5 file cp
Module 5 file cp
 
File handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptxFile handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptx
 
File handling-c
File handling-cFile handling-c
File handling-c
 
data file handling
data file handlingdata file handling
data file handling
 
INput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptxINput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptx
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 
Programming in C Session 4
Programming in C Session 4Programming in C Session 4
Programming in C Session 4
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handling
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
 
Unit 5 dwqb ans
Unit 5 dwqb ansUnit 5 dwqb ans
Unit 5 dwqb ans
 

Último

Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Último (20)

TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 

File Organization

  • 1.  File Organization uses as storage of data, organization of data and access to data, this will be built on your knowledge of Data Structures.  File Structure Design is fast access to great capacity, reduce the number of disk accesses, by collecting data into buffer blocks or buckets and manage growth by splitting these collections.  Physical file is physically exists on secondary storage, known by the operating system, appears in its file directory.  Logical file is what your program actually uses, a ‘pipe’ though which information can be extracted, or sent. 2 - File Organization
  • 2.  The database is stored as a collection of files, each file is a sequence of records, a record is a sequence of fields.  In sequential file organization each relation is stored in a file, one may rely in the file system of the underlying operating system  Multitable clustering may have significant gains in efficiency, but this may not be compatible with the file system of the operating system  First approach is assume record size is fixed, each file has records of one particular type only and different files are used for different relations
  • 4. History of File Structures Design 1. In the beginning it was the tape: Sequential access: Access cost proportional to size of file. 2. Disks became more common: Direct access: Analogy to access to position in array and Indexes were invented: list of keys and points stored in small file allows direct access to a large primary file. 3. Tree structures emerged for main memory (1960`s): Binary Search Trees (BST`s) and Balanced, Self adjusting BST`s: e.g. AVL trees (1963). 4. A tree structure suitable for files was invented: B trees (1979) and B+ trees: good for accessing millions of records with 3 or 4 disk accesses. 5. What about getting info with a single request: Hashing Tables(1970).
  • 5. File Organization: FILE is a predefined data type.  It is defined in stdio.h is a data type.  To reference a data file in C, we need to declare a pointer variable of type FILE. Syntax: FILE *<variable-name>; Where FILE is a special type data structure contained in the standard header file stdio.h that contains information about the current status of the file.  The variable-name refers to the name of the pointer variable.
  • 6.  A file is preferable to an array of structures because i. File is permanent even after the program that created it terminates. ii. We can store huge amount of data in a file iii. Other languages and system features can deal with files.  File is a data type and a region of storage.  The structure FILE is defined in stdio.h header file  We declare file pointer as: FILE *fptr;  FILE is a struct data type defined in stdio.h
  • 7. File pointer: A file pointer is a pointer to FILE data type.  A file pointer is a stream pointer.  A file pointer is a buffer pointer. Syntax: FILE *fp1,*fp2;  A pointer to FILE data type.  File pointer is a pointer to FILE data type is called as a stream pointer or a file pointer.  A file pointer points to the block of information of the stream that has just been opened.  A stream is source or destination of data that may be associated with a disk or other I/O devices.
  • 8. The statement: FILE *fptr1, *fptr2 ; Where fptr1 and fptr2 are pointer variables of type FILE.  They will be assigned the address of a file descriptor, that is, an area of memory that will be associated with an input or output stream.  Whenever you are to read from or write to the file, you must first open the file and assign the address of its file descriptor (or structure) to the file pointer variable.
  • 9. /*Write a program declare, open, read and close use FILES*/ #include<stdio.h> /* writing the data onto the file */ main() { FILE *fptr; /* declare file pointer */ char ch; fptr=fopen("TEXT.DATA", "w"); /* open file for writing */ while((ch=getch())!=EOF) /* read a character from keyboard*/ putc(ch,fptr); /* write a character to file thru pointer */ fclose(fptr); /* close a file */ }
  • 10. /*program to read and write some characters on files */ #include<stdio.h> #include<conio.h> void main() { FILE *fp; char ch; clrscr(); fp=fopen("cds.it","w"); printf("enter some characters on to the file"); while((ch=getchar())!=EOF) fputc(ch,fp); fclose(fp); fp=fopen("cds.it","r"); while((ch=getchar())!=EOF) putchar(ch); fclose(fp); }
  • 11. File operation functions in C Function Name Operation fopen() Creates a new file for use(Opens a new existing file for use) fclose Closes a file which has been opened for use getc() Reads a character from a file putc() Writes a character to a file fprintf() Writes a set of data values to a file fscanf() Reads a set of data values from a file getw() Reads a integer from a file putw() Writes an integer to the file fseek() Sets the position to a desired point in the file ftell() Gives the current position in the file rewind() Sets the position to the beginning of the file
  • 12. Text files: A file that holds text. The term text file is often used as a synonym for ASCII file, a file in which characters are represented by their ASCII codes.  Common functions used for reading from a text stream and used more memory – fgetc() – getc() – fgets() – fscanf()  Common functions used for writing from a text stream used memory. – fputc() – putc() – fputs() – fprintf()
  • 13. Binary files: Files containing non textual information.  A binary file is a file whose content must be interpreted by a program or a hardware processor that understands in advance exactly how it is formatted.  Common functions used for reading from a binary stream and used less memory fgetw() getw()  Common functions used for writing from a binary stream and used less memory fputw() putw()
  • 14. /* Reading and Writing characters in Text files*/ #include<stdio.h> void main() { FILE *fp; char a; fp = fopen("data1","w"); printf("Enter characters into the file , press $ to quitn"); do { scanf("%c",&a); putc(a,fp); }while(a!=‘$'); fclose(fp); fp= fopen("data1","r"); while((a=getc(fp))!='$') printf(" %c",a); fclose(fp); getch(); }
  • 15. Text files: A file that holds text. The term text file is often used as a synonym for ASCII file, a file in which characters are represented by their ASCII codes.  Text Files means collection of characters stores in a file.  File functions are getc() and putc()  In text files characters or integers are readable  New line takes 2 bytes  Each character takes1 byte  Space takes 1 byte  Memory consumption more
  • 16. /* Program to reading and writing characters in text files*/ #include<stdio.h> void main() { FILE *fp; char a; clrscr(); fp = fopen("data1","w"); printf("Enter characters into the file , press $ to quitn"); do { scanf("%c",&a); putc(a,fp); }while(a!=‘$'); fclose(fp); fp= fopen("data1","r"); while((a=getc(fp))!='$') printf(" %c",a); fclose(fp); getch(); }
  • 17. Binary files: Files containing non textual information.  A binary file is a file whose content must be interpreted by a program or a hardware processor that understands in advance exactly how it is formatted.  Binary files means collection of ASCII values stores in a file  File functions are getw() and putw().  In binary files characters or integers are not readable  New line takes zero bytes  Each integer takes 2 bytes  Space takes zero bytes  Memory consumption less.
  • 18. /* Program to Reading and Writing integers in binary files*/ #include<stdio.h> void main() { FILE *fp; int a,i,n; clrscr(); fp = fopen("data1","w"); printf("Enter integers into the file , press -1 to quitn"); do { scanf("%d",&a); putw(a,fp); }while(a!=-1); fclose(fp); fp= fopen("data1","r"); while((a=getw(fp))!=-1) printf(" %d",a); fclose(fp); getch(); }
  • 19. Text Files vs. Binary Files 1. Text Files means collection of characters stores in a file. 2. The I/O stream can be a text stream or binary stream. 3. A text stream is a sequence of text. In a text stream, each line consists of zero or more characters, terminated with new line character. 4. File functions are getc() and putc() 5. In text files characters or integers are readable 6. New line takes 2 bytes 7. Each character takes1 byte 8. Space takes 1 byte 9. Memory consumption more 1. Binary files means collection of ASCII values stores in a file. 2. A binary stream is a sequence of bytes. 3. In a binary stream, it represents raw data without any conversion. For example, the new line character ‘n’ is not mapped to carriage return while using the binary streams. 4. File functions are getw() and putw(). 5. In binary files characters or integers are not readable 6. New line takes zero bytes 7. Each integer takes 2 bytes 8. Space takes zero bytes 9. Memory consumption less.
  • 20. Writing and reading integers: C provides to write integers to the files and read integers from the files.  C provides the following I/O functions 1. putw() – putw function outputs the integer w to the given stream. 2. getw() – getw function returns the next integer in the named input stream. 1. putw() function: putw() – putw function outputs the integer w to the given stream.  putw() neither expects nor causes special alignment in the file.
  • 21.  On success, putw() returns the integer w.  On error, putw() returns EOF Syntax: putw(int w,fptr); Where fptr is a pointer to type FILE. 2. getw() function: getw() – getw function returns the next integer in the named input stream. Syntax: getw(fptr);  It assumes no special alignment in the file.  getw() should not be used when the stream is opened in text mode.  getw() returns the next integer on the input stream.  On end-of-file or error, getw() returns EOF.
  • 22. FILE MODE OPERATIONS “r” - Reading mode “w” - Writing mode “a” - Appending mode “r+” - both reading and writing mode “w+” - both reading and writing mode “a+” - both reading and appending mode “rb” - Same as mode “r”, but for binary file “wb” - Same as mode “w”, but for binary file.
  • 23. /*Wap to read integers from the keyboard and writes them to the file(“INTEGERS.DAT”).*/ #include<stdio.h> #include<stdlib.h> main() { FILE *fp; int i,numb; clrscr(); fp=fopen("INTEGERS.DAT","w"); if(fp==NULL) { printf("Error opening filen"); exit(1); }
  • 24. for(i=0;i<100;i++) { scanf("%d",&numb); if(numb==-99) break; putw(numb,fp); } fclose(fp); fp=fopen("INTEGERS.DAT","r"); if(fp==NULL) { printf("Error opening file"); exit(1); } while((numb=getw(fp))!=EOF) printf(" %d", numb); fclose(fp); getch(); }
  • 25. File Handling Functions  FILE is a structure that holds the description of a file and is defined in stdio.h. 1. Naming a file – Ex. “Text.dat”, “cse.it” 2. Opening a file - fp = fopen(“filename”, ”accessmode”); 3. Reading from or writing into a File fgetc(),fputc() – reading , writing characters fgets(),fputs() – reading , writing strings fread(),fwrite() – reading , writing blocks of data fprintf(),fscanf() – for formatted I/O functions 4. Closing a file: syntax: fclose(fp); 5. fseek(FILE * , int Offset , int origin) – makes the file pointer at a desired location. 6. ftell(fp) – which tells the position of file pointer in the file 7. rewind(fp) – places the file pointer at beginning of the file
  • 26.  Organization of Records in Files: The choice of a proper organization of records in a file is important for the efficiency of real databases. i. Heap – A record can be placed anywhere in the file where there is space. ii. Sequential – Store records in sequential order based on the value of the search key of each record. iii. Hashing – A hash function computed on some attribute of each record, the result specifies in which block of the file the record should be placed. iv. Multitable clustering file organization - Records of several different relations can be stored in the same file and motivation is to store related records on the same block to minimize I/O.  File Organization is two types 1. Sequential File Organization 2. Multitable Clustering File Organization
  • 27. 1. Sequential File Organization: Suitable for applications that require sequential processing of the entire file. The records in the file are ordered by a search-key
  • 28. 2. Multitable Clustering File Organization: Store several relations in one file using a multitable clustering file organization.
  • 29. Files Uses 1. Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two major problems. 2. It becomes cumbersome and time consuming to handle large volumes of data through terminals. 3. The entire data is lost when either the program is terminated or computer is turned off therefore it is necessary to have more flexible approach where data can be stored on the disks and read whenever necessary, without destroying the data, this method employs the concept of files to store data.
  • 30. Data Structures vs. File Structures 1. A Systematic way of organizing and accessing data is called Data Structure. 2. It involves Representation of Data and Operations for accessing data. 3. Data Structures deal with data in main memory. 4. Data structures study how data are stored in a computer so that operations can be implemented efficiently, Conceptual and concrete ways to organize data for efficient storage and manipulation. 5. Data structure requires i. Space for each data item it stores. ii. Time to perform each basic operation. iii. Programming Effort. 1. Data processing from a computer science perspective is Storage of data, Organization of data and Access to data. 2. It involves Representation of Data and Operations for accessing data. 3. File Structures deal with data in secondary storage device (File). 4. Good File Structure Design is Fast access to great capacity, Reduce the number of disk accesses, By collecting data into buffers / blocks or buckets and Manage growth by splitting these collections. 5. A file can be treated as i. a stream of bytes ii. a collection of records with fields (we will discuss it know )