Files.h
Description
The Files.h library is a compact, single-header C++ file-utilities module that provides line-oriented file operations and an in-memory vector<string> editing model. It offers simple, high-level functions for reading, writing, copying, moving, inserting, deleting, searching, updating, encrypting/decrypting, and converting files ↔ vectors of lines. Designed as a convenient toolkit for CLI tools, small scripts, or prototyping file manipulation logic without pulling in heavy libraries.
Technologies Used
-
Language: C++ (single-header style)
- Standard library:
<string>,<fstream> - Custom Utility Library:
Utils.h - Primary data model: line-based
vector<std::string>used as an editable in-memory representation of a file
Functionality / Features
- Basic I/O
prtFile(path)— print file contents tocout.clrFile(path)— truncate/clear a file.
- Copy / Move
copyFileToFile(src, dest, override=false)— copy file contents (append or overwrite).moveFileToFile(...)— copy then clear the source.copyFileToVct(src, vct, override=false)/moveFileToVct(...)— load file lines into avector<string>(optionally clearing vector).getVectorFromFile(path, clearSrc=false)— convenience that returns avector<string>.
- Save / Persist
copyVctToFile(vct, dest, override=false, copyEmptyLns=true)— write vector back to file with options.moveVctToFile(vct, dest, override=false)— write then clear the vector.
- Line-level editing
appendLnInFile(path, ln),insertLnInFile(path, pos, ln),insertFileInFile(srcPath, destPath, pos).updateLnInFile(path, lnPos, newLn)andupdateLnInFile(path, ln, newLn)— update by index or by matching line.delLnFromFile(path, lnPos)anddelLnFromFile(path, ln)— delete by index or by exact line (sets line to empty and writes back).
- String-level editing inside file
delStrFromFile(path, str)anddelStrFromFile(path, str, count)— remove substring occurrences.updateStrInFile(path, str, newStr)andupdateStrInFile(path, str, newStr, count)— replace occurrences (global or limited).
- Search & query helpers
isLnInFile(path, ln),isStrInLn(path, lnPos, str),isStrInFile(path, str).getLnFromFile(path, pos),getFirstLnPosInFile(path, ln),isFileEmpty(path).
- File encryption / decryption
encryptFile(path, key)/decryptFile(path, key)— appliesUtils::Encrypt_Decryptover each line and writes back.
- Return semantics
- Most functions return
boolindicating success/failure; query functions return booleans or data (e.g.,vector<string>,string,short) as appropriate.
- Most functions return
Implementation Highlights
- Line-first design: The library treats files as ordered collections of lines (
vector<string>) — this simplifies insert/update/delete operations and maps naturally to many CLI/text-processing use cases. - Consistent read-edit-write pattern: Most mutating operations use
copyFileToVct(...)→ modify thevector→copyVctToFile(...)sequence. That keeps the code straightforward and reduces duplication. - Small, composable primitives: Core functions (
copyFileToVct,copyVctToFile,appendLnInFile,prtFile) are simple and composable; higher-level behaviors (move, insert file, encrypt/decrypt) are built by combining them. - Flexible write modes:
copyFileToFileandcopyVctToFileacceptoverrideflags to choose overwrite vs append, andcopyVctToFilecan optionally skip empty lines — practical for varied file workflows. - String-level operations reuse standard
std::stringfunctions: substring search/erase/insert loops perform global or limited replacements/deletions directly on lines, keeping logic explicit and easy to trace. - Integration with Utils: Uses
Utils::Encrypt_Decryptfor lightweight per-line encryption/decryption and relies on the broader Utils toolset when necessary.
Conclusion
- Concrete, practical utility: File manipulation is a ubiquitous task; this project shows you can build reliable, reusable tooling that saves time across scripts and projects.
- Demonstrates API design & composition: The header exposes a small, consistent surface area (mostly bool-returning helpers) and composes simple primitives to implement more complex behaviors — a good example of pragmatic API design.
- Integrates knowledge across modules: It ties together file I/O,
vector<string>data modeling, string manipulation (fromStrings.h), and encryption (fromUtils.h), which highlights your ability to compose modules into useful tools.
How to Use
- Download the header file from the section below.
- Include it in your C++ project using
#include "...", with the appropriate path between the double quotes. - Make sure that you have
Utils.halso installed.
Source Code
You can find the source code for this header here.