加密功能未链接到主

时间:2015-12-02 03:31:43

标签: c++ pointers encryption linker file-handling

出于教育的目的,我正在编写一对小函数,它们将文件作为二进制文件打开,一次读取一个字符,更改所述字符的值并写入新的加密文件。 (注意,这本身并不是真正的加密。)

出于某种原因,我在while循环条件下的调试器中收到此错误消息:

线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)

我怀疑问题在于理解指针。这是我的整个程序。提前谢谢。

//  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;

// Function Prototypes
void encrypt(fstream *unEncryptedFile);
fstream *fileOpener();

int main() {

    encrypt(fileOpener());

    return 0;
}

/****************************************************
 *                  fileOpener()                    *
 *   Opens file with error checking and displays    *
 *   message if error, then exiting gracefully.     *
 ****************************************************/
fstream *fileOpener(){

    fstream inFile; // Create fstream object
    inFile.open("crypty.txt", ios::in | ios::binary); // Open file


    if (!inFile) {
        cout << "Error opening file." << endl;
        return NULL;

    } else {

        fstream *filePtr; // create a pointer to the file.

        filePtr = &inFile; // Pass the address of inFile to the pointer.

        return filePtr;
    }


}


/***************************************************
 *                    encrypt()                    *
 *    Encrypts a file with a simlpe algorithm.     *
 ***************************************************/ 
void encrypt(fstream &unEncryptedFile){

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

    fstream cryptFile;
    cryptFile.open("decrypy.txt", ios::out | ios::binary);

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

    int streamSize = 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()) { // <--Code reports bug at this statement.

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


        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();

}

1 个答案:

答案 0 :(得分:0)

  

我怀疑问题在于理解指针。

可能,几乎是对的,但是要理解指向局部变量的指针。

当你定义这样的局部变量时:

fstream inFile; // Create fstream object

inFile在堆栈上分配。

以下:

filePtr = &inFile

...您将在filePtr中获得指向堆栈中某个位置的指针。

退出该功能时:

return filePtr;

...与函数调用相关联的堆栈部分将返回堆,filePtr将指向未使用的内存地址,该地址将被下一个被调用的函数/执行代码覆盖。

那么,为什么不直接返回inFile