Hello,
Is there a way to make a (char) array with a variable length?
I'm (kind of) working on an "encryption program" that takes a file and encrypts it using a ridiculously simple algorithm (if you can even call it that).
The problem is, when it asks the user to input the file name (including the extension and directories), if the file name is longer than the array 'name1' then there is an error that pops up after the program exits (even though everything encrypts and works out OK).
I really just need to know if I can create an array that has a variable length.Code:#include <iostream> #include <fstream> using namespace std; bool badfilename = false; class Encryption { fstream file1;//source file fstream file2;//destination file public: Encryption::Encryption(char* filename1, char* filename2) { file1.open(filename1, ios::in | ios::out | ios::binary); if(!file1.is_open()) { cout << "ERROR: Cannot find file with the name of " << filename1 << "!" << endl; badfilename = true; return; } if(badfilename) return; file2.open(filename2, ios::out|ios::binary); } //encrypts the file void Encrypt(void) { char currentByte; bool currentBit; int index = 0; //sets the pointers to the beginning of the file file1.seekg (0, ios::beg); file2.seekp (0, ios::beg); //reads the first value file1.read(¤tByte, 1); while(file1.good()) { //loop for four bits for(int c = 0; c < 4; c++) { //finds out if the first bit is a one currentBit = (int)((unsigned char)currentByte / 128); //shifts the byte over currentByte <<= 1; //if the first bit was a one then we add it to the end if(currentBit) { currentByte += 1; } } //writes the character file2.write(¤tByte, 1); //increments the pointer file1.seekg (++index); file2.seekp (index); //reads the next value file1.read(¤tByte, 1); } } //closes both of the files void close(void) { file1.close(); file2.close(); } }; int main( void ) { char name1[999999]; //IS THERE A WAY TO HAVE A VARIABLE LENGTH ARRAY?!!!!!! cout << "***ENCRYPTION PROGRAM***" << endl; cout << "Please enter the file name (including the extension):" << endl; cin>>name1; cout << endl; Encryption delta(name1, "output1.txt"); if(!badfilename) { delta.Encrypt();//Encrypt the file delta.close();//Close the file Encryption gamma("output1.txt", "output2.txt"); gamma.Encrypt();//Decrypt (by re-encrypting) output1.txt which is the encrypted version of the orignal file delta.close();//Again, close it. cout << "Succesfully encrypted file! \"output1.txt\" contains the encrypted data." << endl; //Happpy } else { cout << "Error opening file. Could not encrypt!" << endl;//Depressing } system("PAUSE"); return 0; }
If not, is there another more advanced data storage type?
Any and all help is appreciated!



LinkBack URL
About LinkBacks


