当前位置: 技术问答>linux和unix
关于unix高级编程的一个例子
来源: 互联网 发布时间:2017-03-06
本文导语: #include "apue.h" #include int main(int argc, char *argv[]) { DIR *dp; struct dirent *dirp; if (argc != 2)//请问此处的if(argc!=2)是什么意思? err_quit("usage: ls directory_name"); if ((dp = opendir(argv[1])) == NULL) err_sys("can't open ...
#include "apue.h"
#include
int
main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;
if (argc != 2)//请问此处的if(argc!=2)是什么意思?
err_quit("usage: ls directory_name");
if ((dp = opendir(argv[1])) == NULL)
err_sys("can't open %s", argv[1]);
while ((dirp = readdir(dp)) != NULL)
printf("%sn", dirp->d_name);
closedir(dp);
exit(0);
}
#include
int
main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;
if (argc != 2)//请问此处的if(argc!=2)是什么意思?
err_quit("usage: ls directory_name");
if ((dp = opendir(argv[1])) == NULL)
err_sys("can't open %s", argv[1]);
while ((dirp = readdir(dp)) != NULL)
printf("%sn", dirp->d_name);
closedir(dp);
exit(0);
}
|
int main(int argc, char **argv)
argc ---argument count 参数个数
argv----argument vector 参数内容组成的指针数组
举例: ./main you can try
argc = 4
argv[1] = "./main"
argv[2] = "you"
........
argc ---argument count 参数个数
argv----argument vector 参数内容组成的指针数组
举例: ./main you can try
argc = 4
argv[1] = "./main"
argv[2] = "you"
........