Text files · Archivos de texto
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 archivo mantiene los datos después de que el programa se detiene
- El plan de estudios 2026–2028 (sección 8.3) almacena datos en un archivo de texto.
- Abres el archivo, lees o escribes un elemento y luego lo cierras.
- Este curso mantiene el archivo en memoria para una única ejecución, para que puedas ver el resultado.
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.
LEER y ESCRIBIR
OPENFILE "name.txt" FOR READlee lo que ya está allí.OPENFILE "name.txt" FOR WRITEcrea un archivo nuevo. Cualquier contenido previo se pierde.READFILEcoloca el próximo elemento en una variable.WRITEFILEalmacena un valor.CLOSEFILEcuando hayas terminado. Un archivo solo puede estar abierto en un modo a la vez.
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.
Leer cada línea
EOF("List.txt")esTRUEcuando no queda más líneas por leer.WHILE NOT EOF(...)lee todo el archivo. CadaREADFILEcoloca la siguiente línea en unSTRING.- Cierra el archivo al terminar. Una apertura posterior podrá volver a utilizarlo.
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.
Ten cuidado
- Declara la variable antes de hacerle un
READFILE. WRITEreemplaza el archivo. Léelo primero si aún necesitas el texto antiguo.
Common mistakes
- Open for
READorWRITE, thenREADFILEorWRITEFILE, thenCLOSEFILE. - The name of the file is a string, in double quotes.
Errores comunes
- Abrir para
READoWRITE, luego usarREADFILEoWRITEFILE, y finalmenteCLOSEFILE. - El nombre del archivo es una cadena de texto, entre comillas dobles.
Now you try
- Write a line, read it back, and
OUTPUTit. - Press Check answer to test it.
Ahora practica
- Escribe una línea, léela de nuevo y hazla
OUTPUT. - Pulsa Check answer para probarlo.
Write the line Hello to · hasta Note.txt, read it back into LineOfText, and output it. · Escribe la línea Hello en Note.txt, léela de nuevo en LineOfText y preséntala.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Store Saved in · hacia adentro FileA.txt, read it into LineOfText, write that same text to FileB.txt, and output it. · Almacena Saved en FileA.txt, léela en LineOfText, escribe ese mismo texto en FileB.txt y preséntala.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Write Cat and · y Dog to · hasta List.txt, then output both lines with WHILE NOT EOF. · Escribe Cat y Dog en List.txt, luego presenta ambas líneas con WHILE NOT EOF.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.