Wednesday, October 8, 2014

Re: readfile() and writefile() in practice

Thanks all!

Sorry -- I normally would include an example but I sent that email from my phone and have unreliable access to my computer (where I've been playing with this). I am on a Mac, for what it's worth (not yet using Vim on the iPhone :D ).

What I've tried to do is to combine a file with the contents of a buffer and writefile() that. So here is the file test.txt:

---
Tis is
text this is
---

And this code:

---
let dir = "test"

function! Append()
  let contents = readfile(g:dir . "test.txt", "b")
  call add(contents, join(getline(1, '$'), "\n"))

  call writefile(contents, g:dir . "does-this-work.txt", "b")
endfunction
---

when Append() is called on this buffer content:

---
This is one line of text.
This is another line.
---

gives me the file does-this-work.txt:

---
Tis is
text this is

This is one line of text.^@This is another line.
---

In other words it looks like readfile() and writefile() are indeed behaving correctly (so that was a mistake on my part -- sorry). Where I'm having trouble is with the getline() that gets the buffer contents. If I join() it, I don't know how to represent the newlines (obviously '\n' doesn't work). If I don't join() it, I get E730 ('using List as a String').

Ben

--
b

On Mon, Oct 6, 2014 at 7:37 PM, John Little <John.B.Little@gmail.com> wrote:
On Tuesday, October 7, 2014 9:59:53 AM UTC+13, Benjamin Klein wrote:
> As far as I can tell, readfile() and writefile() have something against new lines...
> Sent from my iPhone

;) I'd guess you're not running Vim on an iPhone (however fun that might be) but on Windows.  Looking at the code, I don't think DOS endings are written.  There is this comment when writing a file:
    /* Always open the file in binary mode, library functions have a mind of
     * their own about CR-LF conversion. */

I would expect the written file to be ok when read again by readfile(), or by vim if you have "unix" in your setting of the 'fileformats' option, or even by MS Word, but to hammer away on one line if you use the DOS command "type", and problematic in various Windows utilities, or (I'm guessing here) some Mac software.

Regards, John Little

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: