SlideShare ist ein Scribd-Unternehmen logo
1 von 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 )

Weitere ähnliche Inhalte

Was ist angesagt?

File Organization
File OrganizationFile Organization
File Organization
Manyi Man
 

Was ist angesagt? (20)

Slide 4 dbms users
Slide 4 dbms usersSlide 4 dbms users
Slide 4 dbms users
 
Structure of dbms
Structure of dbmsStructure of dbms
Structure of dbms
 
File organization and indexing
File organization and indexingFile organization and indexing
File organization and indexing
 
Directory structure
Directory structureDirectory structure
Directory structure
 
File Organization
File OrganizationFile Organization
File Organization
 
Distributed operating system
Distributed operating systemDistributed operating system
Distributed operating system
 
Information retrieval 6 ir models
Information retrieval 6 ir modelsInformation retrieval 6 ir models
Information retrieval 6 ir models
 
Database design & Normalization (1NF, 2NF, 3NF)
Database design & Normalization (1NF, 2NF, 3NF)Database design & Normalization (1NF, 2NF, 3NF)
Database design & Normalization (1NF, 2NF, 3NF)
 
File organization
File organizationFile organization
File organization
 
B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
 
File Management in Operating System
File Management in Operating SystemFile Management in Operating System
File Management in Operating System
 
Databases: Normalisation
Databases: NormalisationDatabases: Normalisation
Databases: Normalisation
 
Overview of Storage and Indexing ...
Overview of Storage and Indexing                                             ...Overview of Storage and Indexing                                             ...
Overview of Storage and Indexing ...
 
File organization 1
File organization 1File organization 1
File organization 1
 
File system structure
File system structureFile system structure
File system structure
 
B trees dbms
B trees dbmsB trees dbms
B trees dbms
 
Distributed File Systems
Distributed File Systems Distributed File Systems
Distributed File Systems
 
Normalization
NormalizationNormalization
Normalization
 
FIle Organization.pptx
FIle Organization.pptxFIle Organization.pptx
FIle Organization.pptx
 
Parallel Database
Parallel DatabaseParallel Database
Parallel Database
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Kürzlich hochgeladen

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Kürzlich hochgeladen (20)

Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 

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 )