diff --git a/fileoperations.h b/fileoperations.h index dc643ab..b5cb949 100644 --- a/fileoperations.h +++ b/fileoperations.h @@ -1,78 +1,110 @@ - +// This header uses 'const char *' for the filepath input #ifndef FILEREADING_FILEOPERATIONS_H #define FILEREADING_FILEOPERATIONS_H +#include #include #include -#include "ostream" +#include +#include class fileoperate{ public: - fileoperate(std::string basefilepath){ + fileoperate(const char *basefilepath){ //Constructor no_of_lines = 0; - filepath = basefilepath.c_str() ; + filepath = basefilepath; - if (file_valid() == true){ + if (file_valid() != true){ std::cout << "DIRECTORY ERROR" << std::endl; }else { - std::cout << "Do you want to create file? y/n" ; + std::cout << "Do you want to create file? y/n: " ; std::string reply; std::cin >> reply ; std::cout << std::endl; if (reply == "y" | reply == "Y") { try { - std::ofstream h(basefilepath); + std::cout << "Input Filename: " ; + std::cin >> filename; + + strcpy(finalpath, basefilepath); + strcat(finalpath, filename); // The only place I do not use 'convert_finalpath()' function. + + std::ofstream h(finalpath, std::ios_base::out); h.close(); } catch (...) { std::cout << "Oops! Couldn't create file in " << basefilepath << " directory" << std::endl; } }else if (reply == "n" | reply == "N"){ - std::cout << "No file created" << std::endl; + std::cout << "No file created \n"; + + std::cout << "Do you wish to edit a file? y/n: "; + std::string reply2; + std::cin >> reply2; + + if(reply2 == "y" | reply2 == "Y"){ + std::cout << "Input Filename: " ; + std::cin >> filename; + }else if(reply2 == "n" | reply2 == "N"){ + exit(-2); + }else{ + std::cout << "INVALID PARAMETER \n"; + } }else{ std::cout << "INVALID PARAMETER" << std::endl; } } + //Set-up 'finalpath' variable + convert_finalpath(finalpath); + std::cout << finalpath; } void nline(){ - no_lines(); + lines_counter(); std::cout << "lines: " << no_of_lines << std::endl; } - void write(std::string text, std::string mode){ - if (mode == "a") { - std::ofstream w(filepath, std::ios_base::app); - w << std::endl; + void write(std::string text, char mode){ + // "a" = Append, "w" = Overwrite + if (mode == 'a') { + std::ofstream w(finalpath, std::ios_base::app); + + //Only start writing on a newline if the file is NOT empty + if(!filepath_empty(finalpath)) + w << std::endl; + w << text ; - }else if (mode == "w"){ - std::ofstream w(filepath); + std::cout << "Successfully written to " << filename << "\n"; + }else if (mode == 'w'){ + std::ofstream w(finalpath); w << text ; + + std::cout << "Successfully overwrote " << filename << "\n"; } } - void merge(std::string filepath2){ - + void merge(const char *filepath2){ std::string line; - std::ifstream r(filepath2, std::ios_base::in); + std::ifstream m(filepath2, std::ios_base::in); std::cout << "Merging file " << filepath2 << " to " << filepath << std::endl; - while (!r.eof()){ - std::getline(r, line); - write(line , "a"); + while (!m.eof()){ + std::getline(m, line); + write(line , 'a'); // Append string from 'r' to file in our basefilepath } - std::cout << "Done" << std::endl; + std::cout << "Merging Done" << std::endl; + m.close(); } void readfile(){ std::string line; - std::ifstream r(filepath, std::ios_base::in); + std::ifstream r(finalpath, std::ios_base::in); while(!r.eof()){ getline(r, line); @@ -83,11 +115,13 @@ class fileoperate{ } private: - std::string filepath; + const char *filepath; + char filename[50]; + char finalpath[200]; int no_of_lines; - void no_lines(){ - std::ifstream l(filepath, std::ios_base::in); + void lines_counter(){ + std::ifstream l(finalpath, std::ios_base::in); std::string ss; while (!l.eof()) { @@ -101,7 +135,7 @@ class fileoperate{ bool file_valid(){ std::ifstream check(filepath, std::ios_base::in); - if (check.fail()){ + if (check.bad()){ //.fail() => Dir.Err for some reason return false; }else{ return true; @@ -109,6 +143,19 @@ class fileoperate{ check.close(); } + + bool file_empty(std::ifstream& file){ + return file.peek() == std::ifstream::traits_type::eof(); + } + bool filepath_empty(const char *_filepath){ + std::ifstream checkfile(_filepath, std::ios_base::out); + return checkfile.peek() == std::ifstream::traits_type::eof(); + } + + char convert_finalpath(char final_p[200]){ + strcpy(final_p, filepath); + strcat(final_p, filename); + } }; #endif diff --git a/main.cpp b/main.cpp index 5cac638..00571a9 100644 --- a/main.cpp +++ b/main.cpp @@ -1,8 +1,46 @@ -#include +//DEMO +#include #include "fileoperations.h" -using namespace std; -int main() { - fileoperate heavy("file location"); - return 0; +int main(){ + char* path; + std::cout << "What directory are you working in? (Enter Filepath. Add '\\' at the end)\n >>> "; + std::cin >> path; + + fileoperate fileop(path); + std::string txt; + + //Using the functions + fileop.nline(); + + std::cout << "\nWhat would you like to do to the file?\n"; + std::cout << "A = Append to File; \nO = Overwrite File; \nM = Merge with another file; \nR = Read a File\n >>> "; + + char reply; + std::cin >> reply; + + if(reply == 'a' | reply == 'A'){ + //Append + std::cout << "Input Text: "; + std::cin >> txt; + fileop.write(txt, 'a'); + } + if(reply == 'o' | reply == 'O'){ + //Overwrite + std::cout << "Input Text: "; + std::cin >> txt; + fileop.write(txt, 'w'); + } + if(reply == 'm' | reply == 'M'){ + //Merge + std::cout << "Input Directory(Filepath) of the file to be merged: "; + std::cin >> txt; + fileop.merge(txt.c_str()); + } + if(reply == 'r' | reply == 'R'){ + //Read + fileop.readfile(); + } + + return 0; }