1

i am programming with eiffel and every time i open a file and start writing into it, it deletes its content and starts writing into it like it's an empty file, is there a way to do it without deleting the previouse content?

Here is a example of code

local
    f: PLAIN_TEXT_FILE
do
    create f.make_open_write("C://myName/desktop//myfile.txt")
    f.put_integer(3)

and now the file will contain 3 and all the previouse data will be lost!

ctrl-alt-delor
  • 7,506
  • 5
  • 40
  • 52
bnaya
  • 93
  • 3

2 Answers2

2

Creation procedure make_open_append should be used in this case:

create f.make_open_append ("C:/myName/desktop/myfile.txt")
f.put_integer(3)
Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35
1

If you want to append text at the end of the file, use make_open_append like that:

f.make_open_append("C://myName/desktop//myfile.txt")

If you want to rewrite from the beginning of the file (like in your example, replacing the first character of the file with 3), you can open in read write mode and write to it like this:

f.make_open_read_write("C://myName/desktop//myfile.txt")
f.put_integer(3)
Louis M
  • 576
  • 2
  • 4
  • 1
    Calling `make_open_read_write' will still shrink the file. To overwrite the content, you need to map the file to memory which can be done using platform specific APIs. – Emmanuel Stapf Feb 26 '14 at 05:50