博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
VC调用exe文件
阅读量:4299 次
发布时间:2019-05-27

本文共 2908 字,大约阅读时间需要 9 分钟。

第1步: 新建工程,选择Empty Project,Finish

这里写图片描述

第2步: 新建cpp文件,并输入代码

/* * 使用matlab生成的exe做加减运算(每次调用exe都会启动matlab引擎,比较花时间) */#include 
using namespace std;void main(){ FILE *fp; int a,b,aaddb,aminusb; char status[20]; cout<<"输入加数与被加数:"<
>a>>b; if ((fp=fopen("MatlabIn.txt","wt+")) == NULL){ cout<<"打开文件MatlabIn.txt失败!"<

第3步: 启动matlab2013,新建Function文件,输入代码,并生成exe(Windows Standalone Application)

% %% 对MatlabIn.txt中的参数进行处理并输出到MatlabOut.txt% %function addandminus()MatlabInfid = fopen('MatlabIn.txt','rt');if MatlabInfid<0    MatlabOutfid = fopen('MatlabOut.txt','wt+');    fprintf(MatlabOutfid,'%s\n','status:打开MatlabIn.txt失败');    fprintf(MatlabOutfid,'%s\n','a+b:');    fprintf(MatlabOutfid,'%s\n','a-b:');    fclose(MatlabOutfid);    return;enda  = fscanf(MatlabInfid,'a:%d\n');b  = fscanf(MatlabInfid,'b:%d\n');fclose(MatlabInfid);MatlabOutfid = fopen('MatlabOut.txt','wt+');fprintf(MatlabOutfid,'%s\n','status:成功');fprintf(MatlabOutfid,'a+b:%d\n',a+b);fprintf(MatlabOutfid,'a-b:%d\n',a-b);fclose(MatlabOutfid);end

第4步: 将生成的exe文件复制到上面新建的vc工程目录下,运行vc程序成功!

改进: 上面的方法虽然能运行,但每次调用exe都要启动matlab引擎,比较花时间,可以将第2步和第3步代码加以改进

/* * 使用matlab生成的exe做加减运算(exe一直处于运行状态,直到该程序发出退出命令) */#include 
#include
#include
using namespace std;void main(){ FILE *fp; int a,b,aaddb,aminusb; char status[20]; WinExec("addandminus.exe",SW_HIDE); // hu 启动matlab后直接执行下一步程序 for (int ii=0;ii<5;ii++){ cout<<"输入加数与被加数:"<
>a>>b; if ((fp=fopen("MatlabIn.txt","wt+")) == NULL){ cout<<"打开文件MatlabIn.txt失败!"<
% %% 每隔0.1s判断一次指令文件状态% MatlabRun.tmp   对MatlabIn.txt中的参数进行处理并输出到MatlabOut.txt% MatlabStop.tmp 退出程序% %function addandminus()while 1    pause(0.1);    if exist('MatlabRun.tmp','file')   % hu 若该文件存在,执行程序        MatlabInfid = fopen('MatlabIn.txt','rt');        if MatlabInfid<0            MatlabOutfid = fopen('MatlabOut.txt','wt+');            fprintf(MatlabOutfid,'%s\n','status:打开MatlabIn.txt失败');            fprintf(MatlabOutfid,'%s\n','a+b:');            fprintf(MatlabOutfid,'%s\n','a-b:');            fclose(MatlabOutfid);            delete('MatlabRun.tmp');            while exist('MatlabRun.tmp','file')                delete('MatlabRun.tmp');            end            continue;        end        a  = fscanf(MatlabInfid,'a:%d\n');    % hu 获得输入参数        b  = fscanf(MatlabInfid,'b:%d\n');        fclose(MatlabInfid);        MatlabOutfid = fopen('MatlabOut.txt','wt+');        fprintf(MatlabOutfid,'%s\n','status:成功');        fprintf(MatlabOutfid,'a+b:%d\n',a+b);   % hu 输出结果        fprintf(MatlabOutfid,'a-b:%d\n',a-b);        fclose(MatlabOutfid);        delete('MatlabRun.tmp');   % hu 删除该文件表示程序运行完成        while exist('MatlabRun.tmp','file')            delete('MatlabRun.tmp');        end    elseif exist('MatlabStop.tmp','file')  % hu 若该文件存在,退出程序        delete('MatlabStop.tmp');        break;    endendend
你可能感兴趣的文章
CountDownLatch源码解析加流程图详解--AQS类注释翻译
查看>>
ES相关度评分
查看>>
我们一起做一个可以商用的springboot脚手架
查看>>
idea在搭建ssm框架时mybatis整合问题 无法找到mapper
查看>>
java设计基本原则----单一职责原则
查看>>
HashMap的实现
查看>>
互斥锁 synchronized分析
查看>>
java等待-通知机制 synchronized和waity()的使用实践
查看>>
win10 Docke安装mysql8.0
查看>>
docker 启动已经停止的容器
查看>>
order by 排序原理及性能优化
查看>>
Lock重入锁
查看>>
docker安装 rabbitMq
查看>>
git 常用命令 入门
查看>>
linux安装docker
查看>>
关闭selinx nginx无法使用代理
查看>>
shell 脚本部署项目
查看>>
spring cloud zuul网关上传大文件
查看>>
springboot+mybatis日志显示SQL
查看>>
工作流中文乱码问题解决
查看>>