Thread: Variable Length Arrays

  1. #1
    Registered User
    Join Date
    Apr 2009

    Variable Length Arrays

    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).

    #include <iostream>
    #include <fstream>
    using namespace std;
    bool badfilename = false;
    class Encryption
         fstream file1;//source file
         fstream file2;//destination file
         Encryption::Encryption(char* filename1, char* filename2)
    , ios::in | ios::out | ios::binary);
    			  cout << "ERROR: Cannot find file with the name of " << filename1 << "!" << endl;
    			  badfilename = true;
    , 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
    , 1);
                   //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
                             currentByte += 1;
                   //writes the character               
                   file2.write(&currentByte, 1);
                   //increments the pointer
                   file1.seekg (++index);
                   file2.seekp (index);
                   //reads the next value
         , 1);
         //closes both of the files
         void close(void)
    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;
    	cout << endl;
        Encryption delta(name1, "output1.txt");
        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
    		cout << "Error opening file. Could not encrypt!" << endl;//Depressing
        return 0;
    I really just need to know if I can create an array that has a variable length.
    If not, is there another more advanced data storage type?

    Any and all help is appreciated!
    Last edited by Flaug; 04-16-2009 at 09:37 PM.

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Yes, there is. std::string is just what you need. string - C++ Reference
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    Dec 2006
    std::string for ... well... strings, and std::vector for other types.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Thanks for all the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird modification to string length
    By ChwanRen in forum C Programming
    Replies: 0
    Last Post: 08-17-2003, 10:45 AM
  2. How to declare global arrays of unknown length?
    By ajm218 in forum C Programming
    Replies: 3
    Last Post: 08-07-2003, 09:13 AM
  3. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM
  4. Need help
    By awkeller in forum C Programming
    Replies: 2
    Last Post: 12-09-2001, 03:02 PM
  5. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM