如何在C代码中解析带有空格的文件夹路径 [英] How to parse a folder path with spaces in C code

查看:206
本文介绍了如何在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.

这篇关于如何在C代码中解析带有空格的文件夹路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆