Thread: my fflush() and other questions~

  1. #1
    Registered User
    Join Date
    May 2007
    Location
    China
    Posts
    37

    Please help with my fflush() and other questions~

    hi all,
    I am a novice of C. i've read almost all articles about the topic of flushing the stdin,
    I mimic and try to make my own flush function ( myfflush() ).
    Code:
    #include "stdio.h"
    #include "string.h"
    
    char str[5];
    
    void main()
    {
                    //char str[5];                /*why can't here?*/
    	void myfflush();
    
    	fgets ( str, sizeof( str ), stdin );
    
    	//flushall();                  /* these 3 funcs,only one available each time*/
    	//myfflush();
    	//fflush ( stdin );
    	printf ( "str is:%s\n", str );
    	printf( "Stdin is:%s\n", stdin );
    
    	//fflush ( stdout );          /* usage of ffush(stdout)?*/
    	printf ( "Stdout is:%s\n", stdout );
    }
    
    void myfflush()
    {
         //extern char str[5];                 /* why can't work when defined in main()*/
    	 char eat;
    	 if ( !strchr( str, '\n') )
    	 {
    		 while ( (eat = getchar() != '\n' ) && (eat != '\0') )
    		 ;
    	 }
    }
    then I compare it with fflushall(), fflush(stdin)--not caring the portability.

    when I do this in tc++, it works well, here is the input and outputs:
    with any one of the 3 functions: (remove "//" before the function when using it, and add it back when using another)
    input: abcdefgh
    output: str is:abcd
    Stdin is:
    Stdout is:

    it indicates that all the 3 functions worked well and had the same result.
    ***********************

    when I do this in vc++6.0, the returned results are the same,but they have a little different with that of tc,
    Stdin and Stdout have contents this time.

    with any one of the 3 functions:
    input: abcdefgh
    output: str is:abcd
    Stdin is:(some confusing characters)
    Stdout is:(some other confusing characters)
    ***********************

    then I run it in VC++2005, my function( myfflush() ) seems to work differently with the other two functions,as the
    Stdin is different...:

    1.with fflush(stdin) or flushall();
    input: abcdefgh
    output: str is:abcd
    Stdin is:(some confusing characters--let's call it cstring1.)
    Stdout is:(some other confusing characters--let's call it cstring2.)
    2.with myfflush();
    input: abcdefgh
    output: str is:abcd
    Stdin is:(some confusing characters--different with cstring1)
    Stdout is:(some other confusing characters--the same with cstring2.)

    Here are my questions:
    1.Does it mean that my function( myfflush() ) doesn't have the same function with the other two?
    if it does, where?
    2.what does fflush( stdout ) do as it doesn't clean the screem~...? does printf() reads the string
    from str[5] to a buffer of stdout, and fflush( stdout ) clears this buffer? if so, why that buffer
    left uncleaned after performing printf();
    3.i try to define str[5] inside main() and declare it in myfflush() with extern char str[5], but it
    doesn't work , why?


    many thanks! :)
    Last edited by albert3721; 05-13-2007 at 02:13 AM. Reason: add more questions

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    getchar() returns an int, not a char, and char < int, so your getchar() won't fit into you char, possible loss of data there.
    NEVER fflush( stdin ), fflush is only for output streams (read the FAQ).

    2. fflush clears the internal output buffer, for example printf(string) might load 50 chars into the internal input buffer but it doesn't want to output them yet, until it gets 120... Of course this is a trvial example. So fflush() forces the flushing of the buffer, to the stream open'd (screen or file).

    3. Variables declared in the global scope are in scope everywhere, its not like PHP where you must say so.

  3. #3
    Registered User
    Join Date
    May 2007
    Location
    China
    Posts
    37

    got it.

    thank you, it helps a lot!

  4. #4
    Registered User
    Join Date
    May 2007
    Location
    China
    Posts
    37

    wait...

    Code:
    fflush clears the internal output buffer, for example printf(string) might load 50 chars into the 
    internal input buffer but it doesn't want to output them yet, until it gets 120... Of course this
     is a trvial example. So fflush() forces the flushing of the buffer, to the stream open'd (screen or file).
    what is internal output buffer? is it created for printf() automatically?
    see...
    Code:
     
    ......
    char string[15] = "string buf";
    printf (string);
    Does printf() read data from string[15] and store it in its own buffer(internal out buffer) first before
    putting it to screem?
    Code:
    for example printf(string) might load 50 chars into the internal input buffer but it doesn't want to 
    output them yet, until it gets 120...
    printf() can wait until it has enough data? how does it happen?


    could i have the explanations? thank you
    Last edited by albert3721; 05-13-2007 at 04:51 AM. Reason: format wrong

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    > printf() can wait until it has enough data? how does it happen?
    Well that's an awkward way to express what really happens. Both input and output are line buffered in Standard C. Output functions usually wait for a newline before writing to stdout.

    Consider how some people like to put the newline at the beginning of the output statement.
    You could probably reproduce the mistake just by doing that.
    Code:
    #include <stdio.h>
    
    int main( void ) {
      char buf[20];
    
      printf( "\nType something: " );
      if( fgets( buf, sizeof buf, stdin ) != NULL )
        puts( buf );
    
      return 0;
    }
    
    /* my output:
    * what do I do here?
    * Type something: what do I do here?
    */
    See how something like misplaced newlines can mess up what you're supposed to read? fflush is used to force writing to a stream if things aren't going correctly.

    Also, don't put newlines at the front. I don't understand why people do that.

  6. #6
    Registered User
    Join Date
    May 2007
    Location
    China
    Posts
    37

    ..

    thanks citizen, but i am still , i'll think about it.
    another unsloved...
    fflush clears the internal output buffer, for example printf(string) might load 50 chars into the internal input buffer
    what is internal output buffer? is it created for printf() automatically?
    see...
    Code:
     ......
    char string[15] = "string buf";
    printf (string);
    Does printf() read data from string[15] and store it in its own buffer(internal out buffer) first before putting it to screem?

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    Quote Originally Posted by albert3721 View Post
    what is internal output buffer? is it created for printf() automatically?
    Does printf() read data from string[15] and store it in its own buffer(internal out buffer) first before putting it to screem?
    In that sense, I think you are talking about printf's arguments, and yes, storage space will be created for that automatically just by calling the function. Otherwise, how else would the printf function work? Printf is built to work like a black box though, so implementation details are somewhat of a secret. You don't have to know about or care how printf really works to use it.

    I will tell you that if the format string is simple enough, "reading data from string" and writing it directly to stdout is essentially what happens. Like

    printf( "%s\n", string );

    If you want to know what you can do with printf, all you need to do is read a reliable source on formatted output such as printf's man page.

  8. #8
    Registered User
    Join Date
    May 2007
    Location
    China
    Posts
    37

    :)

    Thank you!

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    fflush clears the internal output buffer, for example printf(string) might load 50 chars into the internal input buffer
    what is internal output buffer?
    For now let's assume that we're working with files here, instead of the screen.

    Instead of just writing every character to a file every time you want it to, the computer queues up the data you "wrote" to the file with your program. When the buffer is full, then the hard drive actually writes the data to the disk. (Hard drives and other magnetic disk drives are more efficient that way.)

    But this means that, if you print something to a file, it won't actually appear unless you print lots of stuff after it. An alternative to printing lots of stuff to the file just to get the data to appear is to flush the file, that is, tell the operating system that you want the data in the buffer written to the file, even though the buffer may not be full. (Closing a file also has the same effect, because you can't write anything else to it.)

    Thus, fflush() flushes an output file, causing data in the buffer to be written to the file. It does not do anything to input files. Its behaviour is undefined for input files. Do not use it on input files. See this FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    The screen is a little different: it's relatively efficient to print small amounts of data to the screen, so the screen is often line buffered (the buffer holds one line) or not buffered at all. But of course you might not be writing to the screen but rather to a file, if your program's output is redirected. It's best to flush stdout if any data you write to it does not end with a newline.

    So how does one clear the input buffer? http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed