Anki Ju
Anki Ju

Reputation: 1

How to pass commandline argument as part of a string parameter

This is part of the code I'm facing issue with :

void encrypt(const char *fileIn, const char *fileOut, const unsigned char *key);

int main(int argc, char *argv[]) 
{
     const unsigned char key[100];
     srand(time(NULL));

     aes_init();
     encrypt(argv[1], "/home/python/encrypt/"argv[1]".encrypted", argv[3]);

     return 0;
 }

As you can see, in the encrypt function, I'm asking the user to enter the file name via command line for input. For output of the same function, I wanted the same name to be just appended by '.encrypted'. However, I get the following error whenever I try to compile the code.

In function ‘main’:
error: expected ‘)’ before ‘argv’
error: too few arguments to function ‘encrypt’
note: declared here

What am I doing wrong? Please help.

Upvotes: 0

Views: 207

Answers (2)

Peter Miehle
Peter Miehle

Reputation: 6070

in C, string manipulation is not as smooth as in modern languages. You have to append strings by using library functions.

char buffer[CCHMAXPATH];
sprintf(buffer, "/home/%s.encrypted", argv[1]);
encrypt(argv[1], buffer, argv[3]);

Upvotes: 1

Jayesh Bhoi
Jayesh Bhoi

Reputation: 25865

I think you want something easy string manipulation like this

snprintf(key,100,"/home/python/encrypt/%s.encrypted",argv[1]);
encrypt(argv[1],key, argv[3]);

Upvotes: 2

Related Questions