V: How to flush output of print function?

Created on 23 Jun 2019  路  5Comments  路  Source: vlang/v

BUG?
I ran this code.

import time

fn main(){
  mut s = ''
  for _ in 0..100 {
    println(s)
    s += '#'

    time.sleep_ms(100)
    C.fflush(C.stdout)
  }
}

I got error.

check()
next token = `}`
pass=2 fn=`main`
panic: progressbar.v:11
expected `(` but got `)`
-> [1]

So, I fixed such that

C.fflush(C.stdout()) // <-- C.fflush(C.stdout) 

and I got error.

/Users/fuyutarow//.vlang0.0.12//progressbar.c:4221:18: error: called object type 'FILE *' (aka 'struct __sFILE *') is not a function or function pointer
 fflush ( stdout ( ) ) ;
          ~~~~~~ ^
1 error generated.
V panic: clang error
-> [1]

Most helpful comment

Take a simple look at code, seems V always think C.somesym as a C function, that say V don't support C global varables usage, at least now.

All 5 comments

I had problems too to get values of defined variables from C, however for your problem you can use 0 as the file descriptor of stdout.

Thank you. But flush does not work properly.
C.fflush (0), # fflush (stdout); or # fflush (0); got the same result.

Take a simple look at code, seems V always think C.somesym as a C function, that say V don't support C global varables usage, at least now.

You are correct, this needs to be fixed.

For now, I've enabled stdout: C.fflush(stdout)

Note that there's no C. before it (for now).

I believe, this issue stands again. The snippet below doesn't flush and prints the entire sequence at exit.

import time

fn main() {
  for i in [1, 2, 3, 4, 5] {
    print('$i')
      time.sleep(1)
      // C.fflush(C.stdout) // works if uncommented
  }
}

V 0.1.27 7585483
Debian GNU/Linux 10 (buster)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

taojy123 picture taojy123  路  3Comments

elimisteve picture elimisteve  路  3Comments

markgraydev picture markgraydev  路  3Comments

radare picture radare  路  3Comments

choleraehyq picture choleraehyq  路  3Comments