Wednesday, October 8, 2014

Re: readfile() and writefile() in practice

Hi Ben!

On Mi, 08 Okt 2014, Ben Klein wrote:

> 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")

contents is a list here.

> call add(contents, join(getline(1, '$'), "\n"))

To that list you are adding the current buffer content as a single
string.

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

writefile writes each listitem and because binary mode is set, it will
replace "\n" by an ASCII NUL.

> 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').

See my comments above.

Best,
Christian
--
Faulheit ist die Angewohnheit, sich auszuruhen, bevor man müde ist.

--
--
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: