I wanted to write a C code to zip a file in unix. I implemented it using system function using UNIX shell command “zip -r filepath”. The zip -r filepath command is working when I execute it directly through UNIX shell.
I have witten the code as below
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
int system(const char *zip -r /root/Desktop/hi.txt);
return 0;
}
But I am getting a compile time error saying
"error:expected ‘;’, ‘,’ or ‘)’ before string constant"
syntax system function : http://linux.die.net/man/3/system
int system(const char *command);
How can I fix this? I tried putting the UNIX command in quotations, even though it didn’t work.
When the compiler parses
int system(const char *zip -r /root/Desktop/hi.txt);it starts interpreting it as a declaration of something becauseintis a type andsystemcan very well be an identifier name and because of the parens and the trailing;it could be interpreted as a function prototype. But then the compiler chokes on-r /root/Desktop/hi.txtbecause it can’t be parsed as a valid part of the function parameter list.You don’t need a declaration, you need a function call, so you drop
intandconst char *, and the string parameter needs to be quoted: