在之前的一些看書或者學(xué)習(xí)中,一直有一種感覺有問題的態(tài)度,那就是認(rèn)為看懂了,但是不動手,感覺這樣看書的效果不是很大。ls命令估計(jì)是我們在linux/unix里面用的最多的一個(gè)命令了,我們就用c來簡單的實(shí)現(xiàn)一下ls命令。
//// ls.c// apue//// Created by chenqing on 13-8-22.// Copyright (c) 2013年 chenqing. All rights reserved.// #include "/usr/include/apue.h"#include "dirent.h" int main(int argc,char *argv[]){ DIR *dp ; //創(chuàng)建一個(gè)DIR結(jié)構(gòu)的指針 //更多信息參考http://www.gnu.org/software/libc/manual/html_node/Opening-a-Directory.html struct dirent *dirp; if (argc != 2) { err_sys("需要兩個(gè)參數(shù)"); //err_sys 是在error.c中定義的一個(gè)函數(shù) } if ((dp = opendir(argv[1])) == NULL) { err_quit("讀取目錄出錯(cuò)了!"); } while ((dirp = readdir(dp)) != NULL) { printf("%s/n",dirp->d_name); } closedir(dp); exit(0); }
新聞熱點(diǎn)
疑難解答