问题
I'm programming with gcc
in CentOS 5.5 and the most of time I use printf()
and fprintf()
to print on terminal, but in some websites I've seen that some people use write()
. I want to know if there's other ways to print on terminal.
Thanks.
回答1:
There are some major differences between these functions.
- The standard library provides some functions to output to
stdout
:printf
,puts
,putchar
etc. - And some functions to output to a stream, you can specify the stream to
stdout
:fprintf
,fputs
,fwrite
, etc. - But
write
is different, it's a low-level I/O function. The standard library doesn't provide any low-level I/O functions. For example, POSIX provideswrite
that can output to a file descriptor.
Google for how to use each one of them.
回答2:
You could use puts()
or putchar()
.
puts("Hello, world!\n");
There's a also fputs()
, putc()
, and fputc()
if you want/need to specify a FILE*
to write to.
回答3:
All the output functions in the C standard I/O library could be used:
- fprintf()
- fputc()
- fputs()
- fputwc()
- fputws()
- fwprintf()
- fwrite()
- printf()
- putc()
- putchar()
- puts()
- putwc()
- putwchar()
- vfprintf()
- vfwprintf()
- vprintf()
- vwprintf()
- wprintf()
Most of the other write-like functions in POSIX could be used (but a few are reserved for sockets and those probably can't be used).
- aio_write()
- dprintf()
- putc_unlocked()
- putchar_unlocked()
- pwrite()
- vdprintf()
- write()
- writev()
There are many functions in the curses
library that could be used.
来源:https://stackoverflow.com/questions/19919764/any-other-way-to-print-on-the-screen-instead-of-printf-and-fprintf-in-c