From 98dda68415821901cae54aa13a60f6ce51a1af37 Mon Sep 17 00:00:00 2001 From: Oskar Date: Fri, 13 Sep 2024 16:06:18 +0200 Subject: some other tests --- read_print_file.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 73 insertions(+), 5 deletions(-) (limited to 'read_print_file.cpp') 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 #include #include -#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 vecbuf(1024); + std::vector 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 vecbuf(1024); + std::vector 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': @@ -104,11 +152,21 @@ 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; } -- cgit v1.2.3