Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does work setvbuf() in C [closed]

Tags:

c

can someone explain me how does int setvbuf(FILE *stream, char *buffer, int mode, size_t size) C function works?

I think it sets a buffer for a file stream and stores data in the buffer allocated by setvbuf in size_t size chunks of data, am I right? And when the buffer is full it is flushed?

sorry I am new here

like image 578
lychee Avatar asked May 09 '26 00:05

lychee


1 Answers

I assume you did search google, but you need some help understanding what you have found:

I am quoting interchangeably gnu documentation and cppreference:

 int setvbuf (FILE *stream, char *buf, int mode, size_t size)

After opening a stream (but before any other operations have been performed on it), you can explicitly specify what kind of buffering you want it to have using the setvbuf function. The facilities listed in this section are declared in the header file stdio.h.

The arguments description:

stream - the file stream to set the buffer to

buffer - pointer to a buffer for the stream to use

mode - buffering mode to use. It can be one of the following values:

_IOFBF full buffering

_IOLBF line buffering

_IONBF no buffering size - size of the buffer

If you switch for the c documentation in cppreference you will find the following example:

#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    int file_size;
    char buffer[BUFSIZ];
    FILE * fp = fopen("test.txt","w+");
    if (setvbuf(fp,buffer,_IOFBF,BUFSIZ) != 0)
    {
       perror("setvbuf()");
       fprintf(stderr,"setvbuf() failed in file %s at line # %d\n", __FILE__,__LINE__-3);
       exit(EXIT_FAILURE);
    }
 
    /* Exhibit the contents of buffer. */
    fputs ("aaa",fp);
    printf("%s\n", buffer);
    fputs ("bbb",fp);
    printf("%s\n", buffer);
    fputs ("ccc",fp);
    printf("%s\n", buffer);
    file_size = ftell(fp);
    printf("file_size = %d\n", file_size);
    fflush (fp);              /* flush buffer */
    printf("%s\n", buffer);
    fputs ("ddd",fp);
    printf("%s\n", buffer);
    fputs ("eee",fp);
    printf("%s\n", buffer);
 
    rewind(fp);               /* flush buffer and rewind file */
    char buf[20];
    fgets(buf,sizeof buf,fp);
    printf("%s\n", buf);
 
    fclose(fp);
 
    return 0;
}

Output:

aaa
aaabbb
aaabbbccc
file_size = 9
aaabbbccc
dddbbbccc
dddeeeccc
aaabbbcccdddeee

Pay attention for the following things:

  1. What happens when you fflush the FILE *fp.
  2. What buffer contains after fputs string to fp.
  3. What happens when you rewind(fp), reread from the file all you have been written.

Don't be afraid of documentation/ manual pages, if you get used to them and read them you will be a great developer, moreover now you are familiar with http://en.cppreference.com/, which is very good source to get start with new API functions, good luck.

like image 171
0x90 Avatar answered May 10 '26 14:05

0x90



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!