1

I have a little confused about the result of two slightly different piece of code like this:

FILE* file=fopen("test.txt","w");
char buffer[6]="hello";
char arr[6]="haloo";

//setbuf(file,buffer);
fputs(arr,file);
//fflush(file);

As you can see I firstly commented out two line of code. So the buffer would not be flushed until I close the program, at which time the file stream will be closed too. And then, as what I expect, the program would write the haloo to the test.txt as soon as I close the program. And the same things happened When I don't commented out those two lines. Like this:

setbuf(file,buffer);
fputs(arr,file);
fflush(file);

But, when I only commented out only the flush(file) line of code, like this:

setbuf(file,buffer);
fputs(arr,file);
//fflushed(file);

strange thing happen. I got things like 2800 c579 7a in my test.txt when I close my program.

And then I try to change the buffer a little bit, to something like this:

char buffer[5]="hell";   //change the contents a little bit
char arr[5]="halo";      // also change a little bit

setbuf(file,buffer);
fputs(arr,file);
//fflush(file);

Then I got 00c5 797a in my text.txt.

So I wonder if this is any undefined behavior or default pattern that I don't know.

walkerlala
  • 1,599
  • 1
  • 19
  • 32
  • 3
    *setbuf* requires that the buffer be *BUFSIZ* bytes in size (or 0 bytes if NULL is passed, in which case the stream will be unbuffered). So since your example has undefined behavior, anything can happen. – ooga May 30 '15 at 00:57
  • @ooga No. I have try that (using the latest example). `printf("%d",sizeof(buffer))` give me 5 and `printf("%d",sizeof(BUFSIZ))` give me 4(I don't know why 4, though). Maybe you should try it on your machine. – walkerlala May 30 '15 at 01:04
  • 3
    You don't want to print `sizeof(BUFSIZ)`. Since it's an `int` it will be 4 bytes on a 32-bit machine, but that's immaterial. Just print `BUFSIZ` itself. It'll be at least 256. – ooga May 30 '15 at 01:11
  • @ooga Oh you are right. My bad. Thanks. – walkerlala May 30 '15 at 01:21

1 Answers1

1

I think you want to terminate your buffer with a '\0'. Check that out.

If you are not calling fclose, there might be an undefined behaviour problem because of setbuf, see http://man7.org/linux/man-pages/man3/setbuf.3.html.

Check adding a fclose at the end of the program, this will make sure the fflush is enforced and the stream is cleanly closed, while at the same time avoiding the above mentioned bug.

Luis Yanes
  • 347
  • 2
  • 13
  • wouldn't the string of **five** characters be terminated by '\0' on default if it is fixed into an array of **six** characters long?( I have try that, it's the same) – walkerlala May 30 '15 at 01:32
  • why is that example in that page invalid? – walkerlala May 30 '15 at 02:12
  • I don't know why exactly but it seems to be a case of the buffer being invalidated, freed or closed after the buffer, leading to undefined behaviour. – Luis Yanes May 30 '15 at 02:17
  • I have post it as a question for discuss athttp://stackoverflow.com/questions/30541589/a-valid-program-being-declared-as-invalid-by-man7-org. If you have any interest. – walkerlala May 30 '15 at 02:27
  • Thank you, I hope I could help you. – Luis Yanes May 30 '15 at 02:34