如何在C代码中解析带有空格的文件夹路径
问题描述:
我正在使用以下简单的C代码:
I'm using this simple C code:
char * command = NULL;
sprintf (command, "ls %s", folderpath);
system(command);
问题是文件夹名称中有空格时... 我知道在Unix中我需要添加一个"\",例如ls my \ folder \ name
The problem is when the folder name has a space in it... I know that in Unix I need to add a "\", for example ls my\ folder\ name
我该如何解决?谢谢!
答
简单的解决方法是将文件夹名称放在单引号内-sprintf( command, "ls '%s'", folder );
.当@ndim提醒我们时,请注意命令注入.
Simple way out is to put the folder name inside single quotes - sprintf( command, "ls '%s'", folder );
. Watch out for command injection as @ndim reminds us.