Intro File2
Intro File2
year 2023-2024.
1
Chapter goal :
2
PLAN
Basic operations
Difference between main memory and secondary memory
Physical and logical files
Physical and logical record
Blocking Factor
Static and dynamic files
Files in C
Conclusion
3
2- Basic operations
4
Example :
To create the Students file, we define its structure and the size of its articles
as follows:
5
2.2 Delete
Deleting a file amounts to canceling its storage, that is to say, erasing all
the records that constitute it, as well as its structure. There are two types
of deletion: logical deletion and physical deletion.
Physical Deletion
the file is destroyed. Therefore, you can’t restore or refer to the deleted
data.
6
2.3 Update
7
2.4 Combine Files
Several sender files give a new file.
8
Example :
10
2.6 Sort File
Sort operation is used to sort a file, arranging the records in a particular
order
Example :
11
2.7 Merge File
It consists of grouping the recordings of two or more files into a single file
Condition: The files to be merged must have the same structure.
Consequence: The resulting file will have the same structure as the receiver
files.
12
Example :
We consider that in a school, students are managed according to the course of study through three
files
13
Example :
14
2.8 Extraction
Print the list of successful students from a Students file containing the
following information: ID - student last name - first name - address -
results.The printed list will only contain the following information:ID
student - student results.
15
2.9 The copy
16
3. Difference between main memory and secondary memory
17
Here are some characteristics of the two types of memory:
Central memory: electronic circuit (VLSI)
Secondary memory: usually a mechanical device (discs, tapes, etc.)
Access time: MC 100,000 to 1,000,000 times faster than magnetic disks
Cost per byte (price) : MC more expensive than MS
Persistence: MC volatile, MS persistent
18
4. Physical and logical files
The logical file is described by its structure (the structure of the records it
contains).
The logical file does not depend on the physical medium that will be used
to store the file.
19
4. Physical and logical files
4.2 Physical File :
20
Fixed-length record: the length of a record is defined by the number of
characters in its structure
21
Variable-length record :
Example : Employee file:
Because the Children field is variable: Employees do not have the same
number of children.
22
5. Physical and logical record
The records in a physical file are known as physical records. The physical
record represents the quantity of information exchanged between the
central memory and the storage unit.
A physical record or Block is the smallest data entity that can be read or
written in a single operation.
23
6. Blocking Factor
The number of records that are stored in a block is called the “blocking
factor”. Blocking factor is constant across blocks if record length is fixed, or
variable otherwise.
24
6. Blocking Factor
GAP: Space to speed up or slow down the read speed of the record.
For a file of 1000 logical records with a blocking factor of 1, 1000 I/Os
are needed to read the entire file, but only 500 I/O operations are needed
if the blocking factor is 2.
25
7. Static and dynamic files
26
7. Static and dynamic files
27
Ouvrir ( f, nomfichier, mode )
to open or create a file with the name 'filename' depending on the value of 'mode'."a":
open an old read/write file and load its characteristics into MC"n": create a new
read/write file and allocate an area in MC for its characteristics.Characteristics
Fermer ( f )
closes the f file, saving its characteristics to disk in the event of modifications.
EcrireDir ( F, i, buf )
write the contents of the 'buf' variable to block number 'i' in the file (logical number)
28
Entete( f, i )
returns the value of the 'i'th characteristic of file 'f' (does not require disk access,
because when the file is opened, its characteristics are already loaded into MC)
AllocBloc( f )
allocates a new block to the file and returns its number. In a file seen as an arrayof
blocks, this operation is not necessary, just write after the end of the file)
29
c) Program C
In the following program, we create a binary file containing a certain number of records
for a simple phone schedule management application:
#include <stdio.h>
struct parm printf(“Entering Items \n");
char nom[20]; do
char tel [20]; {
} printf(« Enter the name:");scanf("%s",p.nom);
printf(« Enter the phone number :");scanf("%s",p.tel);
main(void){ fwrite(&p,1,sizeof(p),output);
struct parm p; printf("Do you want to enter another item (Y/N)?");
FILE *output; rep=getche(); printf("\n");
}
char namefile[21]; while(rep==‘Y' || rep==‘y');
char rep=’\0’;int x; fclose(output);
clrscr(); }
30
c) Program C
Here is an example of a program that sequentially browses the data file and displays
its contents :
#include<stdio.h> while ( ! feof(f) ) {
struct parm{
char nom[20]; n = fread(&p, sizeof(p), 1, f);
char tel [20];
} if ( n == 1 )
main(void){
printf(“name: %s \t phone: %s\n", p.nom, p.tel);
struct parm p;
FILE *f; }
char namefile[21];
int x,n,i;
clrscr(); fclose(f);
printf(« Enter the name of file :");
scanf("%21s",namefile);
f=fopen(namefile,"rb"); }
31
c) Program C
The following program shows an example of direct access (using fseek) to a given
position record:
#include<stdio.h>
struct parm{ printf(« Enter the number of item");
char nom[20]; scanf(" %d", &i);
char tel[20];
} fseek( f, (i-1)*sizeof(e), SEEK_SET );
n = fread(&p, sizeof(p), 1, f);
main(void){ if ( n == 1 )
printf(“record num:%3d \t name : %s \t phone :
struct parm p; %s\n", i, p.nom, p.tel);
FILE *f;
char namefile[21];
int x,n,i; fclose(f);
clrscr();
printf(« Enter the name of file to be }
manipulated :");
scanf("%21s",namefile);
f=fopen(namefile,"rb");
32
c) Exemples C
Text File :
#include <stdio.h>
// Items insertion
int main()
printf(« Enter the name and the phone number or 0 0
{
for finished ) : ");
// variable enregistrement
scanf(" %s %s", e.nom, e.tel);
struct Tenreg {
while ( e.nom[0] != '0' ) {
char nom[20];
fprintf(f, " %s , %s\n", e.nom, e.tel );
char tel[15];
printf(« Enter the name and phone number or 0 0 for
} e;
finished ) : ");
// variable fichier
scanf(" %s %s", e.nom, e.tel);
FILE *f;
}
char nomf[30];
fclose( f );
printf(« Enter the name of file: "); return 0;
scanf(" %s", nomf); }
// creating a new file in text mode
f = fopen( nomf, "w" );
if ( f == NULL ) {
printf(« ERROR \n", nomf);
return 0;
}
33
c) Examples
The following program reads the file sequentially and displays the records on the screen.
Files are very important for the management of any application (stock
management, personnel management, school management) because
they enable all the information required for good management to be
grouped together, i.e. they facilitate updating, control, security,
confidentiality, sharing and archiving.
35
Union/Split/
Merge
update
Sort
Create/
Extraction Delete
/Copy
Op
Abstraite Basic
Machine Acces
Files in C tiime
Diff
Introduction to beetwee
n RAM cost
modelisa
tion
file ET MS
persist
Static and ances
dynamic file
Logocal and
physical file
Logical
record
Physical
record
Blockage
factor
fixed
variable
Mind Mapping
36