SIMPLE file IO on OS X

Hi all,

I’m fairly new to OS X coding, and mashing together a bunch of things from other places to do some vision tracking. One of my needs right now is to create a new directory and spit out sequential frames to it as they are read and processed.

I have code to do this on a PC, but I’ve just spent a whole day trying to figure out OS X’s file handling stuff. Normal ‘fopen’, ‘fwrite’ etc. commands clearly don’t cut it in OS X. Anyone have a code snippet that can create a directory, take a normal char* filename and creates a file with that name to dump bytes to, then closes it?

I looked at http://developer.apple.com/mac/library/documentation/Porting/Conceptual/PortingUnix/io-porting/io-porting.html and my head is swimming with not only Forks and FSRefs, but Unicode filename requirements. All I want to do is

a) create a directory with a given filename and a numbered suffix
b) save a sequence of files there, changing the filename each time

Can anyone help with plug-n-play code?

Thanks.

you can use system(“mkdir blah”) to make directories either relative to the data folder or not. ie:

  
  
string command = "mkdir " + ofToDataPath("blah");  
system(command.c_str());  
// makes "data/blah"  
  

or

  
  
system("mkdir /Users/zach/blah");  
  

also, Poco, which is a library that comes included with OF has great cross platform directory / file system code – here’s a short function that we had in a project and I’ve plugged it into the empty example .cpp file so you can see it in action when you press ‘a’:

  
  
#include "testApp.h"  
#include "Poco/File.h"  
#include "Poco/Path.h"  
  
using Poco::Path;  
using Poco::File;  
  
static bool makeDirectory(string dirPath, bool bRelativeToData = true){  
	if( bRelativeToData ) dirPath = ofToDataPath(dirPath);  
	  
	File myFile(dirPath);  
	bool success = false;  
	try{  
		success = myFile.createDirectory();  
	}catch( Poco::Exception &except ){  
		ofLog(OF_LOG_ERROR, "makeDirectory - directory could not be created");  
		return false;  
	}  
	  
	if(!success)ofLog(OF_LOG_WARNING, "makeDirectory - directory already exists");  
	  
	return success;  
}  
  
//--------------------------------------------------------------  
void testApp::setup(){  
  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
  
}  
  
//--------------------------------------------------------------  
void testApp::keyPressed(int key){  
  
	  
	if (key == 'a'){  
		makeDirectory("temp");  
	} else if (key == 'b'){  
		string command = "mkdir " + ofToDataPath("blah");  
		system(command.c_str());  
	}  
}  
  

hope that helps!
zach

also, here’s some code I found on my hard drive (in the eyewriter source) for filesystem stuff. there’s functions like:

copyFromTo(…)
moveFromTo(…)
getFileExt(…)
removeFileExt(…)
doesDirectoryExist(…)
doesFileExist(…)
deleteFile(…)
deleteFolder(…)

example usage:

  
  
if( !fileHelper::doesDirectoryExist("tags") ){  
		fileHelper::makeDirectory("tags/");  
		fileHelper::makeDirectory("tags/images/");  
		fileHelper::makeDirectory("tags/gml/");  
	}  
	  
	if( !fileHelper::doesDirectoryExist("tags/images/") ){  
		fileHelper::makeDirectory("tags/images/");  
	}  
			  
	if( !fileHelper::doesDirectoryExist("tags/gml/") ){  
		fileHelper::makeDirectory("tags/gml/");  
	}  
  

hope that helps!

  • zach

fileHelper.h.zip

Zach - thanks SO much for this. Did the trick handily.

hi

thanks for posting this.

i tried using the makeDirectory example and it works great.
i tried to use the copyFromTo and it seems to not work.
i had to comment out the doesFileExist() part to get it to work. see code below.
but if i use the doesFileExist() inside my testApp.cpp i get correct results. but inside your copyFromTo it seems to get false results.

  
static bool copyFromTo(string pathSrc, string pathDst, bool bRelativeToData = true,  bool overwrite = false){  
		if( bRelativeToData ) pathSrc = ofToDataPath(pathSrc);  
		if( bRelativeToData ) pathDst = ofToDataPath(pathDst);  
  
		/*  
		if( fileHelper::doesFileExist(pathSrc) == false ){  
			cout<<"fileHelper::doesFileExist(pathSrc) == false"<<endl;  
            return false;  
		}  
  
        if( fileHelper::doesFileExist(pathDst) ){  
            if( overwrite ){  
                fileHelper::deleteFolder(pathDst);  
            }else{  
				cout<<"!fileHelper::doesFileExist(pathSrc) = false"<<endl;  
                return false;  
            }  
        }  
*/  
		File fileSrc(pathSrc);  
		try{  
            fileSrc.copyTo(pathDst);  
		}catch (Poco::Exception & except){  
		    ofLog(OF_LOG_ERROR, "copyFromTo - unable to copy to, file must be busy");  
		    return false;  
		}  
		return true;  
	}  
  

also:

(i am using osx)
i am using this to copy a .mov file from the data folder to a remote computer mounted (/Volumes/remoteComputer/folderToCopyTo). same behaviour with when a large file is copied locally.

when coping a file with the fileHelper the whole OF app comes to a halt until the file is done copying.

i tried putting the fileHelper::copyFromTo() in a different thread using the threadExample. same behaviour.

?

thx

update:
i am now using ofxRuiThread.h to help me with threading and it works now without freezing the main app.

i used the ofxRuiThread example and just pasted the file copying code i needed.
i am sure there is a proper way of threading all of the filehelper functions !?

thx.

Hey - just a heads up the fileHelper class was one I wrote for the Portrait Machine project ( http://forum.openframeworks.cc/t/portrait-machine-installation/2146/0 ).

I’ll throw it up on my github account so it can develop into a more mature addon.

now on github with a basic demo example:
http://github.com/ofTheo/ofxFileHelper