Without knowing your code I have to guess: permissions or path problem...
are you using a relative path for the file to write to? Bear in mind that the working directory of your apache process (CGI script?) will be different then when you run the program standalone. Also, the credentials (user) will be different, so maybe the proces has no rights to write the file.