C_and_CPP 板


LINE

开发平台(Platform): (Ex: Win10, Linux, ...) linux 编译器(Ex: GCC, clang, VC++...)+目标环境(跟开发平台不同的话需列出) gcc / g++ 额外使用到的函数库(Library Used): (Ex: OpenGL, ...) no 问题(Question): 一些原因,我必须取得某个执行档的 return code, 即 echo $? 之值 但就是不想用 echo $? 去 pipe。 我查过了 cplusepluse.com 对 system 之说明,没 意会错的话应是说若呼叫该 command 失败传回 -1; 呼叫成功的话会是该 exe / library 之传回值, 也就是 main 的传回值。於是我写了二份 code 做测试 /// code 1 : test.cpp int main(int argc, char **argv) { return argc; } /// code 2 : test2.cpp #include <cstdio> #include <cstdlib> int main(int argc, char ** argv) { printf("%d\n", system(".....ERRORCMD....")); printf("%d\n", system("./test")); printf("%d\n", system("./test 1")); printf("%d\n", system("./test 1 2")); return 0; } /// result 32512 256 512 768 这几个数值看起来是用 bitwise 代表 status, 但和我预期传回的 -1 1 2 3 有很大的落差 ,请问我是不是误会什麽了?问题主要二个 (1) 不用 pipe 如何取得另一个执行档之 return value ? (2) 请问 system 之 return value 意义为何 ??- 先谢谢各位的不吝解惑。 -- 就算把新鲜的肝拿回去,还是一样写码到秃头,加班到天亮, 永远当老板的傀儡 你是不是想这麽做? 是的话你就拿回去~ 拿啊!! 九世宅男 : 下辈子不要再让我干工程师了 ~ < Kuso 星爷语录 > --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 60.251.92.140
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1478689043.A.86D.html ※ 编辑: EdisonX (60.251.92.140), 11/09/2016 18:59:15
1F:→ Schottky: 不要用 system(),使用 vfork() + exec() 11/09 19:11
2F:→ Schottky: 然後使用 wait() 或 waitpid() 就可以取得return status 11/09 19:12
3F:→ Schottky: system 会去呼叫 shell 再叫 shell 执行你的指令 11/09 19:13
4F:推 stimim: The value returned is -1 on error (e.g., fork(2) 11/09 19:14
5F:→ stimim: and the return status of the command otherwise. 11/09 19:14
6F:→ stimim: This latter return status is in the format specified 11/09 19:15
7F:→ stimim: in wait(2). Thus the exit code of the command will be 11/09 19:15
8F:→ stimim: WEXITSTATUS(status). 11/09 19:15
9F:→ pttworld: argc不就是argv的数量,回传? 11/09 19:19
10F:→ pttworld: goo.gl/GT3OIo 11/09 19:25
11F:→ Schottky: 啊啊我看懂你的问题了,你要的值要用 WEXITSTATUS() 去取 11/09 19:25
12F:→ EdisonX: 疑!所以我没意会错推文意思的话,就变成 11/10 00:19
13F:→ EdisonX: WEXITSTATUS( system(pszCmd) ) ; 了吗 ?? 11/10 00:19
14F:→ Schottky: 话是不错,但你这个写程式的习惯很不好 11/10 01:00
15F:→ Schottky: 把 status 先存到变数里比较清楚,而且你有可能需要 11/10 01:01
16F:→ Schottky: 不只一个判断或取值 11/10 01:02
17F:→ EdisonX: 了解!谢谢各位的解答,感谢!! 11/10 01:50







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:WOW站内搜寻

TOP