Reputation: 13
I noticed something when I used the setvbuf () function to set the file processing buffer. If I don't use a buffer size of 256 or higher, I get strange symbols when I try to print the buffer. However, if I use 256 sizes for the buffer, I get the correct char representation of up to 8 characters. I've done research on this problem, but I guess I couldn't find my answer because of my lack of knowledge.
#include <stdio.h>
#define SIZE 8 //Below 256
int main(void)
{
char buffer[SIZE];
FILE *fp = fopen("name.txt","w");
setvbuf(fp,buffer,_IOFBF,SIZE);
fputs("a",fp);
printf("%s\n",buffer);
fclose(fp);
return 0;
}
Expected Output
a
Actual Output
a!'^//Something like this.
Upvotes: 0
Views: 581
Reputation: 52274
The contents of the buffer passed to setvbuf
are indeterminate at any time (see 7.21.5.6/2 in C11 standard or cppreference page on setvbuf). So you shouldn't expect anything.
Upvotes: 1