Consider the following code:
freopen("buffer.txt", "w+", stdin);
fprintf(stdin, "hello");
fseek(stdin, 0, SEEK_SET);
char str[16];
scanf("%s", str);
printf("%s", str);
I've found no entries in standard restricting me from doing that, but also no entries explicitly allowing it. Should I expect this code to work on any standard compliant compiler? Would any standard i/o function break or lead to UB if stdin
(or stdout
) is opened in read-write mode? What about c++ streams?