unix环境高级编程学习1-unix文件和目录

来源:互联网 发布:团团子 琵琶 知乎 编辑:程序博客网 时间:2024/05/17 07:37

root的名字是字符/。

创建新目录时会自动创建两个文件名:.(目录)和..(父目录)

下面开始写第一个unix程序。

说明一下,表示不想折腾电脑了,之前装了cygwin64可以模拟unix环境,安装了gcc,g++,代码在dev-c++下写:

#include <sys/types.h> #include <dirent.h>#include "stdlib.h"#include "stdio.h"int main(int argc, char* argv[]){DIR* dp;dirent* dirp;if(argc != 2){printf("usage: ls dir_name");return 0;}if((dp = opendir(argv[1])) == NULL){printf("can't open %s", argv[1]);}while( (dirp = readdir(dp)) != NULL){printf("%s\n",dirp->d_name);}closedir(dp);exit(0);}

运行结果:

Administrator@hailong-PC /cygdrive/e/unix-c
$ ./test.exe /dev
.
..
fd
mqueue
shm
stderr
stdin
stdout
clipboard
conin
conout
console
dsp
full
kmsg
null
ptmx
pty0
random
scd0
sda
sda1
sda2
sda3
sda4
sda5
sda6
sda7
sr0
tty
urandom
windows
zero

还不错,准备把这本书的代码都码完,那么哥不是有linux编程经验~\(≧▽≦)/~啦啦啦

原创粉丝点击