Text files · Fichiers texte
Open, then read or write
- AS topic 10.3 asks for pseudocode that handles a text file of one or more lines.
- Open the file in one mode:
READ,WRITEorAPPEND. WRITEstarts a new file and loses what was there.APPENDadds after what is there.CLOSEFILEwhen you have finished. A file is open in only one mode at a time.
Ouvrir, puis lire ou écrire
- AS sujet 10.3 demande un pseudocode gérant un fichier texte d'une ou plusieurs lignes.
- Ouvrez le fichier dans un mode :
READ,WRITEouAPPEND. WRITEcrée un nouveau fichier et efface ce qui était dedans.APPENDajoute après ce qui existe déjà.- Fermez
CLOSEFILEquand vous avez fini. Un fichier n'est ouvert que dans un mode à la fois.
OPENFILE "Note.txt" FOR WRITE
WRITEFILE "Note.txt", "Hello"
CLOSEFILE "Note.txt"
DECLARE LineOfText : STRING
OPENFILE "Note.txt" FOR READ
READFILE "Note.txt", LineOfText
CLOSEFILE "Note.txt"
OUTPUT LineOfText
EOF tells you when to stop
EOF("Note.txt")isTRUEwhen there is no further line to read.WHILE NOT EOF(...)reads every line.READFILEputs the next line into aSTRING.
EOF indique quand s'arrêter
EOF("Note.txt")est vraiTRUEquand il n'y a plus de ligne à lire.- ⟨
WHILE NOT EOF(...)⟩ lit toutes les lignes.READFILEmet la prochaine ligne dans une chaîneSTRING.
OPENFILE "List.txt" FOR WRITE
WRITEFILE "List.txt", "a"
WRITEFILE "List.txt", "b"
CLOSEFILE "List.txt"
DECLARE LineOfText : STRING
OPENFILE "List.txt" FOR READ
WHILE NOT EOF("List.txt")
READFILE "List.txt", LineOfText
OUTPUT LineOfText
ENDWHILE
CLOSEFILE "List.txt"
Watch out
- Declare the string before
READFILEuses it. - Random files (
OPENFILE … FOR RANDOM) are A2. This lesson does not use them.
Attention
- Déclarez la chaîne avant que
READFILEne l'utilise. - Les fichiers aléatoires (
OPENFILE … FOR RANDOM) sont du programme A2. Cette leçon ne les utilise pas.
Common mistakes
- Read with
READFILEonly afterOPENFILE … FOR READ. WRITEreplaces the file. UseAPPENDto keep the old lines.
Erreurs courantes
- Lisez avec
READFILEseulement après avoir ouvert le fichierOPENFILE … FOR READ. WRITEremplace le fichier. UtilisezAPPENDpour conserver les anciennes lignes.
Now you try
- Write two lines, then read them back until
EOF.
À vous maintenant
- Écrire deux lignes, puis les relire jusqu'à
EOF.
Write Hello to · à Note.txt, read it back into LineOfText, and output it. · Écrivez Hello pour écrire Note.txt, le lire ensuite dans LineOfText, et l'afficher.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
Write a and · et b to · à List.txt, then output both lines using WHILE NOT EOF. · Écrivez a et b vers List.txt, puis affichez les deux lignes en utilisant WHILE NOT EOF.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
Write one to · à Note.txt, close it, APPEND the line two, then read both lines with WHILE NOT EOF and output them. · Écrivez one vers Note.txt, fermez-le, APPEND la ligne two, puis lisez les deux lignes avec WHILE NOT EOF et affichez-les.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.