write() to stdout and printf output not interleaved?

后端 未结 5 1931
轻奢々
轻奢々 2020-11-29 07:48
#include 
#define MAXLEN 256

int main() {
  int n;
  char buf[MAXLEN];
  while((n = read(0,buf,sizeof(buf))) != 0){
    printf(\"n: %d:\",n);
    wri         


        
相关标签:
5条回答
  • 2020-11-29 08:19

    Use fwrite (streams version) rather than write.

    Note that, while is associated with file number 1, it isn't the same thing.

    0 讨论(0)
  • 2020-11-29 08:22

    Printf is buffered.

    You can force printf to 'flush' its buffer using the fflush call:

    #include <stdio.h>
    #define MAXLEN 256
    
    int main() {
      int n;
      char buf[MAXLEN];
      while((n = read(0,buf,sizeof(buf))) != 0){
        printf("n: %d:",n);
        fflush(stdout); /* force it to go out */
        write(1,buf,n);
      }
      return 1;
    }
    

    In general, printf() being buffered is a good thing. Unbuffered output, particularly to visible consoles that require screen updates and such, is slow. Slow enough that an application that is printf'ing a lot can be directly slowed down by it (especially on the Windows platform; Linux and unixes are typically impacted less).

    However, printf() being buffered does bite you if you also fprintf(stderr,) - stderr is deliberately unbuffered. As a consequence, you may get your messages with some printf() missing; if you write to another FILE handle that is also associated with the terminal, and might be unbuffered, make sure you first explicitly fflush(stdout).

    0 讨论(0)
  • 2020-11-29 08:26

    Printf is using stdio and it is buffered. Push it out by sending a changing to "n: %d:\n"

    0 讨论(0)
  • 2020-11-29 08:27

    The manpage for fgets tells me:

    It is not advisable to mix calls to input functions from the stdio library with low-level calls to read(2) for the file descriptor associ‐ ated with the input stream; the results will be undefined and very probably not what you want.

    So the best solution would be not to to use write and printf on the same descriptor.

    0 讨论(0)
  • 2020-11-29 08:33

    You can use the std fflush() function to flush the std out buffer or you can make use of an additional \n at the end of the control string inside the printf. Something like this

    printf("\n :%d:\n",n);
    

    Its always better to use the write() & read() functions in C instead of printf() and scanf(). Printf and scanf have got some problems like printf stores the string parameter in stdout buffer. So a manual flush is required which is done through fflush function or by means of \n. In a small hello world printing program you will not find such a problem as the stdout buffer is flushed at the end of the program execution. Better use write() which works fine. scanf also have the problem of reading spaces and a lot of other problems related to stdin buffer.

    For example in the code below:

    main()  {   char a; int i=0,c; for(;i<2;i++) { scanf("%d",&c); scanf("%c",&a);} }
    

    The above program as got the problem of reading \n into stdin on pressing enter. We could resolve this but not flushing the stdin buffer or making use of \n character. Always better to use the read() and write() functions.

    Hope that helps....

    0 讨论(0)
提交回复
热议问题