File Encryption & Read/Write in Binary Mode

This is a discussion on File Encryption & Read/Write in Binary Mode within the C++ Programming forums, part of the General Programming Boards category; Hi, I am working on an encryption program. There are two versions. One is text encryption and the second is ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    File Encryption & Read/Write in Binary Mode

    Hi,
    I am working on an encryption program. There are two versions. One is text encryption and the second is binary encrytion. The text encryption is fairly simply. The downside to the text version is that is only works on text files. One the other hand, I cannot get the binary encryption program to work.

    There are two problems. First, I cannot copy complete raw data from any file and save it into a character array. I have tried giving the array a specific size and I have tried dynamically allocating the size of the array based on the size of the ifstream.

    ifstream openFile;
    openFile.open(x, ios::in | ios::binary);
    openFile.seekg(0, ios::end);
    size = openFile.tellg();

    char *temp;
    temp = new char(size);

    openFile.read((char *) temp, sizeof(temp));

    The code above does work, however, "sizeof(temp)" always output a 4 even if its subscript is 1000. the char * does not affect it because I saw the same number even if I use:

    char temp[100];

    How do you read raw data (binary) from any file and save it in memory so you can make specific encryption and then write it back in binary mode?

    Thanks,
    Kuphryn

  2. #2
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    you are using it to find the size of a pointer, which is 32-bits, 4 bytes...

    use filelength() for handles, and convert streams to handles using fileno ()...
    hasafraggin shizigishin oppashigger...

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Thanks.

    Can you post a quick example? I am not familiar with filelength() and fileno() functions.

    Kuphryn

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Using dynamic allocation:
    Code:
    int main(void)
    {
       long size;
       char *filename = "filename1.txt";
       char *out_filename = "filename2.txt";
       ifstream openFile;
       openFile.open(filename, ios::in | ios::binary);
       if (!openFile)
       {
          cout << "Unable to open file:" << filename << endl;
          return 0;
       }
       openFile.seekg(0, ios::end); 
       size = openFile.tellg();
       cout << "size of file " << filename << ":" << size << endl;
    
       char *temp;
       temp = new char[size];
    
       openFile.read((char *) temp, size);
    
       ofstream out(out_filename,ios::binary);
       out.write((char *) temp, size);
       openFile.close();
       out.close();
    
       delete []temp;
       return 0;
    }
    Also, you can declare a static array:
    char temp[2700];
    cout << "sizeof temp:" << sizeof(temp) << endl;

    sizeof() here will be 2700. However if temp is passed to a function, then sizeof() will be the size of a pointer.
    Last edited by swoopy; 11-30-2001 at 06:32 PM.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    ahhhhhhh. I think I know what is going on.

    I have assumed that sizeof() was required in functions read/write:

    x.read((char *) temp, sizeof(temp));

    I did not know an int or long could replace sizeof(temp).

    Thanks!

    Kuphryn

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Right. For example, you can do this:

    const long size = 200;
    .
    .
    char temp[size];
    x.read((char *) temp, size);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print file in binary mode
    By vikernes in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2006, 12:43 AM
  2. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 11:51 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. copying binary file
    By samc2004 in forum C Programming
    Replies: 5
    Last Post: 12-09-2003, 01:34 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

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