Sorry, but I REALLY don't understand what could possibly be wrong with my code (or maybe it's not my code?).
When I run the writeChar(char*,char) function, my program crashes. I really don't have any idea why, as my pointers (I think, I've checked, but I'm not very good at that) are all initialized by the control reaches the writeChar function.
My code is below.
Functions.h:
#pragma once
#include <fstream>
#include <string.h>
#define nullptr 0
using namespace std;
char newline = '\n';
char supportedChars[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','x','y','z', \
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','X','Y','Z', \
'1','2','3','4','5','6','7','8','9','0' \
,newline,'\t','\\','/','.',',','?','!','@','#','$','%','^','&','*','(',')','-','_','=','+','~','`','<','>','{','[','}',']', '\0'
};
class info
{
public:
bool eof;
bool good;
bool fail;
bool bad;
char r;
};
info getNextChar(char* to);//read the next char in binary form.
void writeChar(char* to,char x);//writes to the write buffer.
void flush(char* to) {} //writes to the file, and cleans up (if needed)
char rotate(char x);//checks that it's supported, and increments it 13 times.
void all();
char rotate(char x) //Done -- works
{
//No need to check them, as the char type suppots all (?)
return (x+13);
}
ifstream& openFile(char* pszFileName)//utility -- works
{
ifstream* pFileStream = nullptr;
pFileStream = new ifstream(pszFileName);
if (pFileStream->good())
{
pFileStream->seekg(0);
}
return *pFileStream;
}
ofstream& openFile2(char* pszFileName)//utility -- works
{
ofstream* pFileStream = nullptr;
pFileStream = new ofstream(pszFileName);
return *pFileStream;
}
info getNextChar(char* to)//Works
{
static ifstream& in= openFile(to);
char r;
in>>r;
if(in.good() == false)
cerr<<"Note that the file open with errors, or some other part of it completed with errors."<<endl;
info i;
i.eof = in.eof();
i.good = in.good();
i.fail = in.fail();
i.bad = in.bad();
i.r = r;
return i;
}
void writeChar(char* to, char x)
{
char* dest = "2";
strcat (dest,to);
cout<<"writing to file: "<<dest<<endl;
static ofstream& in = openFile2(to);
in << x;
}
void all(char* to)
{
info i;
bool good;
//loop through all of the chars.
writeChar(to, 'w');
}
main.cpp
#include <iostream>
//#include <stdio.h>
//#include <stdlib.h>
#include "Functions.h"
#define nullptr 0
using namespace std;
int main(/*int nNumberOfArgs, char* pszArgs[]*/)
{
char* pszArgs[] = {"NAME","Test.txt"};
cout<<"Encrypt Using ROT13"<<endl;
cout<<"Rotate file: "<<pszArgs[1]<<"\n[Y/N] >";
char yn;
cin>> yn;
if(yn == 'Y'|| yn == 'y')
{
//Yes
//run the rotation
all(pszArgs[1]);
}
else
{
char* path = nullptr;
cout<<"You have entered no.\nPlease enter correct file. Enter '0' to exit\n>";
cin >> path;
if(path == "0"||path == "'0'")
{
flush(pszArgs[1]);
cout<<"Exiting. Please note that the program has not encrypted anything yet."<<endl;
// exit(2);//exits with code 2, meaning that this option was used.
}
//re-run the check.
bool yes = false;
while(!yes)
{
cout<<"Rotate file: "<<pszArgs[1]<<"\n[Y/N] >";
cin >> yn;
if(yn == 'Y'|| yn == 'y')
{
//Yes
//run the rotation
yes =true;
all(pszArgs[1]);
}
else
{
yes = false;
continue;
}
}
}
}
This is meant to be a very simple ROT13 program. (Meaning that to encrypt a file, it rotates all of the chars 13 spots.)
Another strange thing is that when I do certain things (can't find the source of the problem here), Windows opens up and says that it can't find a certain dll. The dll is related to g++, my compiler (well, MinGW, but that's just a port of g++ so...).
The whole supported chars but is useless... I really don't know why I made it... took me forever!
Thanks,
Jack