A computer program uses a data structure, for example an array, to store data. The problem is, when the computer is turned off, all of the data is lost.
One way to fix this problem is to store the data in a text file. ↓ is an example of a simple text file.
The data can be stored in the file and then read into the computer program line by line, possibly into an array, for example, for processing.
The computer program can also write new data into the text file. When the textfile is closed, all of the information will be available the next time it is opened.
One very interesting thing to note is that a text file stores text ie STRINGS. It does not store numbers as INTEGERS or REALS ie it stores "216" not 216.
OPENFILE "myText.txt" FOR WRITE
f = open("myText.txt", "w")
OPENFILE "myText.txt" FOR WRITE
INPUT msg
WRITEFILE myText.txt, msg
f = open("myText.txt", "w")
msg = input("Please type a message: ")
f.write(msg)
OPENFILE "myText.txt" FOR WRITE
INPUT msg
WRITEFILE myText.txt, msg
CLOSEFILE myText.txt
f = open("myText.txt", "w")
msg = input("Please type a message: ")
f.write(msg)
f.close()
Note: Text files only store text! They do not understand numbers eg INTEGERS or REALS.
Look at this code: would it work?
f = open("ages.txt", "w")
age = int(input("How old are you? "))
ageInDays = age*365
f.write(ageInDays)
f.close()
Do some research. How can we write a number into a textfile in Python?
Fix the above problem and share your solution with your teacher and/or classmate(s).