加密功能的问题未链接到主机 [英] Issue With Encryption Function Not Linking To Main

查看:153
本文介绍了加密功能的问题未链接到主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了教育的目的,我正在编写一小堆功能,打开一个文件作为二进制文件,一次读取一个字符,更改所述字符的值并写入一个新的加密文件。 (注意,这不是真正的加密。)



由于某些原因,我在调试器中的while循环条件中收到此错误消息: / p>

线程1:EXC_BAD_ACCESS(code = 1,address = 0x0)



我怀疑问题是理解指针。这是我的程序的全部。提前感谢。

  //此程序读取文件的内容,对其进行加密,并将内容写入单独的文件。 

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

使用namespace std;

//全局常量
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;

//函数原型
void encrypt(fstream * unEncryptedFile);
fstream * fileOpener();

int main(){

encrypt(fileOpener());

return 0;
}

/ ************************************ ****************
* fileOpener()*
*打开具有错误检查的文件,并显示*
*消息,如果错误,然后正常退出。 *
************************************************* ******* /
fstream * fileOpener(){

fstream inFile; //创建fstream对象
inFile.open(crypty.txt,ios :: in | ios :: binary); //打开文件


if(!inFile){
cout<<< 打开文件错误。 << ENDL;
返回NULL;

} else {

fstream * filePtr; //创建一个指向该文件的指针。

filePtr =& inFile; //将inFile的地址传递给指针。

return filePtr;
}


}


/ ******************* ********************************
* encrypt()*
*加密文件一个simlpe算法。 *
************************************************* ****** /
void encrypt(fstream& unEncryptedFile){

char ch,cy;
char * chPtr =& cy; //初始化并分配char指针。

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

int幅度= 0,slope = SLOPE_NEGATIVE; //设置初始幅度和斜率。

int streamSize = 1;

/ * while循环内的加密模式。

limit> 10 *
9 * *
8 * *
7 * *
6 * *
5 * *
4 * *
3 * *
2 * *
1 * *
开始> 0 * 2345678901234567890 * 2345678901234567890 * - <一个赫兹(周期)
-1 * *
-2 * *(数字行:每个整数表示一个循环周期)
-3 * *
-4 * *
-5 * *
-6 * *
-7 * *
-8 * *
-9 * *
limit> -10 *

* /
/ ****************************** *****************************
上面的模式表示单个字符
被读取,然后增加幅度值。

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

while(!unEncryptedFile.fail()){//< - 代码报告此语句的错误。

ch = unEncryptedFile.get(); //获取文件中的下一个字符。


if(amplitude> NEGATIVE_INT_LIMIT&& slope == SLOPE_NEGATIVE){
amplitude - = STEP;
cy = ch +幅度;
cryptFile.write(chPtr,streamSize); //调整后的字符值,cy被写入文件。

} else if(amplitude< = NEGATIVE_INT_LIMIT){
slope = SLOPE_POSITIVE;
幅度= NEGATIVE_INT_LIMIT;
cy = ch +幅度;
cryptFile.write(chPtr,streamSize); //调整后的字符值,cy被写入文件。


} else if(amplitude< POSITIVE_INT_LIMIT&& SLOPE_POSITIVE){
幅度+ = STEP;
cy = ch +幅度;
cryptFile.write(chPtr,streamSize); //调整后的字符值,cy被写入文件。

} else if(amplitude> = POSITIVE_INT_LIMIT){
slope = SLOPE_NEGATIVE;
amplitude = POSITIVE_INT_LIMIT;
cy = ch +幅度;
cryptFile.write(chPtr,streamSize); //调整后的字符值,cy被写入文件。
}

}

//文件关闭。
unEncryptedFile.close();
cryptFile.close();

}


解决方案


我怀疑问题是理解指针。


可能,几乎你是对的,但理解指向本地的指针变量



当您定义本地变量时:

  fstream INFILE; //创建fstream对象

inFile 在堆栈上。



具有以下内容:

  filePtr =& inFile 

...您将在 filePtr 。



当您退出该功能时:

  return filePtr; 

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



所以,为什么不只是返回 inFile 本身?


For the purpose of education, I'm writing a small pair of functions which open a file as a binary file, read each character one at a time, change the value of said character and write to a new, encrypted file. (Note, this isn't true encryption per se.)

For some reason, I'm getting this error message in my debugger at the while-loop condition:

Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)

I suspect the issue is with understanding pointer. Here is my program in it's entirety. Thanks ahead of time.

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

Probably, almost you right, but with understanding of pointers to local variables.

When you define local variables like this:

fstream inFile; // Create fstream object

inFile is allocated on the stack.

With following:

filePtr = &inFile

... you'll get pointer to some place on the stack in filePtr.

And when you exit that function:

return filePtr;

... stack portion, associated with function call will be returned to heap and filePtr will point to unused memory address, which will be overriden by next called function/executed code.

So, why not just return inFile itself?

这篇关于加密功能的问题未链接到主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆