fwrite/format on sockets?

Martin Bjorklund mbj@REDACTED
Sat Aug 26 12:35:54 CEST 2000

Torbjorn Tornkvist <tobbe@REDACTED> wrote:
> > It seems like calling fwrite/format on a socket fails with badarg. Is there
> > a way to do something like this? 
> Use: gen_tcp:send/2 for this.
> > If sockets are not IODevices usable by
> > fwrite, is there an equiviland to Common Lisp's (format nil "Format
> > string")? (Ie return the string which would normally be printed instead of
> > printing it).
> Use: io_lib:format/2 as shown in the example below:
> 1> lists:flatten(io_lib:format("hello ~s~n",["world"])).
> "hello world\n"

But note that gen_tcp:send/2 does not need a flattened list.  In this
case you can save some performance by sending the result from
io_lib:format/2 directly to gen_tcp:send/2.  Also, in many cases you
might not even have to call io_lib:format/2, but instead write code
like this:

   gen_tcp:send(Sock, [String, ": ", integer_to_list(Integer)])

and so on.

(This is actually true for all ports.  If you find yourself using
lists:flatten a lot, think again!)


More information about the erlang-questions mailing list