In c++, I have this code:
#include <unistd.h>
#include <iostream>
using namespace std;
int main()
{
cout<<"before-sleep"<<endl;
sleep(5);
cout<<"after-sleep"<<endl;
return 1;
}
then i compile it with g++ to main.exe and run it. when run it I redirect the std out to a file :
./main.exe > file.log
however, the two line "before-sleep" and "after-sleep" are print to the file almost at the same time -- all have 5s delay. How can I made the first line print to the file immediately and the second line print to the file with 5s delay?
The situation is the same in python:
import time
print 'before sleep'
time.sleep(5)
print 'after sleep'