Skip to content

Commit

Permalink
Updated project and improved code :)
Browse files Browse the repository at this point in the history
**HAPPY 2017!**
  • Loading branch information
illescasDaniel committed Jan 3, 2017
1 parent b9af743 commit 4676659
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 70 deletions.
6 changes: 5 additions & 1 deletion File.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
65E0AEFA1D0DC5D6005554B0 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0730;
LastUpgradeCheck = 0820;
ORGANIZATIONNAME = "Daniel Illescas Romero";
TargetAttributes = {
65E0AF011D0DC5D6005554B0 = {
Expand Down Expand Up @@ -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 = "-";
Expand Down Expand Up @@ -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 = "-";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0730"
LastUpgradeVersion = "0820"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
33 changes: 13 additions & 20 deletions File/include/file.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef file_hpp
#define file_hpp
#pragma once

#include <fstream>

Expand All @@ -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 */
22 changes: 8 additions & 14 deletions File/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using namespace std;

int main(int argc, const char * argv[]) {
int main() {

// Write
File test1("rw.txt", ios::out);
Expand All @@ -17,7 +17,6 @@ int main(int argc, const char * argv[]) {
string read;

File::getline(test1,read);

cout << read << endl;

// Write
Expand All @@ -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;

Expand Down
18 changes: 4 additions & 14 deletions File/makefile
Original file line number Diff line number Diff line change
@@ -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*
46 changes: 26 additions & 20 deletions File/src/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,80 +5,86 @@ 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");
}
}

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);
}
void File::getline(File& file, string& name) {
std::getline(file.rw, name);
}

0 comments on commit 4676659

Please sign in to comment.