class methods
- ofFile()
- ~ofFile()
- canExecute()
- canRead()
- canWrite()
- changeMode()
- close()
- copyFromTo()
- copyTo()
- create()
- doesFileExist()
- exists()
- getAbsolutePath()
- getBaseName()
- getEnclosingDirectory()
- getExtension()
- getFileBuffer()
- getFileName()
- getSize()
- isDevice()
- isDirectory()
- isFile()
- isHidden()
- isLink()
- moveFromTo()
- moveTo()
- open()
- openFromCWD()
- operator!=()
- operator
- operator<=()
- operator=()
- operator==()
- operator>()
- operator>=()
- path()
- readToBuffer()
- remove()
- removeFile()
- renameTo()
- setExecutable()
- setReadable()
- setWriteable()
- writeFromBuffer()
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 ==, !=, <, >, <=, >= operators. This is done so that the files can be stored in std::containers and sorted.
ofFile file(ofToDataPath("foo.xml"));
ofFile file2(ofToDataPath("foo2.xml"));
cout << "is foo less than foo2? " << ((file < file2) ? "true" : "false") << endl;
Documentation from code comments
path to a file or directory
inherits from an fstream so you can read/write using the stream operators once a file path has been opened
ofFile()
ofFile::ofFile()
Creates an empty ofFile.
Documentation from code comments
Create an ofFile instance.
Does not refer to a specific file until you either open a file or create a file or directory path.
ofFile(...)
ofFile::ofFile(const filesystem::path &path, ofFile::Mode mode=ReadOnly, bool binary=true)
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
Documentation from code comments
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
ofFile(...)
ofFile::ofFile(const ofFile &mom)
canExecute()
bool ofFile::canExecute()
Whether the file is an executable file.
Documentation from code comments
Check if the current path is executable.
Returns: true if executable
canRead()
bool ofFile::canRead()
Whether the file can be read or not.
Documentation from code comments
Check if the current path is readable.
Returns: true if readable
canWrite()
bool ofFile::canWrite()
Whether the file can be written to or not.
Documentation from code comments
Check if the current path is writable.
Returns: true if writable
changeMode(...)
bool ofFile::changeMode(ofFile::Mode mode, bool binary=true)
Changes the mode of the file from the current mode to the one passed in.
Documentation from code comments
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).
close()
void ofFile::close()
Closes the ofFile instance.
Documentation from code comments
Close a currently open file.
copyFromTo(...)
bool ofFile::copyFromTo(const filesystem::path &pathSrc, const filesystem::path &pathDst, bool bRelativeToData=true, bool overwrite=false)
Documentation from code comments
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 ofFile::copyTo(const filesystem::path &path, bool bRelativeToData=true, bool overwrite=false)
Copy the file from its current location into the path parameter. This is similar to the cp command.
Documentation from code comments
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
create()
bool ofFile::create()
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
Documentation from code comments
Create a file at the current path.
Creates as a write only binary file by default.
Returns: true if the file was created
create(...)
bool ofFile::create(const filesystem::path &path)
Documentation from code comments
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
doesFileExist(...)
bool ofFile::doesFileExist(const filesystem::path &fPath, bool bRelativeToData=true)
Documentation from code comments
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 ofFile::exists()
Tests whether a file path exists or not.
Documentation from code comments
Check if a file exists at the current path.
Returns: true if the file exists
getAbsolutePath()
string ofFile::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
Documentation from code comments
\biref Get the absolute, full path of the file, ie. "images" -> "/Users/mickey/of/apps/myApps/Donald/bin/data/images".
Returns: current path as an absolute path
getBaseName()
string ofFile::getBaseName()
Documentation from code comments
\biref Get the current path without its last component, ie. "images/duck.jpg" -> "images" and "images/some/folder" -> "images/some".
Returns: current path basename
getEnclosingDirectory()
string ofFile::getEnclosingDirectory()
Returns the relative path to the directory containing the file, for instance:
ofFile file(ofToDataPath("foo.xml"));
cout << file.getEnclosingDirectory(); // prints "../../../data/xml"
Documentation from code comments
Get the enclosing parent directory of a path, ie. "images/duck.jpg" -> "images", assumes the path is in the data directory.
Returns: current path's enclosing directory
getExtension()
string ofFile::getExtension()
Returns the extension of the file.
ofFile file(ofToDataPath("foo.xml"));
cout << file.getExtension();
Documentation from code comments
Get the current path without its extension, ie. "duck.jpg" ->"duck".
Returns: current path file extension
getFileBuffer()
filebuf * ofFile::getFileBuffer()
Documentation from code comments
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 ofFile::getFileName()
Returns the actual file name.
Documentation from code comments
Get the filename of the current path by stripping the parent directories, ie. "images/duck.jpg" -> "duck.jpg".
Returns: current path filename
getSize()
uint64_t ofFile::getSize()
Gets the size of the file at the file path.
Documentation from code comments
get the size of the file at the current file path
Returns: size in bytes
isDevice()
bool ofFile::isDevice()
Returns whether the file path points to a mounted device.
Documentation from code comments
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
isDirectory()
bool ofFile::isDirectory()
Returns whether the file path points to a directory or not.
Documentation from code comments
Check if the current path is a directory and not a file.
Returns: true if a directory
isFile()
bool ofFile::isFile()
Whether the file path points to a file (it could also be a directory)
Documentation from code comments
Check if the current path is a file and not a directory.
Returns: true if a file
isHidden()
bool ofFile::isHidden()
Returns whether the file path points to a hidden file or not.
Documentation from code comments
Check if the current path is hidden.
Works on Mac & Linux which denote hidden files by prepending a period to the filename -> ".hello", however always returns false on Windows.
Returns: true if hidden
isLink()
bool ofFile::isLink()
Returns whether file is an alias or not.
Documentation from code comments
Check if the current path is a system link to another file or directory.
Returns: true if a system link
moveFromTo(...)
bool ofFile::moveFromTo(const filesystem::path &pathSrc, const filesystem::path &pathDst, bool bRelativeToData=true, bool overwrite=false)
Documentation from code comments
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 ofFile::moveTo(const filesystem::path &path, bool bRelativeToData=true, bool overwrite=false)
Moves the file to the location specified by path. This is similar to the mv command.
Documentation from code comments
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
open(...)
bool ofFile::open(const filesystem::path &path, ofFile::Mode mode=ReadOnly, bool binary=true)
Opens the file with the file mode, either Reference, ReadOnly, WriteOnly, ReadWrite, Append
Documentation from code comments
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
openFromCWD(...)
bool ofFile::openFromCWD(const filesystem::path &path, ofFile::Mode mode=ReadOnly, bool binary=true)
Documentation from code comments
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
operator!=(...)
bool ofFile::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 ofFile::operator
Tests whether a file path is greater than the file path of the ofFile on the right hand side.
Tests whether a file path is greater than the file path of the ofFile on the right hand side.
operator<=(...)
bool ofFile::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 & ofFile::operator=(const ofFile &mom)
operator==(...)
bool ofFile::operator==(const ofFile &file)
Tests whether a file path is equal to the file path of the ofFile on the right hand side.
operator>(...)
bool ofFile::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 ofFile::operator>=(const ofFile &file)
Tests whether a file path is greater than or equal to the file path of the ofFile on the right hand side.
path()
string ofFile::path()
Returns the string of the ofFile file path.
Documentation from code comments
Get the current path.
Returns: current path
readToBuffer()
ofBuffer ofFile::readToBuffer()
Read the file into an ofBuffer object and return it.
Documentation from code comments
Read the contents of a file at the current path into a buffer.
Returns: buffer with file contents
remove(...)
bool ofFile::remove(bool recursive=false)
deletes a file or folder, be careful as this is not undo-able.
Documentation from code comments
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
removeFile(...)
bool ofFile::removeFile(const filesystem::path &path, bool bRelativeToData=true)
Documentation from code comments
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 ofFile::renameTo(const filesystem::path &path, bool bRelativeToData=true, bool overwrite=false)
Renames the file with the new file name. If you specify a different path then this will move the file as well.
Documentation from code comments
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
setExecutable(...)
void ofFile::setExecutable(bool executable=true)
Toggles the file as executable or not executable.
Documentation from code comments
Set the executable flag of the current path.
setReadable(...)
void ofFile::setReadable(bool readable=true)
Documentation from code comments
Set the readable flag of the current path.
setWriteable(...)
void ofFile::setWriteable(bool writeable=true)
Toggles the file as writeable or not writeable.
Documentation from code comments
Set the writable flag of the current path.
writeFromBuffer(...)
bool ofFile::writeFromBuffer(const ofBuffer &buffer)
Write an ofBuffer instance to the file path.
Documentation from code comments
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
Last updated Tuesday, 19 November 2024 17:25:41 UTC - 2537ee49f6d46d5fe98e408849448314fd1f180e
If you have any doubt about the usage of this module you can ask in the forum.
If you want to contribute better documentation or start documenting this section you can do so here
If you find anything wrong with this docs you can report any error by opening an issue