0

for educational purposes, I am writing a small encryption program (not real encryption). I've restructured the program so all my code is in main to simplify things for myself.

My unEncrypted file fails. I don't know why. Here's what I know so far.

  1. Using Xcode
  2. Tested statements by switching the order which the files are opened (and tested) to verify only the unencrypted file failing: it is
  3. Gone to Build Phases >> Copy Files >> Add File, adding both files to Absolute Path
  4. I've checked the spelling of the files and my code
  5. Changed the order again for good measure, still the unencrypted file fails
  6. Cleared any flags which may accidentally be there
  7. Rewound the read/write positions of the files

Here is my code in it's entirety.

//  This program reads the contents of a file, encrypts it, and write the contents into a separate file.


#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
#include <cmath>

using namespace std;

// Global Constants
const int POSITIVE_INT_LIMIT = 10;
const int NEGATIVE_INT_LIMIT = -10;
const int SLOPE_NEGATIVE = -1;
const int SLOPE_POSITIVE = 1;
const int STEP = 1;

int main() {

    int amplitude = 0, slope = SLOPE_NEGATIVE; //Set initial amplitude and slope.

    int streamSize = 1;

    char ch, cy;
    char *chPtr = &cy; // Initialize and assign char pointer.

    //open an unencrypted file to read from
    fstream unEncryptedFile;
    unEncryptedFile.open("testA", ios::in | ios::binary);

    //open a file to write encrypted info
    fstream cryptFile;
    cryptFile.open("testB", ios::out | ios::binary);

    //Clear flags previous set, just in case
    unEncryptedFile.clear();
    cryptFile.clear();

    //Rewind the files, just in case
    unEncryptedFile.seekg(0L, ios::beg);
    cryptFile.seekp(0L, ios::beg);

    if (unEncryptedFile.fail()) {
        cout << "Error opening read file." << endl;
        exit(1);
    }

    if (cryptFile.fail()) {
        cout << "Error opening write file." << endl;
        exit(1);
    }



    /*      Encryption pattern inside while-loop.

     limit>     10                             *
                9                             * *
                8                            *   *
                7                           *     *
                6                          *       * 
                5                         *         *
                4                        *           *
                3                       *             *
                2                      *               *
                1                     *                 *
     start>     0*2345678901234567890*2345678901234567890* -- < one hertz (cycle)
               -1 *                 *
               -2  *               *   (Number line: each integer represents a single while-loop cycle.)
               -3   *             *
               -4    *           *
               -5     *         *
               -6      *       *
               -7       *     *
               -8        *   *
               -9         * *
     limit>    -10         *

     */
    /*************************************************************
     The pattern above depictes a single character
     being read, and then the value of amplitude is added to it.

     *************************************************************/

    while (!unEncryptedFile.fail()) {

        ch = unEncryptedFile.get(); // Get the next character in the file.

        cout << ch << endl; // test for proper read

        if (amplitude > NEGATIVE_INT_LIMIT && slope == SLOPE_NEGATIVE) {
            amplitude -= STEP;
            cy = ch + amplitude;
            cryptFile.write(chPtr, streamSize); //Adjusted character value, cy, is written to file.

        } else if (amplitude <= NEGATIVE_INT_LIMIT){
            slope = SLOPE_POSITIVE;
            amplitude = NEGATIVE_INT_LIMIT;
            cy = ch + amplitude;
            cryptFile.write(chPtr, streamSize); //Adjusted character value, cy, is written to file.

        } else if (amplitude < POSITIVE_INT_LIMIT && SLOPE_POSITIVE){
            amplitude += STEP;
            cy = ch + amplitude;
            cryptFile.write(chPtr, streamSize); //Adjusted character value, cy, is written to file.

        } else if (amplitude >= POSITIVE_INT_LIMIT){
            slope = SLOPE_NEGATIVE;
            amplitude = POSITIVE_INT_LIMIT;
            cy = ch + amplitude;
            cryptFile.write(chPtr, streamSize); //Adjusted character value, cy, is written to file.
        }

    }

    //Files are closed.
    unEncryptedFile.close();
    cryptFile.close();



    return 0;
}
NonCreature0714
  • 5,744
  • 10
  • 30
  • 52
  • Consider [http://stackoverflow.com/questions/24097580/ifstreamis-open-vs-ifstreamfail](this) as fail-check best-practice, and use `while (!unEncryptedFile.eof())` as end-of-file check – ankhzet Dec 02 '15 at 05:59
  • @ankhzet, interestingly, using `while (!unEncryptedFile.eof())` created an infinite loop, with \377 as output... I don't know what to make of that one. – NonCreature0714 Dec 02 '15 at 06:08
  • Have you tried `unEncryptedFile.read(&ch, 1);` instead of `ch = unEncryptedFile.get();` in conjunction with `while (!unEncryptedFile.eof())`? – ankhzet Dec 02 '15 at 06:38
  • @ankhzet, I just tried it. Same result. But here's what is interesting - \377 and \376 keep showing up as values of ch and cy. Those are escape characters which mean "eof". Maybe, for some reason, there is an eof escape character before reading any text? – NonCreature0714 Dec 02 '15 at 06:47
  • @ankhzet, [here](http://stackoverflow.com/questions/13830338/377-character-in-c) is a post concerning \377 - though it doesn't explain why in the world my read position starts at eof, even after explicitly placing it at the beginning of the file – NonCreature0714 Dec 02 '15 at 06:49
  • It's possible, that file, opened with `unEncryptedFile.open("testA", ios::in | ios::binary)` is actually empty. Probably, when app launched, `"testA"` is expanded to some other file location than you intended. Maybe, specifying absolute path will help... – ankhzet Dec 02 '15 at 09:39
  • btw, `if (amplitude < POSITIVE_INT_LIMIT && SLOPE_POSITIVE)`, i belive, it must be `if (amplitude < POSITIVE_INT_LIMIT && slope == SLOPE_POSITIVE)` – ankhzet Dec 02 '15 at 09:44

1 Answers1

0

you need to provide the file extension with the file name while opening a file

int main() {

    int amplitude = 0, slope = SLOPE_NEGATIVE; //Set initial amplitude and slope.

    int streamSize = 1;

    char ch, cy;
    char *chPtr = &cy; // Initialize and assign char pointer.

    //open an unencrypted file to read from
    fstream unEncryptedFile;

    unEncryptedFile.open("testA.txt", ios::in | ios::binary);

I tried your code with this and it's working.

Shvet Chakra
  • 1,043
  • 10
  • 21
  • I just tried it with extensions and it's not working for me... Restarting compiler... And it's not failing to open no... So **something was going on with my compiler ** :/ – NonCreature0714 Dec 02 '15 at 06:06