Pregunta

I'm writing an ASE/RSA encryption program, currently working on the interface. What I'm currently trying to do is ofstream out the results of the encryption/decryption to a local folder i.e. "Encrypted/FILE.txt". When the user chooses to enter the data they want encrypted through stdin, I want to use a user-entered command line argument as the file name. This necessitates that I combine the folder path with the file name:

ofstream out("Encrypted/FILE.txt");

I'm currently achieving this using the following code:

char ddest[] = "Decrypted/";
char edest[] = "Encrypted/";
char * decf;
char * encf;
decf = ddest;
encf = edest;
...
if(enc) //If the user wants to encrypt
    out.open(strcat(encf, argv[argc-2])); //resulting in "Encrypted/FILE.txt"
else
    out.open(strcat(decf, argv[argc-2]));

I also use this same strcat method of combining the path with the file name when the user wishes to use command line argument inputs as file names instead of stdin. When I run my program, it works for all cases EXCEPT attempting to ENCODE from STDIN. After a bit of debugging, I have narrowed the issue down to the ofstream open function. And It's not a result of strcat, as the following code

cout << "a" << endl;
char * path = strcat(encf, argv[argc-2]);
cout << "b" << endl;
out.open("Decrypted/test.txt");
cout << "c" << endl;

results in the following output

escher@ubuntu:~/AESencrypt$ ./ucrypt -e -s secretstdin.txt key
a
b
Segmentation fault (core dumped)

The kicker is that the program successfully created the file test.txt in the Decrypted folder, both when the path is implicit and when the strcat function is used in open().

So, can anyone offer a suggestion on this issue? My guess is that my method of combining the path and file names is crude and is causing memory issues.

¿Fue útil?

Solución

Don't use strcat in C++ programs. It's a lot of work, and error-prone, since you have to do the memory management yourself.

If you just want to concatenate strings, use this:

std::string filename;
filename += "directory/";
filename += basename;

std::ofstream out(filename.c_str());

And if things get more complicated, use a std::ostringstream, to which you can concatenate also integers and other objects.

In your special case, you created a C string "Encrypted/". This C string does not have room to append something. When you try anyway, you will overwrite arbitrary memory, leading to undefined behaviour. The assignment encf = edest doesn't create a new string, it just makes encf point to the same string as edest.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top