0% found this document useful (0 votes)
13 views

Intro File2

The document provides an introduction to file management concepts, including basic operations such as creating, deleting, updating, and combining files. It distinguishes between main memory and secondary memory, as well as physical and logical files and records. The document also includes examples of file operations in C programming and emphasizes the importance of files in managing information for various applications.

Uploaded by

aymen Beskri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Intro File2

The document provides an introduction to file management concepts, including basic operations such as creating, deleting, updating, and combining files. It distinguishes between main memory and secondary memory, as well as physical and logical files and records. The document also includes examples of file operations in C programming and emphasizes the importance of files in managing information for various applications.

Uploaded by

aymen Beskri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Introduction to files

Presented by Pr. Nabil KESKES

year 2023-2024.

1
Chapter goal :

Master the terminology and fundamental concepts of files.

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

2.1 Creating a file


Create your structure
Enter items from the file and store them on magnetic (optical) media.

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.

Logical deletion is the process of not actually deleting the data,


but setting a column called “flag” to make it appear as if the data has been
deleted to the user.

Physical Deletion
the file is destroyed. Therefore, you can’t restore or refer to the deleted
data.
6
2.3 Update

The update includes the following three treatments:


- Creating new records
- Deleting existing records
- Modifying the content of a record

7
2.4 Combine Files
Several sender files give a new file.

8
Example :

In the sender files, we have alphanumeric information "Employee


address" (for example: chorfa 10190 bouira).
In the new file, we need to carry out processing that depends on the
wilaya. In this case, we will split the "Address" field into sub-fields (City,
Postcode, Wilaya) as follows:
Ville : Chorfa
Code postal : 10190
Wilaya : Bouira
The information remains the same, only the way it is represented
differs.
9
2.5 Split File
It is the opposite operation of the Combine. A sender file gives to several
receiver files.

10
2.6 Sort File
Sort operation is used to sort a file, arranging the records in a particular
order
Example :

Consider the Students file containing the following information:


(Student number, student surname, student first name, student
course).The simplest way to sort this file is to choose the key (student
number) as the sort argument.

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

This processing consists of extracting or copying recordings or parts of


recordings onto another medium according to a given criterion.
Example :

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

Copying a file means duplicating its content on a medium. There may be


various reasons for doing this:
 Enable faster access times.

 Guarantee greater reliability to avoid loss of information.

16
3. Difference between main memory and secondary memory

Computers have two fundamental types of memory — main memory and


secondary storage. Main memory is where data and instructions are stored
so that they can be accessed directly by the CPU. Secondary storage is used
to permanently store data such as the operating system and the user's files.

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

4.1 Logical file :

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 :

The physical file is the result of storing a logical file on a physical


medium. The physical file is defined by its content and its physical
medium.

5. Physical and logical record

5.1 Logical record:

The records in a logical file are called logical records.


The size of a record is measured in bytes or characters; it can be fixed,
variable.

20
Fixed-length record: the length of a record is defined by the number of
characters in its structure

Example: Student file


record of fixed length 50 characters
8c NCE
20c Last name
20c first name
2c section

21
Variable-length record :
Example : Employee file:

variable-length record (CNSS, last name, First name, Grade, Position,


Children, Marital status)

Because the Children field is variable: Employees do not have the same
number of children.

Solution: create a file for children with the following structure:

Children (CNSS, First Name, DN)


1 ALI 01/01/1985
1 HEDIA 31/12/1987
1 MOSTAPHA 03/05/2000
2 JIHENE 04/06/2001

22
5. Physical and logical record

5.2 Physical recording

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.

Case of a magnetic tape

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

The MS is modelled as a contiguous area of sequentially numbered


blocks (these numbers represent block addresses).Blocks are
contiguous areas of bytes of the same size, containing, among other
things, the data(records). In the following schema ram, the MS contains
3 files E, F and G

26
7. Static and dynamic files

To write algorithms on file structures we will use the abstract machine


defined by the following model:

{ouvrir, fermer, lireDir, ecrireDir, lireSeq, ecrireSeq, aff_entete, entete,


allocbloc }
A file is therefore a set of logically numbered blocks (1, 2, 3, ... n).

Declaration of a 'f' file


var
f : FICHIER de TypeBloc BUFFER buf1, buf2 ENTETE (type1, type2,
...typem);
:

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.

LireDir (F, i, buf )


read the contents of block number 'i' from file 'f' (logical number of blocks) from
buffer 'buf'.

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

printf(« Enter the neme of file :");


scanf("%21s",namefile);
output=fopen(namefile,"wb");

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.

#include <stdio.h> // Items reading


int main()
{ fscanf(f, "%s , %s", e.nom, e.tel);
// variable enregistrement printf("\t name : %s \t phone number : %s\n", e.nom,
struct Tenreg { e.tel);
char nom[20]; while ( ! feof(f) ) {
char tel[15]; fscanf(f, "%s , %s", e.nom, e.tel);
} e; printf("\t name : %s \t phone number : %s\n", e.nom,
// variable fichier e.tel);
FILE *f; }
char nomf[30]; // File closing
int n, i; fclose( f );
printf(« Enter the name of file : "); return 0;
scanf(" %s", nomf); }
// open the file in text mode
f = fopen( nomf, "r" );
if ( f == NULL ) {
printf(« ERROR \n", nomf);
return 0;
}
34
4. Conclusion

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

You might also like