diff options
author | Oskar <[email protected]> | 2024-09-13 16:06:18 +0200 |
---|---|---|
committer | Oskar <[email protected]> | 2024-09-13 16:06:18 +0200 |
commit | 98dda68415821901cae54aa13a60f6ce51a1af37 (patch) | |
tree | 7830c827e779d631d001c6f9e64247bf156f8211 | |
parent | f3551cb5e80efff6b50357c45e18833946756765 (diff) |
some other tests
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | README | 1 | ||||
-rw-r--r-- | read_print_file.cpp | 78 |
3 files changed, 75 insertions, 5 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1892925 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +rockyou.txt @@ -1 +1,2 @@ Just a little experiment +In conclusion, the first function is currently the fastest. diff --git a/read_print_file.cpp b/read_print_file.cpp index 5221c1d..a62c582 100644 --- a/read_print_file.cpp +++ b/read_print_file.cpp @@ -3,7 +3,8 @@ #include <fstream> #include <vector> #include <unistd.h> -#define USAGE "rpf [-1][-2][-3] [FILE]" +#define USAGE "rpf [-1][-2][-3][-4][-5] [FILE]" +#define VEC_BUF_SIZE 4096 void PrintFile(const std::filesystem::path &path) { @@ -37,7 +38,7 @@ void PrintFileBUFFERED_SINGLE_CHAR(const std::filesystem::path &path) { auto size = thisfile.tellg(); // get size thisfile.seekg(0, std::ios::beg); // seek to beginning of file while (!thisfile.eof()) { - std::vector<char> vecbuf(1024); + std::vector<char> vecbuf(VEC_BUF_SIZE); for(auto a = vecbuf.begin() ; a != vecbuf.end() ; ++a) { *a = thisfile.get(); } @@ -64,7 +65,7 @@ void PrintFileBUFFERED_READ(const std::filesystem::path &path) { auto size = thisfile.tellg(); // get size thisfile.seekg(0, std::ios::beg); // seek to beginning of file while (!thisfile.eof()) { - std::vector<char> vecbuf(1024); + std::vector<char> vecbuf(VEC_BUF_SIZE); thisfile.read(vecbuf.data(), vecbuf.size()); std::cout.write(vecbuf.data(), thisfile.gcount()); } @@ -73,6 +74,51 @@ void PrintFileBUFFERED_READ(const std::filesystem::path &path) { thisfile.close(); } +void PrintFileBUFFERED_READ_ARRAY(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()) { + char arrbuf[VEC_BUF_SIZE]; + thisfile.read(arrbuf, VEC_BUF_SIZE); + std::cout.write(arrbuf, thisfile.gcount()); + } + + std::cout << "File size: " << size << std::endl; + thisfile.close(); +} + +void NCAT(const std::filesystem::path &path) { + + FILE *file; + const char *filename = path.c_str(); + file = fopen(filename,"r"); + if (file == NULL) { + fprintf(stderr, "Cannot open file.\n"); + return; + } + + fseek(file, 0, SEEK_END); + auto size = ftell(file); + fseek(file, 0, SEEK_SET); + char buffer[VEC_BUF_SIZE]; + size_t bytes_read; + while ((bytes_read = fread(buffer, 1, VEC_BUF_SIZE, file)) > 0) { + fwrite(buffer, 1, bytes_read, stdout); + } + + std::cout << "File size: " << size << std::endl; + fclose(file); +} + inline void usageOUT() { std::cerr << USAGE << std::endl; } @@ -88,7 +134,9 @@ int main (int argc, char **argv) { int one = 0; int two = 0; int three = 0; - while ((opt = getopt(argc, argv, "123")) != -1) { + int four = 0; + int five = 0; + while ((opt = getopt(argc, argv, "12345")) != -1) { switch (opt) { case '1': @@ -105,10 +153,20 @@ int main (int argc, char **argv) { three = 1; break; + case '4': + + four = 1; + + break; + case '5': + + five = 1; + + break; } } - if((one + two + three) > 1) { + if((one + two + three + four + five) > 1) { usageOUT(); return EXIT_FAILURE; } @@ -133,6 +191,16 @@ int main (int argc, char **argv) { PrintFileBUFFERED_READ(path); std::cout << "PrintFileBR" << std::endl; } + + if(four == 1) { + PrintFileBUFFERED_READ_ARRAY(path); + std::cout << "PrintFileBRA" << std::endl; + } + + if(five == 1) { + NCAT(path); + std::cout << "NCAT" << std::endl; + } return EXIT_SUCCESS; } |