0

Hi, i am new to c/c++ and i need to execute external cabextract program to extract archive, but if i sprintf command and file name to command variable i get segmentation fault. How i need to modify code to avoid segmentation fault?

Here's an example:

void cabext(const char *fname)
{
	char *command;
	sprintf(command,"cabextract -L -d /tmp/ %s",fname);
	system(command);
}

int main() {
	const char *fname = "/home/me/archive.cab";
	cabext(fname);
}
2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Zolookas
0

line 3 does not allocate any memory for that array and sprintf() doesn't allocate it either.

void cabext(const char *fname)
{
	char command[1024] = {0};
	sprintf(command,"cabextract -L -d /tmp/ %s",fname);
	system(command);
}
Votes + Comments
Yes indeed.
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.