0

Firstly I would like to apologize if I have used the words 'stream' and 'buffer' incorrectly.

In response to this question -> How to read blocks of data from a file and then read from that block into a vector?

I was told that reading record by record or block by block from file won't have much of an effect since the standard library already does buffering as it reads data from a file. Moreover, since it is possible to resize the buffer, it isn't necessary to read block by block. So, to test this I decided to do a little experiment of my own.

FILE *file;
file=fopen("out","r");
setvbuf(file,NULL,_IOFBF,1024);
char c=fgetc(file);

I have a file "out" which has 2048 characters. I associated a buffer of 1024 bytes with it via setvbuf. According to http://www.cplusplus.com/reference/cstdio/setvbuf/ , when an input operation is requested (with the mode _IOFBF), the buffer is completely filled. So when I ask to read a character via

char c=file.get()

the buffer should be completely filled, i.e., it should have the first half of file "out".

Now my question is how do I display the contents of the buffer so that I can verify my experiment (if my understanding is correct at all, that is)?

Thanks.

Community
  • 1
  • 1
Paagalpan
  • 1,261
  • 2
  • 16
  • 25
  • The `setvbuf` call will not work with C++ file streams, that should have given you a compiler error. [`setvbuf`](http://en.cppreference.com/w/cpp/io/c/setvbuf) is used for old C stdio `FILE` files. Check e.g. [this reference](http://en.cppreference.com/w/cpp/io) for the buffer classes used in C++. – Some programmer dude Feb 28 '13 at 12:27
  • Yeah I just realized it after I posted this question. I'll make the encessary changes. – Paagalpan Feb 28 '13 at 12:32

0 Answers0