diff --git a/File.xcodeproj/project.pbxproj b/File.xcodeproj/project.pbxproj index 3c335d1..63561da 100644 --- a/File.xcodeproj/project.pbxproj +++ b/File.xcodeproj/project.pbxproj @@ -115,7 +115,7 @@ 65E0AEFA1D0DC5D6005554B0 /* Project object */ = { isa = PBXProject; attributes = { - LastUpgradeCheck = 0730; + LastUpgradeCheck = 0820; ORGANIZATIONNAME = "Daniel Illescas Romero"; TargetAttributes = { 65E0AF011D0DC5D6005554B0 = { @@ -167,8 +167,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; CODE_SIGN_IDENTITY = "-"; @@ -211,8 +213,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; CODE_SIGN_IDENTITY = "-"; diff --git a/File.xcodeproj/xcuserdata/Daniel.xcuserdatad/xcschemes/File.xcscheme b/File.xcodeproj/xcuserdata/Daniel.xcuserdatad/xcschemes/File.xcscheme index 259d444..6e99ae9 100644 --- a/File.xcodeproj/xcuserdata/Daniel.xcuserdatad/xcschemes/File.xcscheme +++ b/File.xcodeproj/xcuserdata/Daniel.xcuserdatad/xcschemes/File.xcscheme @@ -1,6 +1,6 @@ @@ -16,26 +15,20 @@ class File { public: File(); - File(string name, ios_base::openmode mode); - - fstream & operator<< (string text); - fstream & operator>> (string & text); - - void open (string name, ios_base::openmode mode); - - ~File(); - - - string toString(); - static string toString(string name); - - static void toFile(string name, string text); + File(const string& name, const ios_base::openmode& mode); + void open(const string& name, const ios_base::openmode& mode); + + fstream & operator<<(const string& text); + fstream & operator>>(string& text); + + string getFileName() const; string getline(); - static void getline(File & file, string & name); + string toString(); - string getFileName(); + ~File(); + static string toString(const string& name); + static void saveTextTo(const string& fileName, const string& text); + static void getline(File& file, string& name); }; - -#endif /* file_hpp */ diff --git a/File/main.cpp b/File/main.cpp index d2ca42a..b06f086 100644 --- a/File/main.cpp +++ b/File/main.cpp @@ -3,7 +3,7 @@ using namespace std; -int main(int argc, const char * argv[]) { +int main() { // Write File test1("rw.txt", ios::out); @@ -17,7 +17,6 @@ int main(int argc, const char * argv[]) { string read; File::getline(test1,read); - cout << read << endl; // Write @@ -27,35 +26,30 @@ int main(int argc, const char * argv[]) { b << "test 1" << endl; - //cout << b.getline() << endl; - - // Read - b.open("rw2.txt",ios::in); + b.open("rw2.txt", ios::in); - cout << b.getline() << endl; cout << b.getline() << endl; string two; + b >> two; + cout << two << endl; + b >> two; cout << two << endl; // Read - File test2("rw2.txt",ios::in); + File test2("rw2.txt", ios::in); cout << test2.toString() << endl; - // Read and Write file in one line! - cout << File::toString("rw2.txt") << endl; - - File::toFile("test2.txt","hello\nprogrammer"); - + File::saveTextTo("test2.txt", "hello\nprogrammer"); // EMPTY - cout << "TEST: " << endl; + cout << "TEST EMPTY: " << endl; File test3; diff --git a/File/makefile b/File/makefile index 7bd953d..26298b4 100644 --- a/File/makefile +++ b/File/makefile @@ -1,16 +1,6 @@ -all: clean objects - @mkdir bin - @g++ obj/main.o obj/file.o -o bin/Program - @rm -rf obj - @printf "\nCompilation finished!\nGo to the 'bin' folder to open the program\n\n" - -objects: - @mkdir obj - @g++ -c -std=c++11 main.cpp -I include -o obj/main.o - @g++ -c -std=c++11 src/file.cpp -I include -o obj/file.o +all: clean + @g++ -std=c++14 -O3 -Wall -Wextra main.cpp src/file.cpp -I include -o fileProgram clean: - @rm -rf bin - @rm -rf obj - -# Easier compilation: g++ -std=c++11 main.cpp src/file.cpp -I include + @rm -rf *.txt + @rm -rf fileProgram* diff --git a/File/src/file.cpp b/File/src/file.cpp index 29883d9..fd02724 100644 --- a/File/src/file.cpp +++ b/File/src/file.cpp @@ -5,30 +5,34 @@ File::File() { name = ""; } -File::File(string name, ios_base::openmode mode) { - open(name,mode); +File::File(const string& name, const ios_base::openmode& mode) { + open(name, mode); } -fstream & File::operator<<(string text) { - if (rw.is_open()) +fstream & File::operator<<(const string& text) { + + if (rw.is_open()) { rw << text; + } return rw; } -fstream & File::operator>>(string & text) { - if (rw.is_open()) +fstream & File::operator>>(string& text) { + + if (rw.is_open()) { rw >> text; + } return rw; } -void File::open(string name, ios_base::openmode mode) { +void File::open(const string& name, const ios_base::openmode& mode) { this->name = name; close(); - rw.open(name,mode); + rw.open(name, mode); if (rw.fail()) { printf("File couldn't be open"); @@ -36,49 +40,51 @@ void File::open(string name, ios_base::openmode mode) { } void File::close() { - if (rw.is_open()) + if (rw.is_open()) { rw.close(); + } } File::~File() { close(); } -string File::getFileName() { +string File::getFileName() const { return name; } string File::getline() { string s; - std::getline(rw,s); + std::getline(rw, s); return s; } -void File::toFile(string name, string text) { - File write(name,ios::out); +void File::saveTextTo(const string& fileName, const string& text) { + File write(fileName, ios::out); write << text; } string File::toString() { - open(this->name,ios::in); + open(this->name, ios::in); string s2; - while(!rw.eof()) + while(!rw.eof()) { s2 += getline() + '\n'; + } return s2; } -string File::toString(string name) { +string File::toString(const string& name) { - File read(name,ios::in); + File read(name, ios::in); return read.toString(); } -void File::getline(File & file, string & name) { - std::getline(file.rw,name); -} \ No newline at end of file +void File::getline(File& file, string& name) { + std::getline(file.rw, name); +}