免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1604 | 回复: 3
打印 上一主题 下一主题

请教一下system函数的用法 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-08-18 19:11 |只看该作者 |倒序浏览
我想在c语言中调用mount命令,可不可以采用下面的语句呢。
char *tmp;
sprintf(tmp,"/bin/mount -t vfat %s /mnt/usb",dev);
system(tmp);
其中dev是/dev/sda1。
这样可以实现吗?

论坛徽章:
0
2 [报告]
发表于 2006-08-18 19:40 |只看该作者
应该可以的,最好命令行后加"&",让其后台执行.
char *,应改成char str[256];否则存在溢出问题!

[ 本帖最后由 aiwin28 于 2006-8-18 19:41 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2006-08-18 22:57 |只看该作者
原帖由 liuchang1881 于 2006-8-18 19:11 发表
我想在c语言中调用mount命令,可不可以采用下面的语句呢。
char *tmp;
sprintf(tmp,"/bin/mount -t vfat %s /mnt/usb",dev);
system(tmp);
其中dev是/dev/sda1。
这样可以实现吗?

>>
>>
>>答案是可以。参考如下:
>>
>>
NAME
       system - execute a shell command

SYNOPSIS
       #include <stdlib.h>

       int system(const char *string);

DESCRIPTION
       system()  executes  a command specified in string by calling /bin/sh -c
       string, and returns after the command has been completed.  During  exe‐
       cution  of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT
       will be ignored.

RETURN VALUE
       The value returned is -1 on error (e.g. fork failed),  and  the  return
       status  of  the command otherwise.  This latter return status is in the
       format specified in wait(2).  Thus, the exit code of the  command  will
       be  WEXITSTATUS(status).   In  case  /bin/sh could not be executed, the
       exit status will be that of a command that does exit(127).

       If the value of string is NULL, system() returns nonzero if  the  shell
       is available, and zero if not.

       system() does not affect the wait status of any other children.

论坛徽章:
0
4 [报告]
发表于 2006-08-21 09:37 |只看该作者

请教一下exit函数!

在读程序的时候,经常会看到exit函数,例如exit(0),exit(-1),exit(-2)等,
它们各自代表什么意思呢,之间的区别是什么?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP