Thread: Why does my code make the file 0 bytes?

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    96

    Why does my code make the file 0 bytes?

    Hi! When I try to encrypt/decrypt a text file, everything works properly, but when I try to decrypt a ".png" file it ends up not recording anything in it. Can someone please tell me why?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int Encryptmain ()
    {
        char f1[100];   // array big enough to hold expected input
        char f2[100];   // array big enough to hold expected input
    
        printf ("Please enter file for encryption\n");
        scanf  ("%99s", f1);    // set max characters to read to prevent buffer overflow
        printf ("Please enter the name of the file after encryption\n");
        scanf  ("%99s", f2);
    
        Encrypt(f1, f2);
        return 0;
    }
    
    void Encrypt(char * FILENAME, char * NEW_FILENAME)
    {
             printf("Encryption started\n");
             FILE *inFile;
             FILE *outFile;
             char key[50];         // array big enough to hold expected input
             char process[50];     // array big enough to hold expected input
             int keylen, keyidx;
    
             int Byte;       // fgetc returns int, so use an int
             int newByte;
    
             printf ("Please enter 'encryption or decryption'\n");
             scanf  ("%49s", process);
             printf ("Please enter the key\n");
             scanf  ("%49s", key);
             keylen = strlen(key);
             keyidx = 0;    // starting index into key
             printf("Opening files\n");
    
             inFile = fopen(FILENAME,"rb");
             outFile = fopen(NEW_FILENAME, "w");
    
             if(inFile == NULL)
             {
                printf("Error: Can't Open inFile\n");
             }
             else if(outFile == NULL)
             {
                printf("Error: Can't open outFile\n");
             }
             else
             {
                     printf("File Opened, Encrypting\n");
                     while((Byte = fgetc(inFile)) != EOF)    // read a byte, check if EOF
                     {
                             if (!strcmp(process,"encryption"))
                             {
                                     newByte = Byte + key[keyidx];    // use key index
                                     if (newByte > 255) newByte -= 256;   // check for overflow
                             }
                             else if (!strcmp(process,"decryption"))
                             {
                                     newByte = Byte - key[keyidx];
                                     if (newByte < 0) newByte += 256;
                             }
                             else
                             {
                                newByte = Byte;
                             }
                             fputc(newByte, outFile);
                             keyidx++;
                             // loop to the start of the key if needed
                             if (keyidx >= keylen) keyidx = 0;
                     }
             }
             if (inFile != NULL) fclose(inFile);     // close your files when you're done
             if (outFile != NULL) fclose(outFile);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ArakelTheDragon
    When I try to encrypt/decrypt a text file, everything works properly, but when I try to decrypt a ".png" file it ends up not recording anything in it. Can someone please tell me why?
    The first thing I would try is to change fopen(NEW_FILENAME, "w") to fopen(NEW_FILENAME, "wb"). It doesn't matter on some environments, e.g., Linux, but it could matter for yours.

    By the way, each of your functions should do one thing and do it well. For example, you could have a function dedicated to reading from the already open files and doing the encryption/decryption, another function responsible for opening the files and checking that they are indeed open, and another function to read the input from the user.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2016
    Posts
    96
    It worked, you have a beer from me .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Null bytes in code
    By Aslaville in forum Tech Board
    Replies: 2
    Last Post: 05-11-2015, 12:12 PM
  2. How do I make a constructor make code fail?
    By MutantJohn in forum C++ Programming
    Replies: 29
    Last Post: 08-31-2013, 06:59 PM
  3. Replies: 19
    Last Post: 01-04-2013, 11:20 PM
  4. Replies: 2
    Last Post: 01-02-2013, 11:03 PM
  5. Replies: 1
    Last Post: 12-31-2001, 05:04 PM

Tags for this Thread