您的位置:首页 > 编程语言

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

2013-11-08 21:52 447 查看
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编程经验~\(≧▽≦)/~啦啦啦
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: