Skip to content

Commit ef68ee1

Browse files
committed
Added Reading/Writing Files with Bit String
1 parent 4934096 commit ef68ee1

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

Utils/BinaryIO.h

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@
44
#include <fstream>
55
#include <string>
66
#include "Converter.h"
7+
#include <bit_string.h>
78

89
namespace BinaryIO {
910

10-
bool doesFileExist(const std::string &filename){
11-
std::ifstream input(filename , std::ios::in | std::ios::binary);
11+
static const uint32_t BYTE = 8;
12+
13+
bool doesFileExist(const std::string& filename) {
14+
std::ifstream input(filename, std::ios::in | std::ios::binary);
1215
return input.good();
1316
}
1417

15-
int getFileSize(std::ifstream &input){
16-
input.seekg(0 , std::ios::end);
18+
int getFileSize(std::ifstream& input) {
19+
input.seekg(0, std::ios::end);
1720
return input.tellg();
1821
}
1922

20-
int getFileSize(const std::string &filename){
23+
int getFileSize(const std::string& filename) {
2124
std::ifstream input(filename);
2225
return getFileSize(input);
2326
}
@@ -42,6 +45,26 @@ namespace BinaryIO {
4245
return readString(filename, 0, getFileSize(filename));
4346
}
4447

48+
bit_string readBitString(const std::string& filename, int startPosition, int length) {
49+
std::ifstream input(filename, std::ios::in | std::ios::binary);
50+
bit_string fileData;
51+
if (input) {
52+
input.seekg(startPosition);
53+
fileData.resize(length * BYTE);
54+
input.read((char*) fileData.data(), fileData.length_in_bytes());
55+
input.close();
56+
}
57+
return fileData;
58+
}
59+
60+
bit_string readBitString(const std::string& filename, int startPosition) {
61+
return readBitString(filename, startPosition, getFileSize(filename) - startPosition);
62+
}
63+
64+
bit_string readBitString(const std::string& filename) {
65+
return readBitString(filename, 0, getFileSize(filename));
66+
}
67+
4568

4669
// Append binaryData to the end of the file for strings
4770
void write(const std::string& filename, const std::string& binaryData) {
@@ -53,6 +76,13 @@ namespace BinaryIO {
5376
output.close();
5477
}
5578

79+
// Append binaryData to the end of the file for strings
80+
void write(const std::string& filename, const bit_string& binaryData) {
81+
std::ofstream output(filename, std::ios::out | std::ios::binary | std::ios::app);
82+
output.write((char*) binaryData.data(), binaryData.length_in_bytes());
83+
output.close();
84+
}
85+
5686
// Append binaryData to the end of the file for integers
5787
void write(const std::string& filename, int binaryData) {
5888

0 commit comments

Comments
 (0)