2012年11月2日 星期五

跟黏土一樣 !好用的sprintf

I want to introduce how to combine or stick your Linux command and variable.
I use sprintf .....it is useful!!! awesome!!!

我要做的指令是:

cp /var/web/enter_folder.asp  /var/web/mount/sda1/"new dir"/."new dir".asp

/var/web/mount/sda1/"new dir"/."new dir.asp"   之所以會在檔名前後加上雙引號
quote ("") 是為了把檔名好好包起來XD
避免linux中空白字元會被系統認為指令已經中止了

#include <stdio.h>

char temp[512] = {0};

sprintf (temp , "cp %s/enter_folder.asp %s/%s/\"%s\"/.\"%s\".asp" , mount_point , mount_point , path_for_href , folder_name  ,folder_name);

system (temp);

就可以把我要的變數跟指令"黏"在一起 丟出去使用了!

裡面如果遇到 quote (") 要用跳脫字元(escape) Backslash (\) , 讓( " ) 忠實地
呈現在字串中,否則會被sprintf 忽略




沒有留言:

張貼留言