CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: fflush(stdout)

  1. #1
    Join Date
    May 2018
    Posts
    158

    fflush(stdout)

    If I want to write code for C89 It's necessary to use fflush(stdout) during message print to output ?
    E.g. with printf("message") .

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,820

    Re: fflush(stdout)

    No (for windows).

    printf() will display on stdout without needing to flush the output stream. Have you got a specific circumstance where you think you need fflush(stdout)? fflush() is usually used with a file stream when needed.
    Last edited by 2kaud; November 11th, 2023 at 04:54 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Nov 2018
    Posts
    114

    Re: fflush(stdout)

    By default, stdin and stdout are line buffered for interactive programs.
    A line buffered output stream would be implicitly flushed
    - when the internal buffer is full (possibly BUFSIZ bytes (or chars?))
    - when a newline is seen
    - when the stream is closed (or the program exits)

    You almost certainly need fflush for cases like this.
    Code:
    #include <stdio.h>
    #include <unistd.h>
    int main(void) {
        printf("Countdown begins\n");
        for ( int i = 0 ; i < 10 ; i++ ) {
            printf(".");
            sleep(1);
            fflush(stdout);
        }
        printf("\nboom!\n");
        return 0 ;
    }
    Without the call to fflush, you see no output until it's all over.

    You MAY need a fflush in this case.
    Code:
    #include <stdio.h>
    int main(void) {
        int a;
        printf("Type an int > ");
        //fflush(stdout);
        scanf("%d",&a);
        printf("you typed %d\n",a);
        return 0 ;
    }
    It works fine for me (on Linux), but YMMV.

    The standard is suggestively unhelpful in this matter.
    Quote Originally Posted by c99
    The input and output dynamics of interactive devices shall take place as specified in
    7.19.3. The intent of these requirements is that unbuffered or line-buffered output
    appear as soon as possible, to ensure that prompting messages actually appear prior to
    a program waiting for input.
    But then goes on to say
    Quote Originally Posted by c99
    What constitutes an interactive device is implementation-defined.
    So if you're running in an actual terminal, it may be OK.

    If you're trying to capture the output say using redirection or screen/script/tee/expect, expect weirdness.
    https://linux.die.net/man/1/screen
    https://linux.die.net/man/1/script
    https://linux.die.net/man/1/tee
    https://linux.die.net/man/1/expect

    Like this.

    Without the fflush, this is what I see.
    Code:
    $ gcc foo.c
    $ ./a.out 
    Type an int > 23
    you typed 23
    
    $ ./a.out | tee wibble
    42
    Type an int > you typed 42
    $ cat wibble
    Type an int > you typed 42
    There is no prompt when duplicating stdout using 'tee'.


    And now with the fflush
    Code:
    $ gcc foo.c
    $ ./a.out 
    Type an int > 42
    you typed 42
    
    $ ./a.out | tee wibble
    Type an int > 42
    you typed 42
    $ cat wibble
    Type an int > you typed 42
    There is a prompt when duplicating stdout using 'tee'.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,820

    Re: fflush(stdout)

    Without the call to fflush, you see no output until it's all over.
    Not with Windows and VS. This displays a . every second on Windows:

    Code:
    #include <windows.h>
    #include <stdio.h>
    
    int main() {
    	printf("Countdown begins\n");
    
    	for (int i = 0; i < 10; i++) {
    		printf(".");
    		Sleep(1000);
    	}
    
    	printf("\nboom!\n");
    	return 0;
    }
    Similarly for mixed display/input:

    Code:
    #include <stdio.h>
    
    int main(void) {
    	int a;
    
    	printf("Type an int > ");
    	scanf("%d", &a);
    	printf("you typed %d\n", a);
    
    	return 0;
    }
    also for Windows shows as expected:

    Code:
    Type an int > 123
    you typed 123
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Nov 2018
    Posts
    114

    Re: fflush(stdout)

    In other words, you've observed implementation specific behaviour.

    > Similarly for mixed display/input:
    What happens when you redirect stdout to a pipe?

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,820

    Re: fflush(stdout)

    What happens when you redirect stdout to a pipe?
    Windows doesn't have tee wibble. But using | more gives the same with/without fflush(stdout)

    Code:
    #include <stdio.h>
    
    int main(void) {
    	int a;
    
    	printf("Type an int > ");
    	fflush(stdout);
    	scanf("%d", &a);
    	printf("you typed %d\n", a);
    
    	return 0;
    }
    Code:
    >testc | more
    123
    Type an int > you typed 123
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured