Cobol ANS 74/Fichiers
Fichier séquentiel
modifierENVIRONMENT DIVISION.
...
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FNOM ASSIGN TO (DISK/PRINTER);
ORGANIZATION IS SEQUENTIAL;
ACCESS MODE RANDOM;
FILE STATUS IS FS.
01 ENREG.
02 NM1 PIC X(15).
02 FILLER PIX X(5); VALUE SPACE.
02 NM2 PIX X(20).
DATA DIVISION.
FILE SECTION.
FD FNOM LABEL RECORD STANDARD;
VALUE OF FILE-ID IS "FNOM.DAT".
*OPEN (INPUT/OUTPUT/I-O/EXTEND) FNOM.
*READ FNOM [AT END [INS ... ]].
*WRITE ENREG.
*REWRITE ENREG. - open I-O.
*CLOSE FNOM.
? FACTURE DE GAZ - créer un fichier de consommation de GAZ avec le nom en X(16), adresse X(40), ville X(15) et consommation 9(4)V99. > CRÉER ce fichier en le remplissant et s'il existe déjà, on demande si on veut le détruire (o/n). > FAIRE menu (1. Sortie écran / 2. Rechercher par nom / 3. Ajouter des personnes au fichier / 4. Modifier - smenu (1. nom / 2. adresse / 3. ville / 4. consommation / 0. fin) / 5. Sortie imprimante / 0. fin)
*8 | | | | | | | | | | | | | | | |
IDENTIFICATION DIVISION
PROGRAM-ID. FICHIER-GAZ.
AUTHOR. AUT.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. CPU1.
OBJECT-COMPUTER. CPU2.
SPECIAL-NAMES.
* DECIMAL-POINT IS COMMA.
CURRENCY SIGN IS "E".
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FGAZ ASSIGN TO DISK;
FILE STATUS IS FS.
DATA DIVISION.
FILE SECTION.
FD FGAZ LABEL RECORD STANDARD;
VALUE OF FILE-ID IS "FGAZ.DAT";
RECORD CONTAINS 77.
01 FENREG.
02 NOM PIC X(16).
02 ADR PIC X(40).
02 VIL PIC X(15).
02 CNS PIC 9(4)V99.
WORKING-STORAGE SECTION.
77 FS PIC XX.
88 FS-OK VALUE "00".
88 FS-KO VALUE "10".
77 CH PIC X.
88 CH-OK VALUES "o", "O".
88 CH-KO VALUES "n", "N".
77 NMF PIC X(10).
77 NMR PIC X(16).
77 CHM PIC 9.
77 EOF PIC 9; VALUE 0.
*EDITION
77 CONS-ED PIC Z(3)9.99.
PROCEDURE DIVISION.
*structure principale
PROG.
PERFORM D-PROG.
IF FS-OK PERFORM FOK
ELSE PERFORM CREER.
PERFORM I1-PROG.
PERFORM MENU UNTIL CHM = 0.
PERFORM F-PROG.
STOP RUN.
FOK.
PERFORM D-FOK.
PERFORM CHK1 UNTIL CH-OK OR CH-KO.
PERFORM I2-FOK.
IF CH-OK PERFORM CREER
ELSE PERFORM DMY.
PERFORM F-FOK.
MENU.
PERFORM D-MENU.
IF CHM = 1 PERFORM AFF
ELSE IF CHM = 2 PERFORM RCH-STRT
ELSE IF CHM = 3 PERFORM AJO
ELSE IF CHM = 4 PERFORM MOD
ELSE IF CHM = 5 PERFORM IMP
ELSE IF CHM = 0 PERFORM FIN
ELSE PERFORM ERR.
PERFORM F-MENU.
*traitement principal
D-PROG.
DISPLAY ECRAN.
OPEN INPUT FGAZ.
CLOSE FGAZ.
D-FOK.
DISPLAY "RECREER FICHIER ? (O/N)".
ACCEPT CH.
CHK1.
ACCEPT CH.
I2-FOK.
EXIT.
CREER.
OPEN OUTPUT FGAZ.
CLOSE FGAZ.
DMY.
EXIT.
F-FOK.
EXIT.
I1-PROG.
PERFORM SCR1.
ACCEPT CHM.
D-MENU.
EXIT.
F-MENU.
PERFORM SCR1.
ACCEPT CHM.
F-PROG.
EXIT.
AFF.
*----------
PERFORM D-AFF.
PERFORM AFF1 UNTIL EOF = 1.
PERFORM F-AFF.
D-AFF.
DISPLAY (1, 1) ERASE.
DISPLAY "NOM---------------------------------------------------------------------------".
OPEN INPUT FGAZ.
READ FGAZ AT END MOVE 1 TO EOF.
AFF1.
DISPLAY FENREG.
READ FGAZ AT END MOVE 1 TO EOF.
F-AFF.
DISPLAY "------------------------------------------------------------------------------------".
CLOSE FGAZ.
MOVE 0 TO EOF.
STOP "APPUYEZ SUR UNE TOUCHE POUR CONTINUER".
RCH-STRT.
*----------
OPEN INPUT FGAZ.
PERFORM RCH-NM UNTIL NMR > 2.
PERFORM RCH UNTIL EOF = 1 OR NMR = NOM.
PERFORM RCH-ENR THRU RCH-STOP.
RCH-NM.
*----------
DISPLAY "NOM A RECHERCHER ?".
ACCEPT NMR.
RCH.
*----------
READ FGAZ AT END MOVE 1 TO EOF.
RCH-ENR.
*----------
IF NMR = NOM DISPLAY FENREG
ELSE DISPLAY "ENREGISTREMENT INCONNU".
MOVE 0 TO NMR.
RCH-STOP.
*----------
CLOSE FGAZ.
MOVE 0 TO EOF.
STOP "APPUYER SUR UNE TOUCHE".
AJO.
*----------
PERFORM D-AJO.
PERFORM AJO1 UNTIL CH-KO.
PERFORM F-AJO.
D-AJO.
MOVE "o" TO CH.
DISPLAY (1, 1) ERASE.
DISPLAY "NOUVEL ENREG".
OPEN EXTEND FGAZ.
AJO1.
DISPLAY "nom adr vil cns ?".
ACCEPT NOM.
ACCEPT ADR.
ACCEPT VIL.
ACCEPT CNS.
WRITE FENREG.
DISPLAY "encore (o/n) ?".
ACCEPT CH.
F-AJO.
CLOSE FGAZ.
MOD.
*----------
PERFORM D-MOD.
PERFORM MOD1 UNTIL NMR = "*".
PERFORM F-MOD.
MOD1.
PERFORM D-MOD1.
PERFORM RCH UNTIL NOM = NMR OR EOF = 1.
PERFORM I-MOD1.
IF EOF = 0 PERFORM MOK
ELSE PERFORM MKO.
PERFORM F-MOD1.
MOK.
PERFORM D-MOK.
PERFORM MMOD UNTIL CHM = 0.
PERFORM F-MOK.
MMOD.
PERFORM D-MMOD.
IF CHM = 1 PERFORM M1
ELSE IF CHM = 2 PERFORM M2
ELSE IF CHM = 3 PERFORM M3
ELSE IF CHM = 4 PERFORM M4
ELSE PERFORM ERR.
PERFORM F-MMOD.
D-MOD.
DISPLAY (1, 1) ERASE.
DISPLAY "NOM A RECHERCHER ? (* pour aucun)".
ACCEPT NMR.
D-MOD1.
OPEN I-O FGAZ.
I-MOD1.
EXIT.
D-MOK.
PERFORM SCR2.
DISPLAY "CHOIX ? 1-4".
ACCEPT CHM.
D-MMOD.
EXIT.
M1.
DISPLAY NOM.
DISPLAY "NOUVEAU NOM ?".
ACCEPT NOM.
M2.
DISPLAY ADR.
DISPLAY "NOUVELLE ADRESSE ?".
ACCEPT ADR.
M3.
DISPLAY VIL.
DISPLAY "NOUVELLE VILLE ?".
ACCEPT VIL.
M4.
DISPLAY CNS.
DISPLAY "NOUVELLE CONSOMMATION ?".
ACCEPT CNS.
F-MMOD.
PERFORM D-MOK.
F-MOK.
DISPLAY "ENREGISTREMENT"
REWRITE FENREG.
MKO.
DISPLAY "NOM INCONNU".
STOP "APPUYEZ UE TOUCHE".
F-MOD1.
CLOSE FGAZ.
MOVE 0 TO EOF.
PERFORM D-MOD.
F-MOD.
EXIT.
IMP.
*----------
EXIT.
FIN.
*----------
EXIT.
ERR.
*----------
DISPLAY "CHOIX INCORRECT !".
*MENUS
*----------------------------------------
SCR1.
DISPLAY (1, 1) ERASE.
DISPLAY "MENU PRINCIPAL".
DISPLAY "--------------".
DISPLAY "1 AFFICHER TOUT".
DISPLAY "2 RECHERCHER NOM (*=STOP)".
DISPLAY "3 AJOUTER ENREG".
DISPLAY "4 MODIFIER ENREG".
DISPLAY "5 IMPRIMER FICHIER".
DISPLAY "0 QUITTER".
SCR2.
DISPLAY (1, 1) ERASE.
DISPLAY "MENU MODIFIER".
DISPLAY "-------------".
DISPLAY "1. NOM".
DISPLAY "2. ADRESSE".
DISPLAY "3. VILLE".
DISPLAY "4. CONSOMMATION".
DISPLAY "0. QUITTER".
? Rajouter une option d'impression de liste de consommation par client avec numéro en bas de page
+ rajoutés dans ENVIRONMENT et DATA DIVISION.
8 | | | | | | | | | | | | | | | |
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
SELECT FIMP ASSIGN TO PRINTER.
DATA DIVISION.
FILE-SECTION.
FD FIMP LABEL RECORD OMITTED;
LINAGE IS 25 WITH FOOTING AT 23;
LINES TOP 2; LINES BOTTOM 2.
01 LIGNE PIC X(80).
WORKING-STORAGE SECTION.
01 TITRE.
02 FILLER PIC X(3); VALUE "NOM".
02 FILLER PIC X(35); VALUE SPACE.
02 FILLER PIC X(12); VALUE "CONSOMMATION".
01 SOUL.
02 FILLER PIC X(3); VALUE ALL "-".
02 FILLER PIC X(35); VALUE SPACE.
02 FILLER PIC X(12). VALUE ALL "-".
01 ENR-IMP.
02 NOMI PIC X(30).
02 FILLER PIC X(7); VALUE SPACE.
02 CONSI PIC ZZZ9.99.
01 BAS-PAGE.
02 FILLER PIC X(30). VALUE SPACE.
02 FILLER PIC X(5). VALUE "PAGE ".
02 NUM PIC 99.
77 BLANCHE PIC X; VALUE SPACE.
77 NP PIC 9.
77 SAUT PIC 99.
PROCEDURE DIVISION.
*STRUCTURE
IMPRESSION.
PERFORM D-IMPRESSION.
IF EOF = 0 PERFORM IOK
ELSE PERFORM IKO.
PERFORM F-IMPRESSION.
IOK.
PERFORM D-IOK.
PERFORM IMP UNTIL EOF = 1.
PERFORM F-IOK.
IMP.
PERFORM D-IMP.
IF NP = 1 PERFORM NEWPAGE
ELSE PERFORM MEMPAGE.
PERFORM F-IMP.
*TRAITEMENTS
D-IMPRESSION.
OPEN INPUT FGAZ.
MOVE 0 TO EOF.
READ FGAZ AT END MOVE 1 TO EOF.
D-IOK.
OPEN OUTPUT FIMP.
MOVE " LISTE DES CLIENTS" TO LIGNE.
WRITE LIGNE BEFORE 2.
WRITE LIGNE FROM TITRE BEFORE 1.
WRITE LIGNE FROM SOUL BEFORE 2.
MOVE 0 TO NP.
MOVE 1 TO NUM.
D-IMP.
MOVE CORR ENR-GAZ TO ENR-IMP.
MEMPAGE.
WRITE LIGNE FROM ENR-IMP BEFORE 1;
AT EOP MOVE 1 TO NP.
NEWPAGE.
WRITE LIGNE FROM BLANCHE BEFORE 1.
WRITE LIGNE FROM BAS-PAGE BEFORE PAGE.
WRITE LIGNE FROM TITRE BEFORE 1.
WRITE LIGNE FROM SOUL BEFORE 2.
* WRITE LIGNE FROM ENR-IMP BEFORE 1.
MOVE 0 TO NP.
ADD 1 TO NUM.
F-IMP.
READ FGAZ AT END MOVE 1 TO EOF.
F-IOK.
SUBTRACT LINAGE-COUNTER FROM 25 GIVING SAUT.
WRITE LIGNE FROM BAS-PAGE AFTER SAUT.
CLOSE FIMP.
F-IKO.
EXIT.
F-IMPRESSION.
CLOSE FGAZ.
fichier indexe
modifier- OPEN (INPUT/ I-O/ OUTPUT) FICHIER - active CP
- START FIC KEY IS ((>/ not</ =) nom); INVALID KEY...
- seq.: Act et pointe CP pour lecture séq.
- rand:
START - dyn.: Tous sens.
- READ
- seq.: READ ... [AT END ...]; INVALID KEY ...
- ran.: READ ... KEY IS ...; INVALID KEY ...
- dyn.: direct / READ ... KEY IS ...; INVALID KEY ...
- dyn.: sequen / READ ... NEXT [AT END ...]
- REWRITE ... ; INVALID KEY ... - modif tt sauf CP
- DELETE FNOM; INVALID KEY ... - charge CP et eff() ENREG
- WRITE ENREG; INVALID KEY ... - I-O (ajouter), OUTPUT (écrire).
- CLOSE FNOM
exercices
modifierSoit le fichier suivant // (CP-ClePrimaire) est NUM-PROD et (CleAlternative) est DES-PROD
*8 | | | | | | | | | | | | | | | |
ENVIRONMENT DIVISION
...
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FPROD ASIGN TO DISK;
ORGANIZATION INDEXED;
ACCESS MODE ...
RECORD KEY IS NUM-PROD;
ALTERNATE RECORD DES-PROD DUPLICATES;
FILE STATUS IS STAT-PROD.
DATA DIVISION.
FILE SECTION.
FD FPROD LABEL RECORD STANDARD;
VALUE OF FILE-ID IS "FPROD.DAT".
01 ENR-PROD.
02 NUM-PROD.
03 FAM-PROD PIC XX.
03 FILLER PIC X(4).
02 NOM-PROD.
03 DES-PROD PIC X(5).
03 FILLER PIC X(10).
02 INFO PIC XX.
WORKING-STORAGE SECTION.
77 STAT-PROD PIC XX.
? 1/ Faire programme pour CREER, REMPLIR, AFFICHER, MODIFIER, EFFACER dans ce fichier
- acces sequentiel
- ? 2/ En acces sequentiel, afficher tout le fichier
*8 | | | | | | | | | | | | | | | |
PROCEDURE DIVISION.
*STRUCTURE
PROG.
PERFORM D-PROG.
PERFORM AFF UNTIL EOF=1.
PERFORM F-PROG.
*TRAITEMENTS
D-PROG.
OPEN INPUT FPROD.
MOVE 0 TO EOF.
READ FPROD AT END MOVE 1 TO EOF.
AFF.
DISPLAY ENR-PROD.
READ FPROD AT END MOVE 1 TO EOF.
F-PROD.
CLOSE FPROD.
- ? 3/ Afficher tous les produits dont le nom commence par B
D'abord en dynamique puis s'il est dedans la suite du fichier en sequentiel.
*8 | | | | | | | | | | | | | | | |
PROCEDURE DIVISION.
*STRUCTURE
PROG.
PERFORM D-PROG.
IF FLAG = 0 PERFORM OK
ELSE PERFORM KO.
PERFORM F-PROG.
OK.
PERFORM D-OK.
PERFORM AFF UNTIL DES-PROD > "BZZZZ".
PERFORM F-OK.
*TRAITEMENTS
D-PROG.
OPEN INPUT FPROD.
MOVE "B" TO DES-PROD.
START FPROD KEY IS NOT < DES-PROD;
INVALID KEY MOVE 1 TO FLAG.
D-OK.
MOVE 0 TO EOF.
READ FPROD.
AFF.
DISPLAY ENR-PROD.
READ FPROD AT END MOVE 1 TO EOF.
F-OK.
EXIT.
KO.
DISPLAY "PAS DANS FICHIER".
F-PROG.
CLOSE FPROD.
- acces random
- ? 4/ Entrer un numero de produit et l'afficher
8 | | | | | | | | | | | | | | | |
*STRUCT
RECHERCHE.
PERFORM I1-PROG.
IF FLAG = 0 PERFORM OK
ELSE PERFORM KO.
PERFORM I2-PROG.
*TRAITEMENT
I1-PROG.
OPEN INPUT FPROD.
DISPLAY "NUM ?".
ACCEPT NUM-PROD.
MOVE 0 TO FLAG.
READ FPROD; INVALID KEY MOVE 1 TO FLAG.
OK.
DISPLAY ENR-PROD.
KO.
DISPLAY NUM-PROD, " INEXACT".
I2-PROG.
CLOSE FPROD.
- acces dynamique
- ? 5/ En dynamic, affichet tous les produits dont le nom est "rivet"
Arbre programmatique
0 (PROG) -c01- 1 [I2-] (AKO) (AOK) [I1-] 2 [F-] (AFF) [D-] -c02> CONDITIONS: c01 : {FLAG = 0 / STAT-PROD <> "23"} c02 : DES-PROD <> "rivet" OR {EOF = 1/STAT-PROD = "10"}
Traitements
8 | | | | | | | | | | | | | | | |
*STRUCTURE
...
*TRAITEMENTS
I1-PROG.
OPEN INPUT FPROD.
MOVE "RIVET" TO DES-PROD
READ FPROD KEY IS DES-PROD.
D-AOK.
MOVE 0 TO EOF.
AFF.
DISPLAY ENR-PROD.
READ FPROD NEXT; INVALID KEY MOVE 1 TO EOF.
* pas de invalid key avec file status
F-AOK.
EXIT.
AKO.
DISPLAY "NON TROUVE".
I2-PROG.
CLOSE FPROD.
- ? 6/ Faire les 5 exercices precedents en acces dynamique
- ? 7/ Afficher le 1er produit sans faire OPEN à partir de n'importe ou
- ? 8/ Pour tous les produits de la famille K2, si l'info est AA, elle devient XX. Si l'info est BB, on supprime le produit.
- ? 9/ Afficher tous les produits à partir du produit H51025. (En 2 versions // Toujours le faire / Le faire uniquement s'il est dedans)
fichier relatif
modifier- SELECT
SELECT FICHIER ORGANISATION RELATIVE; ACCESS MODE [sequential/random/dynamic]; RELATIVE KEY IS [VAR]; STATUS IS [VAR].
- OPEN
OPEN {INPUT/OUTPUT/I-O} [FICHIER]
- START
START [FNOM] KEY IS {=/>/NOT <} clé-relative; [INVALID KEY]
- READ
- sequentiel : READ ... AT END ...
- random : READ ... INVALID KEY ...
- dynamic :
- seq : READ ... NEXT AT END ...
- dir : READ ... INVALID KEY ...
- REWRITE [ENREG] INVALID KEY ...
- En random ecrit en fonction de la cle.
- En dynamique ecrit le dernier lu.
- DELETE FNOM INVALID KEY ...
- En dynamique efface à la clé.
- En seqentiel efface le dernier lu.
- WRITE [ENREG]; INVALID KEY ...
- I-O
- Pas en séquentiel.
- Random et dynamique, en fonction de clé
- OUTPUT
- Crée dans ordre croissant de la clé.
files status
modifier- Chaque entrée/sortie fichier génère un status.
FS | I/O | ORGANISATION | MODE | SIGNIFICATION |
00 | All | All | All | Successful |
02 | REWRITE WRITE | Ind | All | Created duplicate alternate key |
02 | READ | Ind | All | Detected alternate duplicate key |
04 | READ | All | All | Record not size of user's buffer |
05 | OPEN | All | All | Optional file not present |
07 | CLOSE OPEN | All | All | Invalid file organization or device |
10 | READ | All | Seq | No next logical record or option file not present (at end) |
14 | READ | Rel | All | Relative record number too large |
21 | REWRITE | Ind | Seq | Primary key changed after READ |
21 | WRITE | Ind | Seq | Attempted nonascending key value (invalid key) |
22 | REWRITE | Ind | All | Duplicate alternate key (invalid key) |
22 | WRITE | Ind, Rel | Ran | Duplicate key (invalid key) |
23 | DELETE READ REWRITE START | Ind, Rel | Ran | Record not in file; optional file not present (invalid key) |
24 | WRITE | Ind, Rel | All | Boundary violation or relative record number too large (invalid key) |
30 | All | All | All | All other permanent errors |
34 | WRITE | Seq | Seq | Boundary violation |
35 | OPEN | All | All | File not found |
37 | OPEN | All | All | Inappropriate device type |
38 | OPEN | All | All | File previously closed with lock |
39 | OPEN | All | All | Conflict of file attributes |
41 | OPEN | All | All | File already opened |
42 | CLOSE | All | All | File not opened |
43 | DELETE REWRITE | All | Seq | No previous READ or START |
44 | REWRITE WRITE | All | All | Invalid record size |
46 | READ | All | Seq | No valid next record (at end) |
47 | READ START | All | All | File not open, or incompatible open mode |
48 | WRITE | All | All | File not open, or incompatible open mode |
49 | DELETE REWRITE | All | All | File not open, or incompatible open mode |
90 | All | All | All | Record locked by another user (record available) |
91 | OPEN | All | All | Open is unsuccessful; file locked by another access stream |
92 | DELETE READ REWRITE START WRITE | All | All | Record locked by another user (record not available) |
93 | UNLOCK | All | All | No current record |
94 | UNLOCK | All | All | File not open, or incompatible open mode |
95 | OPEN | All | All | No file space on device |