linux调用其他目录的程序,获取进程的当前目录,然后调用其他程序的方法(包含linux和window)...

1、方法A:用“./”

int main{

pid_t pid;

pid = fork();

if (pid < 0) {

exit(1);

} else if (pid == 0) {

execle(".\/pktctl.exe", "pktctl.exe", "-a","block in on eth0 proto tcp from 61.135.196.21 port = 80 to 192.168.1.146 port > 1023",

(char *)0, NULL);

}

if (waitpid(pid, NULL, 0) < 0)

exit(1);

return 0;

}

适用性: 1)在linux中运行正确;

2)在window中运行错误(因为是linux的系统调用)。

3)在cygwin下gcc编译,从cygwin的bash和window中双击均能运行正确;

4)没有测试在cygwin下gcc-3 -mno-cygwin编译!

2、方法B:用long getcwd(char   *buf,   unsigned   long   size);

#include

#include

int main()

{

char buffer[MAXPATH];

getcwd(buffer, MAXPATH);

printf( "The current directory is: %s ", buffer);

return 0;

}#include

应该依编程者的环境而定:

在TC2.9是#include

VC6.0定义在#include头文件中;

在qt4.5下是#include;

VS2008下是#include;

适用性:1)在window下运行正确,是绝对路径。

2)在linux下运行正确,是绝对路径。

3)在cygwin下gcc正常编译,在cygwin的bash下能用,双击生成的exe文件出现段内存错误!

4)在cygwin 下gcc-3 -mno-cygwin 编译,双击生成的exe和在cygwin的bash下运行,均能得到正确结果;

3、方法C:GetCurrentDirectory

string GetModuleFolderName()

{TCHAR szFolderName[MAX_PATH];

GetModuleFileName(_AtlBaseModule.m_hInstResource, szFolderName, sizeof(szFolderName));

(_tcsrchr(szFolderName,'\\'))[1] = 0;

return szFolderName;

}适用性:1)在window下使用,得到绝对路径;

2)没有测试从window移植到linux下!

4、方法D:readlinK() /proc/sefl/exe

int main()

{

char buf[256];

char ptr[256];

int npath;

sprintf(ptr,"/proc/%d/exe",getpid());

npath=readlink(ptr,buf,sizeof(buf));

if(npath==-1)

{

printf("readlink error:%s \n",ptr);

}

else

{

buf[npath]='\0';

printf("current :%s\n",buf);

}

return 0;

}

适用性:1)在linxu获得绝对路径;

2)在cygwin下gcc编译的程序,在cygwin的bash下是绝对路径,在window中双击只是一个上层目录的标示;例如:C:/about/abcd/a.exe 只显示/abcd/a.exe

3)在cygwin下gcc-3 -mno-cygwin编译,会报错,编译不通过!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值