I found this interesting behaviour in the following c++ program.
#include <fstream>
void append(std::string path, int i){
FILE *out = fopen(&path[0], "a");
fprintf(out, "%d\n", i);
//fclose(out);
}
int main(){
for(int i=0; i<3; i++) append("./text.txt",i);
return 0;
}
where I forget to close the file in append
.
Then the content of text.txt
after one execution will be
2
1
0
other than
0
1
2
Once I fclose
the file correctly, this effect will disappear.
I wonder how does this happen? BTW, I'm running it on a Ubuntu machine.