Skip to content

Instantly share code, notes, and snippets.

@theoremoon
Created May 7, 2014 09:54
Show Gist options
  • Save theoremoon/9bfb32d6b496d21ba549 to your computer and use it in GitHub Desktop.
Save theoremoon/9bfb32d6b496d21ba549 to your computer and use it in GitHub Desktop.
CaesarEncodeProgram
#include <iostream>
#include <fstream>
#include <string>
#include "head/ReadFile.h"
using namespace std;
void decode(const char *filepath){
std::string file = ReadFile(filepath);
unsigned int size = file.length();
for(int i = 0; i < size; ++i){
file.at(i) -= 10;
}
fstream fs("decoded.txt", std::ios::out);
if(!fs){
cout << "ERROR" << endl;
}
fs << file << endl;
return;
}
int main(int argc, char *argv[]){
if(argc <= 1){
cout << "Finish." << endl;
return 0;
}
if(strcmp(argv[1], "decode") == 0){
decode(argv[2]);
return 0;
}
string file;
file = ReadFile(argv[argc-1]);
unsigned int size = file.length();
for(int i = 0; i < size; ++i){
file.at(i) += 10;
}
fstream fs("encoded.txt", std::ios::out);
if(!fs){
cout << "ERROR" << endl;
}
fs << file << endl;
return 0;
}
#pragma once
#include <fstream>
#include <string>
void ReadFile( const char* FileName, char* StrBuf ){
std::ifstream ifs( FileName, std::ifstream::binary );
int len;
ifs.seekg( 0, ifs.end );
len = static_cast< int >( ifs.tellg() );
ifs.seekg( 0, ifs.beg );
StrBuf = new char[ len ];
ifs.read( StrBuf, len );
return;
}
std::string ReadFile( const char* FileName ){
std::ifstream ifs( FileName, std::ifstream::binary );
int len;
char* StrBuf;
std::string str;
ifs.seekg( 0, ifs.end );
len = static_cast< int >( ifs.tellg() );
ifs.seekg( 0, ifs.beg );
StrBuf = new char[ len ];
ifs.read( StrBuf, len );
str = StrBuf;
delete[] StrBuf;
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment