Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void write_data_to_file(void* data, FILE * data_file, int last_written_byte_position, int bytes_number_to_write, const string& current_file, int id) {
- long int start_position = ftell(data_file);
- fwrite(((void*)((char*)data + last_written_byte_position)), 1, bytes_number_to_write, data_file);
- long int end_position = ftell(data_file);
- save_file_name_and_reading_positions(id, current_file, start_position, end_position);
- }
- bool write_data(int id, void* data, int current_data_size) {
- FILE * data_file = NULL;
- string current_file;
- int last_written_byte_position = 0;
- while (current_data_size != 0) {
- if (data_file == NULL) {
- current_file = get_new_file_path();
- }
- data_file = fopen(current_file.c_str(), "a+b");
- unsigned int file_free_space = get_file_free_space(current_file);
- if (file_free_space == 0) {
- current_file = get_new_file_path();
- data_file = fopen(current_file.c_str(), "a+b");
- }
- int bytes_number_to_write;
- if (current_data_size > file_free_space) {
- bytes_number_to_write = file_free_space;
- write_data_to_file(data, data_file, last_written_byte_position, bytes_number_to_write, current_file, id);
- last_written_byte_position += bytes_number_to_write;
- }
- else {
- bytes_number_to_write = current_data_size;
- write_data_to_file(data, data_file, last_written_byte_position, bytes_number_to_write, current_file, id);
- }
- current_data_size -= bytes_number_to_write;
- is_data_size_invalid(current_data_size);
- fclose(data_file);
- data_file = NULL;
- }
- return true;
- }
- #define CATCH_CONFIG_MAIN
- #include "/Users/TwITe/Documents/Visual Studio 2017/Projects/Database/database_lib.h"
- #include "/Users/TwITe/Documents/Visual Studio 2017/Projects/catch.hpp"
- TEST_CASE("Data was succesfully writed to files", "[data_store]") {
- int arr[5]{ 0, 1, 2, 3, 4 };
- REQUIRE((write_data(1, arr, 20)) == true);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement