Text files · Arquivos 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.
Um arquivo mantém dados após o programa parar
- O currículo 2026–2028 (seção 8.3) armazena dados em um arquivo de texto.
- Você abre o arquivo, lê ou grava um item, depois fecha-o.
- Este curso mantém o arquivo na memória para a única execução, para que você possa ver o 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.
READ e WRITE
OPENFILE "name.txt" FOR READlê o que já está lá.OPENFILE "name.txt" FOR WRITEinicia um novo arquivo. Tudo o que já estava nele é perdido.READFILEcoloca o próximo item em uma variável.WRITEFILEarmazena um valor.CLOSEFILEquando terminar. Um arquivo está aberto em apenas um modo por 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.
Leia cada linha
EOF("List.txt")éTRUEquando não há mais nenhuma linha para ler.WHILE NOT EOF(...)lê todo o arquivo. CadaREADFILEcoloca a próxima linha em umSTRING.- Feche o arquivo quando terminar. Uma abertura posterior pode usá-lo novamente.
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.
Atenção
- Declare a variável antes de
READFILEnela. WRITEsubstitui o arquivo. Leia-o primeiro se ainda precisar do texto antigo.
Common mistakes
- Open for
READorWRITE, thenREADFILEorWRITEFILE, thenCLOSEFILE. - The name of the file is a string, in double quotes.
Erros comuns
- Abra para
READouWRITE, depoisREADFILEouWRITEFILE, depoisCLOSEFILE. - O nome do arquivo é uma string, entre aspas duplas.
Now you try
- Write a line, read it back, and
OUTPUTit. - Press Check answer to test it.
Agora você tenta
- Grave uma linha, leia-a de volta e
OUTPUTela. - Pressione Check answer para testá-lo.
Write the line Hello to · até Note.txt, read it back into LineOfText, and output it. · Escreva a linha Hello em Note.txt, leia-a de volta para LineOfText, e saia com ela.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Store Saved in FileA.txt, read it into LineOfText, write that same text to FileB.txt, and output it. · Armazene Saved em FileA.txt, leia-a para LineOfText, escreva esse mesmo texto em FileB.txt, e saia com ele.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Write Cat and · e Dog to · até List.txt, then output both lines with WHILE NOT EOF. · Escreva Cat e Dog em List.txt, depois saia com ambas as linhas usando WHILE NOT EOF.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.