C++ 在Linux上加密目录中的文件时出现问题
我已经测试了程序的加密部分,它可以很好地用于单个文件,甚至“file1、file2、file3”之类的东西,但不能用于目录。代码在我看来很好,但是在执行时会出现分段错误 它应该对目录中的文件进行加密,并使用新的扩展名(旧名+“.wbf”)将它们写入同一目录中,然后使用相反的扩展名删除进行解密。我只想发布处理文件的部分代码,处理单个文件的do_crypt函数工作得很好,我认为这不是我问题的根源C++ 在Linux上加密目录中的文件时出现问题,c++,linux,segmentation-fault,C++,Linux,Segmentation Fault,我已经测试了程序的加密部分,它可以很好地用于单个文件,甚至“file1、file2、file3”之类的东西,但不能用于目录。代码在我看来很好,但是在执行时会出现分段错误 它应该对目录中的文件进行加密,并使用新的扩展名(旧名+“.wbf”)将它们写入同一目录中,然后使用相反的扩展名删除进行解密。我只想发布处理文件的部分代码,处理单个文件的do_crypt函数工作得很好,我认为这不是我问题的根源 // PHP explode function vector<string>
// PHP explode function
vector<string> explode (string text, char separator)
{
vector<string> split;
int last_trip = 0, pos = 0;
text += separator;
for (pos = 0; pos < text.size(); pos++)
{
if (text[pos] != separator)
{
// continue with iteration
}
else
{
split.push_back(text.substr(last_trip, pos - last_trip));
last_trip = pos + 1;
}
}
return split;
};
// LINUX -- directory listing function
string LS (string dir)
{
DIR *dp;
vector<string> files;
struct dirent *dirp;
if ((dp = opendir(dir.c_str())) = NULL)
{
cout << "Error (" << errno << ") opening " << dir << endl;
//return errno;
}
while ((dirp = readdir(dp)) != NULL)
{
files.push_back(string(dirp->d_name));
}
closedir(dp);
string explosive = "";
for (int i = 0; i < files.size(); i++)
{
explosive += files[i];
if (i != (files.size() - 1)) { explosive += ','; }
}
return 0;
}
// various functions for encryption
int main (int argc, char* argv[])
{
cout << "\t! ENCRYPTR -- File encryption utility written by WBlinder, 2010. !" << endl << endl;
cout << "\t\t\t\tOPTIONS:" << endl;
cout << "\t\t\t1\tCRYPT A FILE" << endl << "\t\t\t2\tDECRYPT A FILE" << endl << endl;
cout << "choice > ";
int opt;
cin >> opt;
string sin, sout;
string dummy; getline(cin, dummy);
/*cout << "input files > ";
cout.clear(); cin.clear();
getline(cin, sin);
vector<string> fin = explode(sin, ',');
cout << "output files > ";
getline(cin, sout);
vector <string> fout = explode(sout, ',');
if (sin.size() != sout.size())
{
cout << "NO. INPUT FILES UNEQUAL NO. OUTPUT FILES" << endl;
return 1;
}*/
string dir;
cout << "dir > ";
getline (cin, dir);
vector<string> input = explode(dir, ',');
vector<string> output = input;
switch (opt)
{
case 1:
for (int i = 0; i < output.size(); i++)
{
output[i] = output[i] + ".wbf";
}
break;
case 2:
for (int i = 0; i < output.size(); i++)
{
output[i] = output[i].substr(0, (output[i].size() - 4));
}
break;
}
cout << "password > ";
getline(cin, key);
cout << "N > ";
cin >> N;
cout << "(768 => fairly secure\t3072 => secure)\nextra rounds > ";
cin >> drop;
for (int brick = 0; brick < input.size(); brick++)
{
do_crypt(opt, dir + input[brick], dir + output[brick]);
}
/*string text;
cout << "text to split: ";
getline (cin, text);
vector<string> tnt = explode(text, '.');
for (int i = 0; i < tnt.size(); i++)
{
cout << i << ": " << tnt[i] << endl;
}*/
}
//PHP分解函数
向量分解(字符串文本、字符分隔符)
{
向量分裂;
int last_trip=0,pos=0;
文本+=分隔符;
用于(pos=0;pos
我想你的意思是如果((dp=opendir(dir.c_str())=NULL)
我想你的意思是
==NULL
你的LS
函数有很多问题。首先,你应该让它直接返回一个向量,而不是用逗号分隔值,将数据打包在字符串中。这将为你节省对explode
函数的调用,并且如果控制器y包含一个带有逗号的文件名(在Linux上是有效名称)
但是导致segfault的更大问题是返回0
行。由于函数被声明为返回字符串
对象,并且字符串
类具有来自常量字符*
的隐式构造函数,因此编译器将其解释为返回字符串(NULL)当使用“代码>空NU/COD>指针调用此构造函数时,会引发<代码> LogICyError < /Calp>异常。由于没有捕获异常,C++运行时调用<代码>异常终止/代码>函数。该函数通过设计导致分段错误,以便停止执行(如果启用的话,可以生成CORDEBOMP以允许后期调试)。
您至少应该像这样重写LS
函数:
string LS (string dir)
{
DIR *dp;
vector<string> files;
struct dirent *dirp;
if ((dp = opendir(dir.c_str())) == NULL)
{
cout << "Error (" << errno << ") opening " << dir << endl;
return string();
}
while ((dirp = readdir(dp)) != NULL)
{
files.push_back(string(dirp->d_name));
}
closedir(dp);
string explosive = "";
for (int i = 0; i < files.size(); i++)
{
explosive += files[i];
if (i != (files.size() - 1)) { explosive += ','; }
}
return explosive;
}
vector<string> LS (string dir)
{
DIR *dp;
vector<string> files;
struct dirent *dirp;
if ((dp = opendir(dir.c_str())) != NULL)
{
while ((dirp = readdir(dp)) != NULL)
{
files.push_back(string(dirp->d_name));
}
closedir(dp);
}
else
{
cout << "Error (" << errno << ") opening " << dir << endl;
}
return files;
}
字符串LS(字符串目录)
{
DIR*dp;
矢量文件;
结构方向*dirp;
if((dp=opendir(dir.c_str())==NULL)
{
cout你的LS
函数有很多问题。首先,你应该让它直接返回一个vector
,而不是用逗号来分隔值,而将数据打包成字符串。这将节省你对explode
函数的调用,并且如果目录中包含一个文件名,在i中用逗号分隔,则不会中断t(在Linux上是一个有效的名称)
但是导致segfault的更大问题是返回0
行。由于函数被声明为返回字符串
对象,并且字符串
类具有来自常量字符*
的隐式构造函数,因此编译器将其解释为返回字符串(NULL)当使用“代码>空NU/COD>指针调用此构造函数时,会引发<代码> LogICyError < /Calp>异常。由于没有捕获异常,C++运行时调用<代码>异常终止/代码>函数。该函数通过设计导致分段错误,以便停止执行(如果启用的话,可以生成CORDEBOMP以允许后期调试)。
您至少应该像这样重写LS
函数:
string LS (string dir)
{
DIR *dp;
vector<string> files;
struct dirent *dirp;
if ((dp = opendir(dir.c_str())) == NULL)
{
cout << "Error (" << errno << ") opening " << dir << endl;
return string();
}
while ((dirp = readdir(dp)) != NULL)
{
files.push_back(string(dirp->d_name));
}
closedir(dp);
string explosive = "";
for (int i = 0; i < files.size(); i++)
{
explosive += files[i];
if (i != (files.size() - 1)) { explosive += ','; }
}
return explosive;
}
vector<string> LS (string dir)
{
DIR *dp;
vector<string> files;
struct dirent *dirp;
if ((dp = opendir(dir.c_str())) != NULL)
{
while ((dirp = readdir(dp)) != NULL)
{
files.push_back(string(dirp->d_name));
}
closedir(dp);
}
else
{
cout << "Error (" << errno << ") opening " << dir << endl;
}
return files;
}
字符串LS(字符串目录)
{
DIR*dp;
矢量文件;
结构方向*dirp;
if((dp=opendir(dir.c_str())==NULL)
{
我还可以做另外一个修改do_crypt(opt,dir+“/”+input[brick],dir+“/”+output[brick]),但仍然是segfult。我还做了另一个修改do_crypt(opt,dir+“/”+input[brick],dir+“/”+output[brick])你是否已经尝试过调试来查看它的分段错误?我会怎么做?我只有调试C语言的经验,而不是C++,我在Ubuntu .gDB上,或者?为了找到Sebug的点,你可以在GDB中启动你的程序(<代码> GDB。/MyApp < /代码>)。在gdb提示符中,您可以使用run
命令启动程序。如果需要传递参数,请在run
之后列出参数。当您编写segfault时,gdb将停止,您可以使用bt
获取调用堆栈,使用list
获取当前执行的代码行。以下是gdb教程:示例代码e不调用LS
函数,即使您怀疑此函数中存在崩溃。它还包含大量已注释的代码。发布时,您应该尝试发布说明问题的最小示例。在这里,您可能会删除所有代码,除了LS
函数,并且main
可能会被删除简化为int main(int argc,char*argv[]){string files=LS(“.”);返回0;}
。这足以在我的计算机上重现崩溃。或者,如果您喜欢GUI,可以使用DDD(或gdb的另一个GUI前端)您是否尝试过调试来查看它的分段错误?我会怎样?我只有调试C++的经验,而不是C++,我在Ubuntu .gDB上,或者?为了找到Sebug的点,可以在GDB中启动程序(<代码> GDB。/MyAPP < /代码>)。在gdb提示符中,使用run
命令启动程序。如果需要传递参数,请列出它们