You should look in your error logs to see if it tells you why you can't open the file, but my first guess would be a permissions problem. When you do it from the command line you're doing it in a directory that you created, so you have read/write permission. When the browser is doing it through CGI it's running as another user (probably www or something similar) and for (hopefully obvious) security reasons the www user is limited in what directories it can read/write.
Try creating either a subdirectory in your cgi directory, or one somewhere in below the DocumentRoot for the webserver, and make sure it has the permissions set so the server can write to it