+int MothurOut::openOutputFileBinary(string fileName, ofstream& fileHandle){
+ try {
+
+ string completeFileName = getFullPathName(fileName);
+#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
+#ifdef USE_COMPRESSION
+ // check for gzipped file
+ if (endsWith(completeFileName, ".gz") || endsWith(completeFileName, ".bz2")) {
+ string tempName = string(tmpnam(0));
+ mkfifo(tempName.c_str(), 0666);
+ cerr << "Compressing " << completeFileName << " via temporary named pipe " << tempName << "\n";
+ int fork_result = fork();
+ if (fork_result < 0) {
+ cerr << "Error forking.\n";
+ exit(1);
+ } else if (fork_result == 0) {
+ string command = string(endsWith(completeFileName, ".gz") ? "gzip" : "bzip2") + " -v > " + completeFileName + string(" < ") + tempName;
+ system(command.c_str());
+ exit(0);
+ } else {
+ completeFileName = tempName;
+ }
+ }
+#endif
+#endif
+ fileHandle.open(completeFileName.c_str(), ios::trunc | ios::binary);
+ if(!fileHandle) {
+ mothurOut("[ERROR]: Could not open " + completeFileName); mothurOutEndLine();
+ return 1;
+ }
+ else {
+ return 0;
+ }
+ }
+ catch(exception& e) {
+ errorOut(e, "MothurOut", "openOutputFileBinary");
+ exit(1);
+ }
+
+}