diff options
author | Oskar <[email protected]> | 2024-09-13 17:10:20 +0200 |
---|---|---|
committer | Oskar <[email protected]> | 2024-09-13 17:10:20 +0200 |
commit | 8d2c11b626736648c86ad09865118a2da9a0149d (patch) | |
tree | 9e2a6dc4406ff755ea0281dc9e87f4134c675946 | |
parent | 98dda68415821901cae54aa13a60f6ce51a1af37 (diff) |
Tried some more ideas, pretty much the same peformance though.
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | read_print_file.cpp | 45 |
2 files changed, 40 insertions, 6 deletions
@@ -1 +1,2 @@ rockyou.txt +rockyou_10x.txt diff --git a/read_print_file.cpp b/read_print_file.cpp index a62c582..327c500 100644 --- a/read_print_file.cpp +++ b/read_print_file.cpp @@ -3,8 +3,8 @@ #include <fstream> #include <vector> #include <unistd.h> -#define USAGE "rpf [-1][-2][-3][-4][-5] [FILE]" -#define VEC_BUF_SIZE 4096 +#define USAGE "rpf [-1][-2][-3][-4][-5][-6] [FILE]" +#define VEC_BUF_SIZE 1048576 // 1 MiB void PrintFile(const std::filesystem::path &path) { @@ -20,12 +20,12 @@ void PrintFile(const std::filesystem::path &path) { std::string data(size, '\0'); // make string of appropriate size thisfile.seekg(0, std::ios::beg); // seek to beginning of file thisfile.read(&data[0], size); // read entire file in to string - std::cout << data; + std::cout.write(&data[0], size); std::cout << "File size: " << size << std::endl; thisfile.close(); } -void PrintFileBUFFERED_SINGLE_CHAR(const std::filesystem::path &path) { +void PrintFileBUFFERED_SINGLE_CHAR(const std::filesystem::path &path) { // extremely slow std::ifstream thisfile; thisfile.open(path); @@ -96,6 +96,28 @@ void PrintFileBUFFERED_READ_ARRAY(const std::filesystem::path &path) { thisfile.close(); } +void PrintFileBUFFERED_READ_STRING(const std::filesystem::path &path) { + + std::ifstream thisfile; + thisfile.open(path); + if(!thisfile.is_open()) { + std::cout << "Cannot open file" << std::endl; + return; + } + + thisfile.seekg(0, std::ios::end); // seek to end + auto size = thisfile.tellg(); // get size + thisfile.seekg(0, std::ios::beg); // seek to beginning of file + while (!thisfile.eof()) { + std::string strbuf(VEC_BUF_SIZE, '\0'); + thisfile.read(&strbuf[0], VEC_BUF_SIZE); + std::cout.write(&strbuf[0], thisfile.gcount()); + } + + std::cout << "File size: " << size << std::endl; + thisfile.close(); +} + void NCAT(const std::filesystem::path &path) { FILE *file; @@ -136,7 +158,8 @@ int main (int argc, char **argv) { int three = 0; int four = 0; int five = 0; - while ((opt = getopt(argc, argv, "12345")) != -1) { + int six = 0; + while ((opt = getopt(argc, argv, "123456")) != -1) { switch (opt) { case '1': @@ -163,10 +186,15 @@ int main (int argc, char **argv) { five = 1; break; + case '6': + + six = 1; + + break; } } - if((one + two + three + four + five) > 1) { + if((one + two + three + four + five + six) > 1) { usageOUT(); return EXIT_FAILURE; } @@ -201,6 +229,11 @@ int main (int argc, char **argv) { NCAT(path); std::cout << "NCAT" << std::endl; } + + if(six == 1) { + PrintFileBUFFERED_READ_STRING(path); + std::cout << "PrintFileBRS" << std::endl; + } return EXIT_SUCCESS; } |