Skip to content

Instantly share code, notes, and snippets.

@haseeb-heaven
Created January 3, 2025 19:43
Show Gist options
  • Save haseeb-heaven/5716f3eb7781b4500efc98ce11574367 to your computer and use it in GitHub Desktop.
Save haseeb-heaven/5716f3eb7781b4500efc98ce11574367 to your computer and use it in GitHub Desktop.
CRUD Operations with std::print and clean optimised code with binary file support using latest C++ 23.
#include <fstream>
#include <string>
#include <print> # C++ 23 print
#include <stdexcept>
bool is_binary_file(const std::string& filename) {
std::ifstream input_file_stream(filename, std::ios::binary);
if (!input_file_stream) {
throw std::runtime_error("Could not open file '" + filename + "'.");
}
char buffer[1024];
input_file_stream.read(buffer, sizeof(buffer));
std::streamsize bytes_read = input_file_stream.gcount();
input_file_stream.close();
if (bytes_read == 0) return false;
size_t non_printable_count = 0;
for (std::streamsize i = 0; i < bytes_read; ++i) {
unsigned char character = static_cast<unsigned char>(buffer[i]);
if (character < 32 || character > 126) {
non_printable_count++;
}
}
return non_printable_count > (bytes_read / 4);
}
std::string read_data(const std::string& filename) {
bool binary_file = is_binary_file(filename);
std::ifstream input_file_stream(filename, std::ios::in | (binary_file ? std::ios::binary : std::ios::in));
if (!input_file_stream) {
throw std::runtime_error("Could not open file '" + filename + "' for reading.");
}
std::string file_data;
if (binary_file) {
input_file_stream.seekg(0, std::ios::end);
std::streamsize file_size = input_file_stream.tellg();
input_file_stream.seekg(0, std::ios::beg);
file_data.resize(file_size);
if (!input_file_stream.read(&file_data[0], file_size)) {
throw std::runtime_error("Could not read binary data from file '" + filename + "'.");
}
} else {
std::string line;
while (std::getline(input_file_stream, line)) {
file_data += line + '\n';
}
}
input_file_stream.close();
return file_data;
}
void write_data(const std::string& filename, const std::string& data, bool binary_mode = false) {
std::ofstream output_file_stream(filename, std::ios::out | (binary_mode ? std::ios::binary : std::ios::out | std::ios::trunc));
if (!output_file_stream) {
throw std::runtime_error("Could not open file '" + filename + "' for writing.");
}
if (!output_file_stream.write(data.data(), data.size())) {
throw std::runtime_error("Could not write data to file '" + filename + "'.");
}
output_file_stream.close();
std::print("Data written to file '{}'.\n", filename);
}
void update_data(const std::string& filename, const std::string& new_data) {
bool binary_file = is_binary_file(filename);
std::ofstream output_file_stream(filename, std::ios::out | std::ios::app | (binary_file ? std::ios::binary : std::ios::out));
if (!output_file_stream) {
throw std::runtime_error("Could not open file '" + filename + "' for updating.");
}
if (!output_file_stream.write(new_data.data(), new_data.size())) {
throw std::runtime_error("Could not append data to file '" + filename + "'.");
}
output_file_stream.close();
std::print("Data appended to file '{}'.\n", filename);
}
void delete_data(const std::string& filename) {
write_data(filename, "", false);
std::print("Data deleted from file '{}'.\n", filename);
}
int main() {
const std::string filename = "log.txt";
try {
std::string file_contents = read_data(filename);
std::print("Read data:\n{}\n", file_contents);
}
catch (const std::exception& error) {
std::print(stderr, "Exception: {}\n", error.what());
}
return 0;
}
@haseeb-heaven
Copy link
Author

Compile and Run using C++ 23 compiler.

Command:
clang++ -std=c++23 -w -o cpp_print crud_file.cpp
./crud_file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment