Ag. D. Hatzimanikas schrieb:
> Hi,
> 
> I was running a function, (say write()) to call redir to write in a 
> file and then another function, (say read()) to call readfile to read
> the last entry from that same file, when I discovered that in every
> second call to those functions, readfile couldn't read the last line of
> the file, although the redirection was successful and the file was
> written properly.
> 
> After some tests I was able to create a reproducible example; below is
> that function.
> 
> 
> function! Test(...)
>     let list = [0, 1]
>     let nr = 1
>     while nr < 10
>         let len = len(list)
>         echo "\n"
>         redir >> test
>         echo join(list)

" instead try
          echon join(list)."\n"

>         redir END
>         let nr += 1
>         call add(list, nr)
>         if a:0 != 0
>             exec a:1.'sleep'
>         endif
>         let lastline = join(readfile('test')[-1:])
>         let lenlastline = len(split(lastline))
>         if len != lenlastline
>             echo lastline
>             echohl Error
>             echo "this pair is different"
>             echohl NONE
>             break
>         endif
>         echo lastline
>     endwhile
> endfunction
> 
> The function takes an optional argument to add a delay (between writing
> and reading).
> 
> Now double (at least) run this function and observe (if it is reproducible)
> in that in every second call to the function, at least in one pair of lines,
> the first line is different than the second.
> 
> Try then:
> 
>     echo readfile('test')[-1:]
> 
> readfile cannot read the last line, while the line was written, check
> this with:
>     
>     echo system('tail -1 test')
> 
> If this test is reproducible, is there an explanation?
> 
> 
> VIM - Vi IMproved 7.2a BETA (2008 Jun 24, compiled Jul 10 2008 13:40:07)
> Included patches: 1-19
> 
> Huge version in Linux 2.6.25.10, with (GNU libc) 2.7.
> 
> Regards,
> 
> Ag.

Hmm, from my testing: :redir works a bit strange.  While you think
:echo "line"  sends "line\n", actually "\nline" is redirected.  That's
why you get a first empty line in the file and the final EOL is missing.
And readfile() doesn't like files with missing EOL at the end.

-- 
Andy



--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_dev" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Raspunde prin e-mail lui