I encountered this phenomenon before. I didn’t study the reason in depth at the time, but avoided it in an inefficient way.
Steps to reproduce:
Open a iex, open a file through the :file module, and write content to it:
iex> {:ok, f} = :file.open "txtfile", [:read, :write, :utf8]
{:ok, #PID<0.168.0>}
iex> IO.write f, "hello"
:ok
iex> IO.read f, :all
“hello”
The above process is all normal. If an external editor such as nano or other edits the content of txtfile to hello1, we can still read the modified content:
iex> :file.position f, :bof
{:ok, 0}
iex> IO.read f, :all
'hello1\n'
However, if you use VIM to edit and save this file, everything is abnormal.
➜ ~ vim txtfile
➜ ~ cat txtfile
hello2
As you can see, I used VIM to modify the content to hello2. Then, I tried to continue reading from the beginning in iex:
iex> :file.position f, :bof
{:ok, 0}
iex> IO.read f, :all
'hello1\n'
Not only that, even if I use any editor to continue editing txtfile, the latest file content will not be read in iex. All this is because I have saved this file with VIM.
























