加密函数未链接到主函数的问题

Issue With Encryption Function Not Linking To Main

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

出于某种原因,我的调试器在 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();

}

I suspect the issue is with understanding pointer.

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

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

fstream inFile; // Create fstream object

inFile分配在栈上。

以下内容:

filePtr = &inFile

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

当您退出该功能时:

return filePtr;

... 与函数调用关联的堆栈部分将被 returned 到堆,filePtr 将指向未使用的内存地址,该地址将被下一个调用覆盖 function/executed代码。

那么,为什么不 return inFile 本身呢?