Important
Member functions
Element access
Capacity
Modifiers
Persistence
The class is not thread safe.
Changes are not saved when a filemanger object is destroyed. Explicit persistance is required via flush or commit.
The class is move-only. Trying to copy a filemanager object won't work due to the nature of the design.
The journal file is kept in the same directory as the main file. Its name follows a simple rule: main file name + _journal. It always uses the .log extension.
Main file: text.txt in C:/dir/text.txt
Journal file: text_journal.log in C:/dir/text_journal.log
Creates a new filemanager instance.
Scans entire file during instantiation as a necessary step for setting important internal systems and variables. Can cause delayed program starts depending on the size of the managed file.
| Parameter | Type | Description | Notes |
|---|---|---|---|
file_path |
std::filesystem::path |
Path to a file | File and parent directories will be created if necessary |
| Exception | Cause | Fix |
|---|---|---|
fm::open_error |
Manager was unable to read file content | Ensure that nothing is locking the file and that the program is run with enough permission |
fm::occupied_error |
Two managers tried to manage the same file path | Either pass by reference/pointer or use move semantics |
#include "filemanager.h"
int main() {
fm::filemanager file("data.txt");
}Retrieves the content of a specified line.
Jumps to the position of a specified line and retrieves the content on the storage drive.
| Parameter | Datatype | Description | Notes |
|---|---|---|---|
index |
size_t |
Which line to read | Starts at 0 |
| Type | Description | Notes |
|---|---|---|
std::string |
Specified line |
| Exception | Cause | Fix |
|---|---|---|
fm::index_error |
Line does not exist | Check bounds with size() or empty() before access |
fm::open_error |
Manager was unable to open file for reading | Ensure that nothing is locking the file and that the program is run with enough permission |
/*
Imagine a file 'data.txt' with following lines:
(1) Hello
(2) World
(3) !
*/
#include <iostream>
#include "filemanager.h"
int main() {
fm::filemanager file("data.txt");
std::cout << file.read(1);
}WorldRetrieves the content at the first line.
Retrieves the content on the storage drive at a cached index which represents the first line
| Type | Description | Notes |
|---|---|---|
std::string |
First line |
| Exception | Cause | Fix |
|---|---|---|
fm::empty_error |
File is empty | Check bounds with size() or empty() before access |
fm::open_error |
Manager was unable to open file for reading | Ensure that nothing is locking the file and that the program is run with enough permission |
/*
Imagine a file 'data.txt' with following lines:
(1) Hello
(2) World
(3) !
*/
#include <iostream>
#include "filemanager.h"
int main() {
fm::filemanager file("data.txt");
std::cout << file.front();
}HelloRetrieves the content at the last line.
Retrieves the content on the storage drive at a cached index which represents the last line
| Type | Description | Notes |
|---|---|---|
std::string |
Last line |
| Exception | Cause | Fix |
|---|---|---|
fm::empty_error |
File is empty | Check bounds with size() or empty() before access |
fm::open_error |
Manager was unable to open file for reading | Ensure that nothing is locking the file and that the program is run with enough permission |
/*
Imagine a file 'data.txt' with following lines:
(1) Hello
(2) World
(3) !
*/
#include <iostream>
#include "filemanager.h"
int main() {
fm::filemanager file("data.txt");
std::cout << file.back();
}!Returns a chronological copy of every line.
Iterates through every line and copies it into a vector. Finishes a tiny bit slower than expected because the CPU has to perform "jumps" over erased indices.
| Type | Description | Notes |
|---|---|---|
std::vector<std::string> |
All lines in the file | Can be empty |
| Exception | Cause | Fix |
|---|---|---|
fm::open_error |
Manager was unable to open file for reading | Ensure that nothing is locking the file and that the program is run with enough permission |
/*
Imagine a file 'data.txt' with following lines:
(1) Hello
(2) World
(3) !
*/
#include <iostream>
#include "filemanager.h"
int main() {
fm::filemanager file("data.txt");
for (std::string line : file.all()) {
std::cout << line << "\n";
}
}Hello
World
!Returns the number of present lines.
Not commited appends also account for the number of present lines
| Type | Description | Notes |
|---|---|---|
size_t |
Number of present lines |
/*
Imagine a file 'data.txt' with following lines:
(1) Hello
(2) World
(3) !
*/
#include <iostream>
#include "filemanager.h"
int main() {
fm::filemanager file("data.txt");
std::cout << file.size();
}3
Returns true if the file is empty.
Not commited, erased lines also account for whether the result is true
| Type | Description | Notes |
|---|---|---|
bool |
Whether the file is empty |
/*
Imagine a file 'data.txt' with following lines:
(1) Hello
(2) World
(3) !
*/
#include <iostream>
#include "filemanager.h"
int main() {
fm::filemanager file("data.txt");
file.clear();
std::cout << file.empty();
}1
Bools are displayed as 1 (true) or 0 (false) on the console
Adds data as a new line at the end of the file.
Appended content is stored in memory. Does not update the file (Check flush() and commit() for more info).
| Parameter | Datatype | Description | Notes |
|---|---|---|---|
args |
Args... |
One or more values to append | Must not contain \n or \r and must be convertable to a string |
| Type | Description | Notes |
|---|---|---|
void |
| Exception | Cause | Fix |
|---|---|---|
fm::input_error |
Input contains newline character (\n or \r) |
Remove \n and \r occurrences when using append() or overwrite() |
fm::locked_error |
All attempts to save failed, manager entered read-only mode | Ensure nothing is locking the journal and main file and run the program with enough permissions |
#include <iostream>
#include "filemanager.h"
int main() {
fm::filemanager file("data.txt");
file.append("hello", "world", '!', 11);
std::cout << file.back();
}helloworld!11Overwrites a line with new text.
Overwrite is only stored in memory. Does not update the file (Check flush() and commit() for more info).
| Parameter | Datatype | Description | Notes |
|---|---|---|---|
index |
size_t |
Line to overwrite | Starts at 0 |
args |
Args... |
Values to overwrite the line with | Must not contain \n or \r and must be convertable to a string |
| Type | Description | Notes |
|---|---|---|
void |
| Exception | Cause | Fix |
|---|---|---|
fm::input_error |
Input contains newline character (\n or \r) |
Remove \n and \r occurrences when using append() or overwrite() |
fm::index_error |
Specified line does not exist | Check bounds with size() or empty() before access |
fm::locked_error |
All attempts to save failed, manager entered read-only mode | Ensure nothing is locking the journal and main file and run the program with enough permissions |
/*
Imagine a file 'data.txt' with following lines:
(1) Hello
(2) World
(3) !
*/
#include <iostream>
#include "filemanager.h"
int main() {
fm::filemanager file("data.txt");
file.overwrite(0, "Good", "bye");
std::cout << file.read(0);
}GoodbyeDeletes a line, shifting later lines down to fill the gap.
Delete is only stored in memory. Does not update the file (Check flush() and commit() for more info).
| Parameter | Datatype | Description | Notes |
|---|---|---|---|
index |
size_t |
Line to erase | Starts at 0 |
| Type | Description | Notes |
|---|---|---|
void |
| Exception | Cause | Fix |
|---|---|---|
fm::index_error |
Specified line does not exist | Check bounds with size() or empty() before access |
fm::locked_error |
All attempts to save failed, manager entered read-only mode | Ensure nothing is locking the journal and main file and run the program with enough permissions |
/*
Imagine a file 'data.txt' with following lines:
(1) Hello
(2) World
(3) !
*/
#include <iostream>
#include "filemanager.h"
int main() {
fm::filemanager file("data.txt");
file.erase(0);
std::cout << file.read(0) << "\n";
std::cout << file.size();
}World
2Delete all lines, effectively making the file empty.
Clear is only stored in memory. Does not update the file (Check flush() and commit() for more info).
| Type | Description | Notes |
|---|---|---|
void |
| Exception | Cause | Fix |
|---|---|---|
fm::locked_error |
All attempts to save failed, manager entered read-only mode | Ensure nothing is locking the journal and main file and run the program with enough permissions |
/*
Imagine a file 'data.txt' with following lines:
(1) Hello
(2) World
(3) !
*/
#include <iostream>
#include "filemanager.h"
int main() {
fm::filemanager file("data.txt");
file.clear();
std::cout << file.size();
}0Records all changes in a journal, making recovery possible after a crash.
Changes won't be written to the main file (see commit()). Can cause delays during program start if changes are not committed occasionaly.
| Type | Description | Notes |
|---|---|---|
bool |
Whether flushing was successful | Handle possible failures if the working directory is unstable |
#include <iostream>
#include "filemanager.h"
int main() {
fm::filemanager file("data.txt");
file.append("this data will be recovered!");
file.flush();
// Imagine abort() is called once here, regardless whether the program
// is restarted, to simulate a random crash and test the recovery.
for (std::string line : file.all()) {
std::cout << line << "\n";
}
}this data will be recovered!
this data will be recovered!First line was recovered from the journal and loaded into memory during construction. The object therefore started with the memory of the previous filemanager instance before the program crashed.
Second line was added just now because after running the program for the second time, we also call file.append("this data will be recovered!"); for the second time.
Deletes the journal and applies all changes to the main file.
Triggers reconstruction internals to make up for the new file. It's discouraged to repeatedly call commit(). It can have a noticeable impact on performance if used carelessly on big files.
| Type | Description | Notes |
|---|---|---|
bool |
Whether committing was successful | Handle possible failures if the working directory is unstable |
| Exception | Cause | Fix |
|---|---|---|
fm::delete_error |
Failed to delete journal file after successful commit | Delete journal file manually! Ensure that program is run with enough permissions and that nothing is locking the journal file |
#include <iostream>
#include "filemanager.h"
int main() {
fm::filemanager file("data.txt");
file.append("this data will be written!");
file.commit();
std::cout << file.front();
}this data will be written!
this data will be written!