Text files · Arquivos 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.
Abra, depois leia ou escreva
- O tópico AS 10.3 pede pseudocódigo que lide com um arquivo de texto de uma ou mais linhas.
- Abra o arquivo em um modo:
READ,WRITEouAPPEND. WRITEinicia um novo arquivo e perde o que havia lá.APPENDadiciona ao que já existe.CLOSEFILEquando terminar. Um arquivo está aberto em apenas um modo por 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 indica quando parar
EOF("Note.txt")éTRUEquando não há mais nenhuma linha para ler.WHILE NOT EOF(...)lê toda a linha.READFILEcoloca a próxima linha em umaSTRING.
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.
Atenção
- Declare a string antes que
READFILEa use. - Arquivos aleatórios (
OPENFILE … FOR RANDOM) são A2. Esta aula não os utiliza.
Common mistakes
- Read with
READFILEonly afterOPENFILE … FOR READ. WRITEreplaces the file. UseAPPENDto keep the old lines.
Erros comuns
- Leia com
READFILEapenas apósOPENFILE … FOR READ. WRITEsubstitui o arquivo. UseAPPENDpara manter as linhas antigas.
Now you try
- Write two lines, then read them back until
EOF.
Agora você tenta
- Escreva duas linhas, depois leia-as novamente até
EOF.
Write Hello to · até Note.txt, read it back into LineOfText, and output it. · Escreva Hello para escrever em Note.txt, ler de volta para LineOfText e imprimir.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Write a and · e b to · até List.txt, then output both lines using WHILE NOT EOF. · Escreva a e b em List.txt, depois imprima ambas as linhas usando WHILE NOT EOF.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Write one to · até Note.txt, close it, APPEND the line two, then read both lines with WHILE NOT EOF and output them. · Escreva one em Note.txt, feche-o, APPEND a linha two, depois leia ambas as linhas com WHILE NOT EOF e saia-as.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.