i have the following code:

HWND window;
	char name[MAX_PATH];

	while(1)
	{
		window = GetForegroundWindow();
		GetWindowText(window,name,sizeof(name));
		strcat(name , "\n");
		WriteToFile(name);
	}

i just want to print it once,and if i open a new window,print that too,once....and so on.....and i dont know what condition to put to work corectly :(