Vai al contenuto

File di testo

2012-01: File and Text

  1. Write a program to read an entire text file into a Small Basic variable.
    You need to create a sample text file (sample.txt) somewhere first.
  2. Write a program to write out the data in a variable to a text file.
  3. Combine the 2 programs above into one program with 2 subroutines, one each to read and write to a file.
    Add some user input to ask for the input and output file names (inside the subroutines) so that a file can be read in and then written out to a different file.
  4. Print out each character and its the Unicode value to the textwindow for each character in the text file.
    Hint look at Text.GetLength, Text.GetSubText and Text.GetCharacterCode.
    You need to check each character in a loop.
  5. Count the number of lines in the text file.
    Hint: the unicode (ascii) codes for a ‘line feed’ is 10 and a ‘carriage return’ is 13.
    These occur at the end of each line of text.
  6. Modify the program to remove the Unicode character output, but ask the user for some sub-text to change and replace to something else.
    Get the program to find all occurrences of the sub-text and replace it with the replacement text and write the modified file out.

1

Scrivi un programma per leggere un intero file di testo in una variabile.
Hai bisogno di creare prima un file di testo di prova (sample.txt).

Soluzione

Legge il testo del programma stesso… e lo visualizza

testo=File.ReadContents("legge_testo.sb")
TextWindow.WriteLine(testo)

2

Scrivi un programma per trasferire dei dati da una variabile a un file di testo.

Soluzione

testo="Ciao mondo!"
File.WriteContents("scrive_testo.txt", testo)

3

Combina i due programmi precedenti in un unico programma con 2 sottoprogrammi, uno per leggere e uno per scrivere da file.
Aggiungi le richieste all’utente di inserire il nome per il file di input e il nome per il file di output in modo che si possa leggere e scrivere su file diversi.

Soluzione

testo="Ciao mondo!"

While "true"
  TextWindow.WriteLine("-------------------------------")
  TextWindow.WriteLine("Testo  : " + testo)
  TextWindow.WriteLine("-------------------------------")
  TextWindow.WriteLine("1. Testo?")
  TextWindow.WriteLine("2. Leggi il testo")
  TextWindow.WriteLine("3. Scrivi il testo")
  x=TextWindow.ReadNumber()

  If x=1 Then
    LeggiTESTO()    
  ElseIf x=2 Then
    Leggi()
  ElseIf x=3 Then
    Scrivi()      
  EndIf  
EndWhile

Sub LeggiTESTO
  TextWindow.Write("Testo? ")
  testo=TextWindow.Read()
EndSub

Sub Leggi
  TextWindow.Write("Nome del file IN? ")
  filein=TextWindow.Read()
  testo=File.ReadContents(filein)
EndSub

Sub Scrivi
  TextWindow.Write("Nome del file OUT? ")
  fileout=TextWindow.Read()
  File.WriteContents(fileout, testo)
EndSub

4

Visualizza nella finestra di testo il carattere e il suo codice Unicode per ogni carattere nel file di testo.
Consiglio: dai un’occhiata a Text.GetLength, Text.GetSubText e Text.GetCharacterCode.
Devi controllare ogni carattere all’interno di un ciclo.

Soluzione

TextWindow.Write("Nome del file IN? ")
filein=TextWindow.Read()
testo=File.ReadContents(filein)

n=Text.GetLength(testo)
For i=1 To n
  c=Text.GetSubText(testo, i, 1)
  x=Text.GetCharacterCode(c)
  TextWindow.WriteLine(c + ":" + x)
EndFor

5

Conta il numero di linee in un file di testo.
Consiglio: i codici unicode (ascii) per line feed e carriage return sono 10 e 13.
Questi caratteri sono presenti alla fine di ogni linea di un file di testo.

Soluzione

CR=13
LF=10

TextWindow.Write("Nome del file IN? ")
filein=TextWindow.Read()
testo=File.ReadContents(filein)
n=Text.GetLength(testo)
x=0

For i=1 To n
  c1=Text.GetSubText(testo, i, 1)
  If(Text.GetCharacterCode(c1)=CR) Then    
    c2=Text.GetSubText(testo, i+1, 1)
    If(Text.GetCharacterCode(c2)=LF) Then
      x=x+1
    EndIf
  EndIf
EndFor

' Conteggio l'ultima linea se non è terminata con CR+LF
If(Text.GetCharacterCode(c1) <> CR) Then    
  x=x+1
EndIf

TextWindow.WriteLine("Numero linee: " + x)

6

Modifica il programma: elimina l’output del codice unicode e invece aggiungi che chieda all’utente un certo testo da sostituire con un altro testo.
Fai in modo che il programma trovi tutte le occorrenze del testo e le sostituisca con il nuovo testo e scriva il file modificato.

Soluzione

...

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

Questo sito usa Akismet per ridurre lo spam. Scopri come i tuoi dati vengono elaborati.