[分享] PHP exec C 相互傳遞與接收
PHP (index.php):
<?php
//傳遞變數給exe執行檔
$text = exec("chello\\chello.exe Hello PHPBoxy", $arr);
//印出陣列大小
echo count($arr)."<br /><br />";
//印出陣列結果
foreach($arr as $value)
echo $value."<br />";
//僅印出最後一行
echo "<br />".$text;
?>
C (chello\chello.exe):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char** argv) {
char str1[1024], str2[1024], str3[1024];
if(argc > 1) {
strncpy(str1,argv[0],sizeof(str1)-1);
strncpy(str2,argv[1],sizeof(str2)-1);
strncpy(str3,argv[2],sizeof(str3)-1);
printf("%s\n%s\n%s", str1, str2, str3);
} else {
printf("%d\n", argc);
}
return (EXIT_SUCCESS);
}
輸出結果:
3
cHello\chello.exe
Hello
PHPBoxy
PHPBoxy
重點在於,
當argv需要做字串的處理時必須要重新複製一份來進行修改,
若直接使用argv來做修改可能會導致資料錯誤的發生,
這是今天改了整天的程式所得來的結果做個整理弄個連結小範例,
非常感謝『legnaleurc』、『firejox』、『hellophoenix』、
『xatier』、『littleshan』、『xatier』、『MOONRAKER』等人的幫忙。
貼文才發現tab字元不會空格,
最後分享一下我自己用js寫的簡單tab轉空格的程式。
http://toolboxy.blogspot.com/2011/06/rp.html
--
酷酷方盒子 - QQBoxy
http://QQBoxy.blogspot.com
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.130.17.100
※ 編輯: gs1458 來自: 140.130.17.100 (07/19 04:02)
→
07/19 09:33, , 1F
07/19 09:33, 1F
→
07/19 14:00, , 2F
07/19 14:00, 2F
→
07/19 21:46, , 3F
07/19 21:46, 3F
→
07/19 22:15, , 4F
07/19 22:15, 4F
→
07/19 22:52, , 5F
07/19 22:52, 5F
→
07/19 22:53, , 6F
07/19 22:53, 6F
推
07/20 02:38, , 7F
07/20 02:38, 7F
→
07/20 02:38, , 8F
07/20 02:38, 8F
→
07/20 02:39, , 9F
07/20 02:39, 9F
推
07/20 03:46, , 10F
07/20 03:46, 10F
→
07/20 04:39, , 11F
07/20 04:39, 11F
→
07/20 11:05, , 12F
07/20 11:05, 12F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章