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);
}
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 )