0

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.

2
  • "/home/python/encrypt/"argv[1]".encrypted" this is how you manipulate it? what you want here? Commented Jul 19, 2014 at 10:20
  • The code works fine and I get the desired output(encrypted file) if I just name it as "outfile.encrypted" but I want each output file to have a different name, I.e, the original name appended by '.encrypted'. Like "trial.doc" to become "trial.doc.encrypted". Commented Jul 19, 2014 at 10:26

2 Answers 2

2

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]);
Sign up to request clarification or add additional context in comments.

5 Comments

Note that second parameter of snprintf includes null character, so maximum possible string's length (i.e. strlen) is 99. Longer strings are simply cut-off.
So, I tried what you said and this is what I got - In function ‘main' warning: passing argument 1 of ‘snprintf’ discards ‘const’ qualifier from pointer target type [enabled by default] /usr/include/stdio.h:387:12: note: expected ‘char * restrict’ but argument is of type ‘const unsigned char *’
Just remove the const modifer from key's definition. @AnkiJu
@alk ok I did that. Program gets compiled! :) but now, I'm facing something else - Segmentation fault (core dumped)
On how to debug a (small) program, you might like to read here: ericlippert.com/2014/03/05/how-to-debug-small-programs @AnkiJu
1

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]);

1 Comment

This has potential UB (unless input is "trusted"), when strlen(argv[1]) >= CCHMAXPATH - strlen("/home/python/encrypt/" ".encrypted") (sprintf automatically appends null character).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.