Thread: Understanding setvbuf()

  1. #1
    Registered User
    Join Date
    Aug 2020
    Posts
    5

    Understanding setvbuf()

    Hello,

    I am right now learning about file I/O operations using C through Prata's book. Right now I am on Chapter 13 of it.


    I am unable to understand where setvbuf() can be used. I understand that we can control buffering with it but how can it be done? Can someone please give a small example of this function in action?

    I have searched on the internet and I have not been able to find a clear example.

    Regards,
    Ishan


  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    69
    I am not sure if it is worth for a beginner to bother about.
    But if you insist, there is an example: C library function - setvbuf() - Tutorialspoint

  3. #3
    Registered User
    Join Date
    Aug 2020
    Posts
    5
    I have gone through that example earlier, but did not understand it.

  4. #4
    Registered User
    Join Date
    Mar 2021
    Posts
    3
    Code:
    /* compare these */
    
    #include <stdio.h>
    #include <unistd.h>
    
    void no_buffering()
    {
        setvbuf(stdout, NULL, _IONBF, 0);
    
        for (char c = 0x61; c < 0x7b; ++c)
        {
            putchar(c);
            usleep(1e5);
        }
    }
    
    void line_buffering()
    {
        setlinebuf(stdout); // default
    
        for (char c = 0x61; c < 0x7b; ++c)
        {
            putchar(c);
            usleep(1e5);
        }
    }
    
    int main(void)
    {
        line_buffering();
    
        no_buffering();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About setvbuf function
    By Mr.Lnx in forum C Programming
    Replies: 4
    Last Post: 08-24-2014, 10:45 AM
  2. problem setvbuf
    By vin_pll in forum C Programming
    Replies: 2
    Last Post: 08-10-2009, 11:58 PM
  3. help me understand setvbuf function
    By sick in forum C Programming
    Replies: 2
    Last Post: 11-27-2008, 09:42 AM
  4. Problem about setvbuf()
    By albert3721 in forum C Programming
    Replies: 8
    Last Post: 05-17-2007, 10:02 AM
  5. Help understanding .NET
    By subnet_rx in forum Windows Programming
    Replies: 7
    Last Post: 09-02-2004, 03:27 PM

Tags for this Thread