ofDocsdocumentation utils ofFile

ofFile

ofFile wraps functionality for opening, reading, writing, and modifying files on your computer.


ofFile file;

file.open(ofToDataPath("temp.xml"), ofFile::ReadWrite, false);
ofBuffer buff = file.readToBuffer();

You can do the same with the bufferFromFile method:

ofBuffer ofBufferFromFile(const string & path, bool binary=false);

To write a buffer to a file, use ofBufferToFile()

ofBuffer dataBuffer;
// fill the buffer with something important
bool fileWritten = ofBufferToFile("data.dat", dataBuffer); 

You can also compare files using the ==, !=, <,


canExecute( )

bool canExecute()

Check if the current path is executable.

Returns: true if executable

Whether the file is an executable file.


canRead( )

bool canRead()

Check if the current path is readable.

Returns: true if readable

Whether the file can be read or not.


canWrite( )

bool canWrite()

Check if the current path is writable.

Returns: true if writable

Whether the file can be written to or not.


changeMode( ... )

bool changeMode(ofFile::Mode mode, bool binary=true)

Reopen the current file path with a different access mode.

Parameters:

mode file access mode depending on how you plan to use the file (read only, read write, etc)

binary set to false if you are reading a text file & want lines split at endline characters automatically

Returns: true if the file was reopened with the new access mode(s).

Changes the mode of the file from the current mode to the one passed in.


close( )

void close()

Close a currently open file.

Closes the ofFile instance.


copyFrom( ... )

void copyFrom(const ofFile &mom)

copyFromTo( ... )

bool copyFromTo(const filesystem::path &pathSrc, const filesystem::path &pathDst, bool bRelativeToData=true, bool overwrite=false)

Copy source path to destination path.

Copies relative to the data path & does not overwrite by default assumes the source & destination path is in the data directory.

Parameters:

pathSrc source file or directory path

pathDst destination file or directory path

bRelativeToData set to false if you are working with paths that are not in the data directory

overwrite set to true if you want to overwrite the file or directory at the new path

Returns: true if the copy was successful


copyTo( ... )

bool copyTo(const filesystem::path &path, bool bRelativeToData=true, bool overwrite=false)

Copy the current file or directory path to a new path.

Copies relative to the data path & does not overwrite by default does not change the current path & assumes the new path is in the data folder.

Parameters:

path destination file or directory path

bRelativeToData set to false if you are working with paths that are not in the data folder

overwrite set to true if you want to overwrite the file or directory at the new path

Returns: true if the copy was successful

Copy the file from its current location into the path parameter. This is similar to the cp command.


create( ... )

bool create(const filesystem::path &path)

Create a file at a given path.

Creates as a write only binary file by default.

Parameters:

path file path

Returns: true if the file was created


create( )

bool create()

Create a file at the current path.

Creates as a write only binary file by default.

Returns: true if the file was created

If the ofFile contains a file path that doesn't exist yet, calling create() generates the file.

ofFile newFile(ofToDataPath("temp.txt"), ofFile::Write); // file doesn't exist yet
newFile.create(); // now file exists

doesFileExist( ... )

bool doesFileExist(const filesystem::path &fPath, bool bRelativeToData=true)

Check if a file or directory exists at a given path.

Parameters:

fPath file path

bRelativeToData set to false if you are working with paths that are not in the data folder and want the direct path without relative "../../"

Returns: true if a file or directory exists


exists( )

bool exists()

Check if a file exists at the current path.

Returns: true if the file exists

Tests whether a file path exists or not.


getAbsolutePath( )

string getAbsolutePath()

Returns the absolute path to the file, on OSX this will be something like /Users/name/openFrameworks/apps/app/data/file.xml on Windows it will something like C:\Documents\openframeworks\apps\app\data\file.xml


getBaseName( )

string getBaseName()

getEnclosingDirectory( )

string getEnclosingDirectory()

Returns the relative path to the directory containing the file, for instance:


ofFile file(ofToDataPath("foo.xml"));
cout << file.getEnclosingDirectory(); // prints "../../../data/xml"


getExtension( )

string getExtension()

Returns the extension of the file.

    ofFile file(ofToDataPath("foo.xml"));
    cout << file.getExtension();

getFileBuffer( )

filebuf * getFileBuffer()

Read the entire contents of the currently opened file into an output stream.

This is basically an easy to use equivalent to rdbuf(): ie. ofLogNotice() << file.getFileBuffer(); write_file << file.getFileBuffer();

\return output stream


getFileName( )

string getFileName()

Returns the actual file name.


getSize( )

uint64_t getSize()

get the size of the file at the current file path

Returns: size in bytes

Gets the size of the file at the file path.


isDevice( )

bool isDevice()

Check if the current path is a device file.

Works on Mac & Linux which can represent devices as files, however always returns false on Windows.

Returns: true if a device file

Returns whether the file path points to a mounted device.


isDirectory( )

bool isDirectory()

Check if the current path is a directory and not a file.

Returns: true if a directory

Returns whether the file path points to a directory or not.


isFile( )

bool isFile()

Check if the current path is a file and not a directory.

Returns: true if a file

Whether the file path points to a file (it could also be a directory)


isHidden( )

bool isHidden()

Returns whether the file path points to a hidden file or not.


isWriteMode( )

bool isWriteMode()

moveFromTo( ... )

bool moveFromTo(const filesystem::path &pathSrc, const filesystem::path &pathDst, bool bRelativeToData=true, bool overwrite=false)

Move source path to destination path.

Moves relative to the data path & does not overwrite by default assumes the source & destination path is in the data directory.

Parameters:

pathSrc source file or directory path

pathDst destination file or directory path

bRelativeToData set to false if you are working with paths that are not in the data folder

overwrite set to true if you want to overwrite the file or directory at the new path

Warning: be careful with slashes here, appending a slash when moving a folder may cause mad headaches in OSX

Returns: true if the move was successful


moveTo( ... )

bool moveTo(const filesystem::path &path, bool bRelativeToData=true, bool overwrite=false)

Move the current file or directory path to a new path.

Moves relative to the data path & does not overwrite by default does not change the current path & assumes the new path is in the data folder.

Parameters:

path destination file or directory path

bRelativeToData set to false if you are working with paths that are not in the data folder

overwrite set to true if you want to overwrite the file or directory at the new path

Returns: true if the copy was successful

Moves the file to the location specified by path. This is similar to the mv command.


ofFile( ... )

ofFile(const ofFile &mom)

Create a new file path using the same path & settings of another file.

Parameters:

mom ofFile instance source

Copy constructor for copying one ofFile into another


ofFile( ... )

ofFile(const filesystem::path &path, ofFile::Mode mode=ReadOnly, bool binary=true)

Create a new ofFile instance and attempt to open the path as a file.

Opens as a binary file with read only access by default.

Parameters:

path file path

mode file access mode depending on how you plan to use the file (read only, read write, etc)

binary set to false if you are working with a text file & want lines split at endline characters automatically

Creates an ofFile using the file path and mode specified. Note that if the file doesn't actually exist on the file system this doesn't actually create file until you call create().

ofFile fileToRead(ofToDataPath("dictionary.txt")); // a file that exists
ofFile newFile(ofToDataPath("temp.txt"), ofFile::Write); // file doesn't exist yet
newFile.create(); // now file exists 

ofFile( )

ofFile()

Create an ofFile instance.

Does not refer to a specific file until you either open a file or create a file or directory path.

Creates an empty ofFile.


open( ... )

bool open(const filesystem::path &path, ofFile::Mode mode=ReadOnly, bool binary=true)

Open the path as a file.

Opens as a text file with read only access by default.

Parameters:

path file path

mode file access mode depending on how you plan to use the file (read only, read write, etc)

binary set to false if you are reading a text file & want lines split at endline characters automatically

Returns: true if the path was opened

Opens the file with the file mode, either Reference, ReadOnly, WriteOnly, ReadWrite, Append


openFromCWD( ... )

bool openFromCWD(const filesystem::path &path, ofFile::Mode mode=ReadOnly, bool binary=true)

Open the path as a file.

Opens as a text file with read only access by default from the current working directory without internally calling ofToDataPath.

Parameters:

path file path

mode file access mode depending on how you plan to use the file (read only, read write, etc)

binary set to false if you are reading a text file & want lines split at endline characters automatically

Returns: true if the path was opened


openStream( ... )

bool openStream(ofFile::Mode _mode, bool binary)

operator!=( ... )

bool operator!=(const ofFile &file)

Tests whether a file path is not equal to the file path of the ofFile on the right hand side.


operator<( ... )

bool operator<(const ofFile &file)

Tests whether a file path is greater than the file path of the ofFile on the right hand side.


operator<=( ... )

bool operator<=(const ofFile &file)

Tests whether a file path is lesser or equal than the file path of the ofFile on the right hand side.


operator=( ... )

ofFile & operator=(const ofFile &mom)

Copy the path and settings of an ofFile into this instance.

Parameters:

mom ofFile instance source

Equals operator which allows you to do this:

ofFile f1 = f2;

operator==( ... )

bool operator==(const ofFile &file)

Tests whether a file path is equal to the file path of the ofFile on the right hand side.


path( )

string path()

Get the current path.

Returns: current path

Returns the string of the ofFile file path.


readToBuffer( )

ofBuffer readToBuffer()

Read the contents of a file at the current path into a buffer.

Returns: buffer with file contents

Read the file into an ofBuffer object and return it.


remove( ... )

bool remove(bool recursive=false)

Removes the file or directory at the current path.

Does not remove non-empty directories by default.

Warning: Be careful! This deletes a file or folder. :)

Parameters:

recursive set to true to remove a non-empty directory and its contents

Returns: true if the path was removed successfully

deletes a file or folder, be careful as this is not undo-able.


removeFile( ... )

bool removeFile(const filesystem::path &path, bool bRelativeToData=true)

Remove a file or directory at a given path.

Parameters:

bRelativeToData set to false if you are working with paths that are not in the data folder and want the direct path without relative "../../"

Returns: true if the path was removed successfully


renameTo( ... )

bool renameTo(const filesystem::path &path, bool bRelativeToData=true, bool overwrite=false)

Rename the current file or directory path to a new path.

Renames relative to the data path & does not overwrite by default does not change the current path & assumes the new path is in the data folder.

Parameters:

path destination file or directory path

bRelativeToData set to false if you are working with paths that are not in the data folder

overwrite set to true if you want to overwrite the file or directory at the new path

Returns: true if the copy was successful

Renames the file with the new file name. If you specify a different path then this will move the file as well.


setExecutable( ... )

void setExecutable(bool executable=true)

Set the executable flag of the current path.

Toggles the file as executable or not executable.


setReadable( ... )

void setReadable(bool readable=true)

Set the readable flag of the current path.


setWriteable( ... )

void setWriteable(bool writeable=true)

Set the writable flag of the current path.

Toggles the file as writeable or not writeable.


writeFromBuffer( ... )

bool writeFromBuffer(const ofBuffer &buffer)

Write the contents of a buffer into a file at the current path.

Parameters:

buffer source byte buffer

Returns: true if buffer's contents written successfully

Write an ofBuffer instance to the file path.


~ofFile( )

~ofFile()

Destructor