一、在本地服务器目录下新建一个 test 文件夹,在此文件夹中新建文件:
1. cpp.html
<!DOCTYPE html> <html> <head> <title>C++测试</title> </head> <body> <form method="post" action="cpp.php"> <div> <p>请测试文字,以空格分隔:</p> <input type="text" name="arr" size="20" /> <input type="submit" value="确定" /> </div> </form> </body> </html>
2. cpp.php
<?php $arr = $_POST[‘arr‘]; if(empty($arr)){ echo "You didn‘t enter any arguments."; } else{ $command = " test " . escapeshellcmd($arr); $return = passthru($command); } ?>
二、新建 test.cpp 文件,编译运行后,将生成的 test.exe 文件复制到上一步创建的test文件夹下;
#include <iostream> #include <stdio.h> using namespace std; int main(int argc, char* argv[]) { cout<<endl<<"You passed "<<argc-1<<" arguement"<<(argc-1==1?"":"s")<<"."<< endl; cout<<(argc-1==1?"This":"These")<<"arguement"<<(argc-1==1?"":"s")<<" "<<(argc-1==1?"is":"are")<<":"<< endl << endl; for (int i = 1; i<argc;i++) cout<<argv[i]<<endl; return 0; }
三、在浏览器中打开 cpp.html
HTML小实例:通过PHP调用C++,布布扣,bubuko.com
原文地址:http://apezone.blog.51cto.com/8926012/1412077