Text files · Fichiers texte
A file keeps data after the program stops
- The 2026–2028 syllabus (section 8.3) stores data in a text file.
- You open the file, read or write one item, then close it.
- This course keeps the file in memory for the one run, so you can see the result.
Un fichier conserve les données après l'arrêt du programme
- Le programme 2026–2028 (section 8.3) stocke les données dans un fichier texte.
- Vous ouvrez le fichier, lisez ou écrivez un élément, puis le fermez.
- Ce cours garde le fichier en mémoire pour cette exécution unique, afin que vous puissiez voir le résultat.
DECLARE LineOfText : STRING
OPENFILE "Note.txt" FOR WRITE
WRITEFILE "Note.txt", "Hello"
CLOSEFILE "Note.txt"
OPENFILE "Note.txt" FOR READ
READFILE "Note.txt", LineOfText
CLOSEFILE "Note.txt"
OUTPUT LineOfText
READ and WRITE
OPENFILE "name.txt" FOR READreads what is already there.OPENFILE "name.txt" FOR WRITEstarts a new file. Anything already in it is lost.READFILEputs the next item into a variable.WRITEFILEstores a value.CLOSEFILEwhen you have finished. A file is open in only one mode at a time.
READ et WRITE
OPENFILE "name.txt" FOR READlit ce qui y est déjà présent.OPENFILE "name.txt" FOR WRITEcrée un nouveau fichier. Tout ce qui était dedant est perdu.READFILEmet le prochain élément dans une variable.WRITEFILEstocke une valeur.- Fermez
CLOSEFILEquand vous avez fini. Un fichier n'est ouvert que dans un mode à la fois.
DECLARE LineOfText : STRING
OPENFILE "FileA.txt" FOR WRITE
WRITEFILE "FileA.txt", "Saved"
CLOSEFILE "FileA.txt"
OPENFILE "FileA.txt" FOR READ
READFILE "FileA.txt", LineOfText
CLOSEFILE "FileA.txt"
OPENFILE "FileB.txt" FOR WRITE
WRITEFILE "FileB.txt", LineOfText
CLOSEFILE "FileB.txt"
OUTPUT LineOfText
Read every line
EOF("List.txt")isTRUEwhen there is no further line to read.WHILE NOT EOF(...)reads the whole file. EachREADFILEputs the next line into aSTRING.- Close the file when you finish. A later open can use it again.
Lire chaque ligne
EOF("List.txt")est vraiTRUEquand il n'y a plus de ligne à lire.WHILE NOT EOF(...)lit tout le fichier. ChaqueREADFILEmet la prochaine ligne dans unSTRING.- Fermez le fichier quand vous avez fini. Une ouverture ultérieure peut l'utiliser à nouveau.
DECLARE LineOfText : STRING
OPENFILE "List.txt" FOR WRITE
WRITEFILE "List.txt", "Red"
WRITEFILE "List.txt", "Blue"
CLOSEFILE "List.txt"
OPENFILE "List.txt" FOR READ
WHILE NOT EOF("List.txt") DO
READFILE "List.txt", LineOfText
OUTPUT LineOfText
ENDWHILE
CLOSEFILE "List.txt"
Watch out
- Declare the variable before you
READFILEinto it. WRITEreplaces the file. Read it first if you still need the old text.
Attention
- Déclarez la variable avant de
READFILEdans celle-ci. WRITEremplace le fichier. Lisez-le d'abord si vous avez encore besoin de l'ancien texte.
Common mistakes
- Open for
READorWRITE, thenREADFILEorWRITEFILE, thenCLOSEFILE. - The name of the file is a string, in double quotes.
Erreurs courantes
- Ouvrez pour
READouWRITE, puisREADFILEouWRITEFILE, puisCLOSEFILE. - Le nom du fichier est une chaîne, entre guillemets doubles.
Now you try
- Write a line, read it back, and
OUTPUTit. - Press Check answer to test it.
À vous maintenant
- Écrivez une ligne, relisez-la, et
OUTPUT-la. - Appuyez sur Vérifier la réponse pour tester.
Write the line Hello to · à Note.txt, read it back into LineOfText, and output it. · Écrire la ligne Hello vers Note.txt, la relire dans LineOfText, et l'afficher.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
Store Saved in FileA.txt, read it into LineOfText, write that same text to FileB.txt, and output it. · Stockez Saved dans FileA.txt, lisez-le dans LineOfText, écrivez ce même texte vers FileB.txt, et affichez-le.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
Write Cat and · et Dog to · à List.txt, then output both lines with WHILE NOT EOF. · Écrire Cat et Dog vers List.txt, puis afficher les deux lignes avec WHILE NOT EOF.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.