Last updated:2026-10-12
C++17 and SQLite Folder Size Statistics¶
Project Practice: Folder Size Calculation and Storage with C++17 and SQLite¶
This section unfolds around a project background: suppose your business needs to periodically check the size of certain folders and persist the results. How would you accomplish this project in C++?
This section walks you through a small project that uses C++17 and SQLite to implement a folder size calculation and storage system, helping you manage your file system better.
Project Goals¶
We will implement the following features: 1. Recursively calculate the size of a specified directory and its subdirectories. 2. Save the calculation results to an SQLite database for easy subsequent querying and analysis. 3. Highly extensible: you can easily modify the project to add multi-threading support, optimize performance, or store the results in another database.
Note: lazy version — the complete code has been updated to the Knowledge Planet community.
Project Implementation Steps¶
1. Environment¶
First, you need to make sure the following tools are installed in your development environment: - A C++17-capable compiler: such as g++; you can check the version with the following command:
Make sure g++ supports the C++17 standard.- SQLite3: we will use SQLite for database operations; you can install it via a package manager:
After installation, the SQLite development library
libsqlite3-devensures we can use SQLite properly in C++.
2. Creating the Project Structure¶
We will split the code into several modules to keep a clear architecture:
- folder_size.h / folder_size.cpp: responsible for recursively calculating the size of directories and their subdirectories.
- database.h / database.cpp: encapsulates the SQLite database operation logic.
- main.cpp: the main program entry, responsible for controlling the overall flow.
The project directory structure is as follows:
3. Code Implementation¶
Next, we will implement each module step by step.
3.1 Directory Calculation Module¶
C++17 provides the std::filesystem library to recursively traverse directories and calculate sizes. So its core implementation is as follows:
uintmax_t total_size = 0;
for (const auto& entry : fs::recursive_directory_iterator(dir_path)) {
if (fs::is_regular_file(entry.path())) {
total_size += fs::file_size(entry.path());
}
}
If you want to calculate the root directory, just calculate it once more.
3.2 SQLite Database Operations¶
Include:
Usage:
sqlite3_close(db_); // 关闭
// 插入
for (const auto& [folder, size] : folder_sizes) {
const std::string insert_sql =
"INSERT OR REPLACE INTO FolderSizes (Folder, Size) "
"VALUES ('" + folder + "', " + std::to_string(size) + ");";
if (!ExecuteSQL(insert_sql)) {
return false;
}
}
// 执行
sqlite3_exec
4. Compiling and Running¶
Compiling the project is very simple:
Then run the program to calculate the directory size and store it in the database:
The program will recursively calculate the size of the specified directory and save the results to the folder_sizes.db database.
5. Querying the Database¶
Finally, you can query the contents of the database through
the SQLite command-line tool:
This will display all the saved directories and their corresponding sizes.
End of this section
Related Topics¶
- Hand-Writing C++ FnOnce | C++那些事
- A Small C++ gRPC Project | C++那些事
- C++ Inter-Process Communication in Practice | C++那些事
- C++ Short URL Service Demo | C++那些事
- Google Benchmark Performance Testing | C++那些事
- C++20 Coroutine HTTP Server | C++那些事
- Protobuf ZeroCopy Serialization | C++那些事
- Accelerating Python with pybind11 | C++那些事
- C++ Stack Calculator Design | C++那些事