Write from a file

0

1

2

3

4

5

6

7

8

9

10

M

R

 

H

A

M

F

L

E

T

T

To write to a file you need to open it in write mode. This enables you to set exclusivity to the file so no one else can update it. Writing to a file will always happen at the end of the file. Consider the file above and then look at the code below -

file = OPEN("myFile.txt", W)
WRITE(file, "new stuff")
CLOSE(file)

This would result in "MR HAMFLETTnewstuff" being stored in the file. Notice that it saves it at the end of the file AND exactly as stated. As I did not put a space at the start of "new stuff" it did not add one!

Most languages will allow you to write to files as text or as bytes. Using bytes means that we can store raw binary data, like a image, rather than simple text. This is sometimes called binary mode. Text mode will save ASCII or unicode values. Under normal circumstances you can assume text mode unless otherwise stated.