I am using this code to extract a password protected RAR file. I am using the std::system() function to invoke the RAR command. If I use the password in the std::system() function, it works. But as tries to pass the password as parameter, it doesn’t. For example, if in this code if I use password pwd, it gives this error:
“pwd is not recognised as internal or external command, operable program or batch file.”
But if I change the code and make it to system("rar e -ppwd wingen.rar"), it works.
Can anybody explain what mistake I am making? Thanks in advance!
Here’s my code:
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char **argv)
{
char pword[20];
printf("enter the pword : ");
gets(pword);
system(("rar e -p%s wingen.rar",pword));
getchar();
return 0;
}
The
system()function doesn’t work likeprintf(). You need to create the full string, and then callsystem():The code you have right now is using the comma operator, which results in your ‘format string’ being completely ignored by your program. What you have there is 100% equivalent to writing:
Which is presumably not what you wanted.