Text files · Archivos de texto
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.
Abrir, luego leer o escribir
- El tema 10.3 pide pseudocódigo que maneje un archivo de texto de una o más líneas.
- Abra el archivo en un modo:
READ,WRITEoAPPEND. WRITEinicia un nuevo archivo y pierde lo que había.APPENDagrega después de lo que hay.CLOSEFILEcuando haya terminado. Un archivo está abierto en un solo modo a la vez.
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 le indica cuándo detenerse
EOF("Note.txt")esTRUEcuando no queda más línea para leer.WHILE NOT EOF(...)lee cada línea.READFILEcoloca la siguiente línea en unaSTRING.
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.
Tenga cuidado
- Declare la cadena antes de que
READFILEla use. - Los archivos aleatorios (
OPENFILE … FOR RANDOM) son A2. Esta lección no los utiliza.
Common mistakes
- Read with
READFILEonly afterOPENFILE … FOR READ. WRITEreplaces the file. UseAPPENDto keep the old lines.
Errores comunes
- Lea con
READFILEsolo después deOPENFILE … FOR READ. WRITEreemplaza el archivo. UseAPPENDpara conservar las líneas antiguas.
Now you try
- Write two lines, then read them back until
EOF.
Ahora intente
- Escriba dos líneas, luego léalas nuevamente hasta que ⟨
EOF⟩.
Write Hello to · hasta Note.txt, read it back into LineOfText, and output it. · Escribe Hello en Note.txt, léelo de nuevo en LineOfText y muéstralo.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Write a and · y b to · hasta List.txt, then output both lines using WHILE NOT EOF. · Escribe a y b en List.txt, luego muestra ambas líneas usando WHILE NOT EOF.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Write one to · hasta Note.txt, close it, APPEND the line two, then read both lines with WHILE NOT EOF and output them. · Escribe one en Note.txt, ciérralo, APPEND la línea two, luego lee ambas líneas con WHILE NOT EOF y muéstralas.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.