I'm writing a program that calculates prime numbers and I would like to put the output to a file if the user gave a filename for an argument.

Unfortunately I have to use windows and not linux, so the easiest option of
Code:
if(dup2(fileno(stdout), open(argv[1], O_WRONLY | O_CREAT | O_EXCL))<0)
    perror("Error");
}
and then printing to stdout all the time wont work. Is there another option I can use to do this?

I would like to avoid ifs in the actual section where I would print the primes in order to prevent the speed loss.

PS: If I have made a mistake in that code snippet and it would actually all work... I'm an idiot, sorry :P