Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   writing to the screen using unbuffered io (http://www.programmingforums.org/showthread.php?t=12729)

cwl157 Mar 6th, 2007 1:46 AM

writing to the screen using unbuffered io
 
Anyone know how to write to the screen using unbuffered IO? Is this even possible? I know about the sprintf() function and have used that to fill a buffer with the message i want displayed but I am unsure of how to display it to the screen.

Arevos Mar 6th, 2007 3:49 AM

I'm sorry if I'm misunderstanding the question, but are you looking for the "printf" function?

cwl157 Mar 6th, 2007 10:09 AM

yea but isn't printf buffered. I basically need the unbuffered IO inequivalent to printf. That is if one exists.

grumpy Mar 6th, 2007 10:31 AM

The standard way is to call fflush(stdout) after each call of printf(). If stdout is buffered then that will flush any buffers.

cwl157 Mar 6th, 2007 10:33 AM

i cant use printf what about calling fflush after an sprintf would that flush the string that got filled up in sprintf to the screen?

DaWei Mar 6th, 2007 11:17 AM

sprintf is a printf that writes to a string, not to the output. If you can use it, why can't you use printf? Perhaps you could explain in more detail what you want to do and why you have restrictions against normal usage (and what the restrictions are).

cwl157 Mar 6th, 2007 12:43 PM

well the one restriction is i can not use buffered io. Isn't printf considered being buffered IO? This is what I'm not sure about. Or is printf the only way to write to stdout which would be the screen?

DaWei Mar 6th, 2007 2:13 PM

You could use low-level I/O to output the material preformatted by sprintf. Another quick way would be to use stderr (via fprintf), which is unbuffered, rather than stdio.

cwl157 Mar 6th, 2007 5:39 PM

i figured it out. sprintf is used to fill the string and then write is used to write the string and then you pass either STDOUT_FILENO for the screen cause its standard out or STDERR_FILENO for standard error

grumpy Mar 7th, 2007 3:37 AM

Quote:

Originally Posted by cwl157 (Post 124890)
i figured it out. sprintf is used to fill the string and then write is used to write the string and then you pass either STDOUT_FILENO for the screen cause its standard out or STDERR_FILENO for standard error

That solution is specific to your particular compiler/library/OS.

In general, there is no specific requirement that printf() be buffered. That is an implementation detail of your compiler/library. If it is buffered, fflush() will flush the buffer.

Buffered I/O is generally only stated as a requirement for learning purposes or as a result of poor design. Using unbuffered I/O is generally a solution that meets a requirement, not a requirement in its own right.


All times are GMT -5. The time now is 2:20 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC