Variable Length Arrays

This is a discussion on Variable Length Arrays within the C++ Programming forums, part of the General Programming Boards category; Hello, Is there a way to make a (char) array with a variable length? I'm (kind of) working on an ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    45

    Variable Length Arrays

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

    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(&currentByte, 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(&currentByte, 1);
                   
                   //increments the pointer
                   file1.seekg (++index);
                   file2.seekp (index);
                   
                   //reads the next value
                   file1.read(&currentByte, 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;
    }
    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
    Posts
    907
    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
    Location
    Canada
    Posts
    3,183
    std::string for ... well... strings, and std::vector for other types.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    45
    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, 08:24 PM
  4. Need help
    By awkeller in forum C Programming
    Replies: 2
    Last Post: 12-09-2001, 02:02 PM
  5. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21