//============================================================================
// Name        : FileAccess.cpp
// Author      : SaEeD
// Description : Read and Write to File using ISO C++ libraries
//============================================================================

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <exception>

using namespace std;

int main(int argc, char *argv[])
{
	if(argc != 3)
	{
		cerr << "Usage: " << argv[0] << " -r|-w <File name>" << endl;
		exit(-1);
	}

	if(string(argv[1]).compare("-r") == 0)
	{
		cout << "[+]Reading from File: " << argv[2] << endl;
		int line_num =0;
		try{
			ifstream InputFile(argv[2]);
			if(InputFile.is_open())
			{
				cout << "[--- File Contents ---]" << endl;
				string line;
				while(! InputFile.eof())
				{
					++line_num;
					getline(InputFile, line);
					cout << "Line " << line_num << ": " << line << endl;
				}
			}else{
				throw "Error";
			}


			InputFile.close();

		}catch(...)
		{
			cerr << "Exception: Cannot Open File " << endl;
		}



	}else if(string(argv[1]).compare("-w") == 0)
	{
		cout << "[+]Writing from File: " << argv[2] << endl;
		ofstream myOutput(argv[2], ios::app);
		if(myOutput.is_open())
		{
			cout << "[Please enter your input now]" << endl;
			string input;
			while(getline(cin, input))
			{
				myOutput << input << endl;
				cout << "[*]Line Written on a file[*]" << endl;
			}
			myOutput.close();
		}else{
			cerr << "Unable to create file."<< endl;
		}

	}else{
		cout << "[!]What the Fuck you wanna do? Read or Write? >:(" << endl;
		exit(-1);
	}


	return 0;
}