salut
je suis faible en pascal.
mon problème est savoir comment stocker dans un fichier texte une liste chainée et la sauvegarder dans le fichier;
merci beaucoup à tous .
Version imprimable
salut
je suis faible en pascal.
mon problème est savoir comment stocker dans un fichier texte une liste chainée et la sauvegarder dans le fichier;
merci beaucoup à tous .
est ce une phrase à la mode sur le forum?Citation:
je suis faible en pascal
Et garder une liste chainée dans un fichier ne releve pas du tout du pascal. Il faut d'abord savoir ce qu'on doit faire (phase papier+crayon+paire de neurones) ensuite on essaie de coder en pascal ce qu'on a retenu comme solution.
merci darrylsite
Bonjour,
La première étape pour toi est de créer une routine qui parcourt simplement la liste chaînée. Tu pourras ensuite t'en servir comme ossature et y greffer le code d'écriture dans le fichier.
As-tu déjà réalisé la première étape ? ;)
Bonsoir;
J'ai essayé mais ce n'est pas résolu.
Voilà mon code :
Enfin merci et bonne nuit.Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 program fichier; uses wincrt; type pointeur=^personne; personne=record nom:string; suivant:pointeur; end; var debut,courant:pointeur; rep:char; f:text; begin assign(f,'c:\wahiba.txt') ; rewrite(f); new(debut); courant:=debut; repeat write('entrez le nom : '); readln (courant^.nom); read (f,courant^.nom); write ('outre element o/n?: '); readln (rep); if rep='o' then begin new(courant^.suivant); courant:=courant^.suivant;end else courant^.suivant:=nil; until rep='n'; close (f); assign (f,'c:\wahiba.txt'); reset (f); while not eof (f) do begin read (f,courant^.nom); writeln (' voila la liste'); writeln('nom ',courant^.nom); courant:=courant^.suivant; end; close (f); end.
Je n'ai pas eu le temps de regarder attentivement ton code mais un problème saute immédiatement aux yeux : pour écrire dans le fichier, c'est write et non read qu'il faut utiliser. ;)
Le programme marche bien. Mais il n'est pas tres propre. Ce sera mieux si tu separais chaque partie du programme : une procedure pour la creation de la liste chainée, une autre procedure pour la sauvegarde des données dans le fichier, une procedure pour restorer la liste à partir du fichier, ...Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 program fichier; uses crt; type pointeur=^personne; personne=record nom:string; suivant:pointeur; end; var debut,courant:pointeur; rep:char; f:text; begin assign(f,'c:\wahiba.txt') ; rewrite(f); new(debut); courant:=debut; repeat write('entrez le nom : '); readln (courant^.nom); write (f,courant^.nom); write ('outre element o/n?: '); readln (rep); if rep='o' then begin new(courant^.suivant); courant:=courant^.suivant;end else courant^.suivant:=nil; until rep='n'; close (f); reset (f); while not eof (f) do begin read (f,courant^.nom); writeln (' voila la liste'); writeln('nom ',courant^.nom); courant:=courant^.suivant; end; close (f); end.
je te donne encore un sacré boulot hein? :aie:
bonjour comment ça va?;
pardon je ne parle pas bien français :oops:
Le programme ne marche pas bien.et je dois faire comment tu dit pour séparer chaque partie dans programme ?
Mais le problème est comment sauvegarder le fichier et séparer l' affichage de la partie sauvegarde.
et voila mon nouveau code:
et merci beaucoup.Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 program fichier; uses WINCRT; type pointeur=^personne; personne=record nom:string; suivant:pointeur; end; var debut,courant:pointeur; rep:char; f:text; choix:integer; nom:string; {---------------------------------------------} procedure cree (nom:string); begin assign(f,'c:\wahiba.txt') ; rewrite(f); new(debut); courant:=debut; repeat write('entrez le nom : '); readln (courant^.nom); write (f,courant^.nom); write ('outre element o/n?: '); readln (rep); if rep='o' then begin new(courant^.suivant); courant:=courant^.suivant;end else courant^.suivant:=nil; until rep='n'; close (f); reset (f); end; {----------------------------------------------} procedure affiche (nom:string); begin while not eof (f) do begin read (f,courant^.nom); writeln (' voila la liste'); writeln('nom ',courant^.nom); courant:=courant^.suivant; end; close (f); end; {------------------------------------------------} begin writeln ('1 : pour cree la liste'); writeln ('2 : pour affiche la liste'); repeat write (' donne le choix: '); readln ( choix); if choix=1 then cree (nom); if choix=2 then affiche (nom); until choix=5 ; end.
j'ai apporté deux ou trois modifications à ton programme.
tu avais oublié de donner une valeur à la variable nom. Il est aussi preferable d' utiliser writeln au lieu de write pour l'ecriture dans le fichier texte dans ton cas.
Pense un peu à bien presenter ton code.Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 program fichier; uses wincrt; type pointeur=^personne; personne=record nom:string; suivant:pointeur; end; var debut,courant:pointeur; rep:char; f:text; choix:integer; nom:string; {---------------------------------------------} procedure cree (nom:string); begin assign(f, nom) ; rewrite(f); new(debut); courant:=debut; repeat write('entrez le nom : '); readln (courant^.nom); writeln (f,courant^.nom); write ('outre element o/n?: '); readln (rep); if rep='o' then begin new(courant^.suivant); courant:=courant^.suivant;end else courant^.suivant:=nil; until rep='n'; close (f); reset (f); end; {----------------------------------------------} procedure affiche (nom:string); begin writeln('voila la liste : '); while not eof (f) do begin readln (f,courant^.nom); writeln(courant^.nom); courant:=courant^.suivant; end; close (f); end; {------------------------------------------------} begin clrscr; nom:='save.txt'; writeln ('1 : pour cree la liste'); writeln ('2 : pour affiche la liste'); repeat write (' donne le choix: '); readln ( choix); if choix=1 then cree (nom); if choix=2 then affiche (nom); until choix=5 ; end.
merci bc
mais la programme ne travaile pas.
Ne voulaient pas executer
et bonsoir.
Nia,
Pour le fun, le code posté par darrylsite remis en forme :
Pour wahiba02 :Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 program fichier; uses wincrt; type pointeur = ^personne; personne = record nom: string; suivant : pointeur; end; var debut, courant : pointeur; rep : char; f : Text; choix : integer; nom : string; {---------------------------------------------} procedure cree(nom : string); begin Assign(f, nom); Rewrite(f); New(debut); courant := debut; repeat Write('entrez le nom : '); Readln(courant^.nom); Writeln(f, courant^.nom); Write ('outre element o/n?: '); Readln(rep); if rep = 'o' then begin New(courant^.suivant); courant := courant^.suivant; end else courant^.suivant := nil; until rep = 'n'; Close(f); Reset(f); end; {----------------------------------------------} procedure affiche(nom : string); begin Writeln('voila la liste : '); while not Eof(f) do begin Readln(f, courant^.nom); Writeln(courant^.nom); courant := courant^.suivant; end; Close(f); end; {------------------------------------------------} begin clrscr; nom := 'save.txt'; Writeln('1 : pour cree la liste'); Writeln('2 : pour affiche la liste'); repeat Write (' donne le choix: '); Readln(choix); if choix = 1 then cree(nom); if choix = 2 then affiche(nom); until choix = 5; end.
Des variables globales en veux-tu, en voilà. À proscrire.
Une procédure/fonction doit recevoir en paramètre toutes les données dont elle a besoin. :)
bonsoir.
le probleme qui reste est ne affiche pas la lasite a executer le programe.
or dans sélecte le choix 2 le programme plouqe.
merci a tout.
Bou,
Là, j'avoue avoir besoin d'un décodeur.
Même sans parler le français parfaitement, un petit effort est nécessaire, au moins pour utiliser les bons mots correctement orthographiés.
La syntaxe est moins importante si les mots sont corrects (enfin, pas toujours, mais pour des phrases simples, c'est généralement vrai).