文件和目录
一、主要函数:
access();dup();dup2();opendir();readdir();closedir();mkdir();chdir();stat();lstat();sprintf();strstr();bzero();truncate();link();unlink();symlink(),readlink();rename();rename();
二、
1
、
access
用于测试对文件的访问权限,比如说测试某个文件是否可读:access(“file.txt”,R_OK),
若成功则返回0
,若失败则返回-1.
还可以测试是否存在某文件。
2
、
dup,dup2
dup(fd);//dup()
的返回值和open
差不多类似,从上往下找第一个未使用的
dup2(fd,1);
如果1
不存在,则先关闭
1
newfd=dup(oldfd);newfd
和oldfd
共享同一文件的指针和状态
3
、
opendir();readdir();closedir();mkdir();chdir();
/*2010-09-18*/
/*
目录操作
*/
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <dirent.h>
int main(int argc,char** argv)
{
if(argc!=2)
{
fprintf(stderr,"wrong usage...");
exit(1);
}
int ret;
DIR* dirp=opendir(argv[1]);//
打开一个目录
if(dirp==NULL)
{
perror("opendir");
exit(1);
}
struct dirent* dirptr;
char* savepath=argv[1];
while(1)
{
dirptr=readdir(dirp);//
读目录
if(dirptr==NULL)
break;
printf("%s
",dirptr->d_name);
}
closedir(dirp);//
关闭目录
exit(0);
}
4
、stat,lstat
其功能是返回一个文件的信息
其中lstat
能判断软链接
int ret;
struct stat buf;
ret=stat(argv[1],&buf);//
返回的文件信息存于buf
中
if(ret==-1)
{
perror("stat");
exit(1);
}
printf("link=%d/n",buf.st_nlink);
5、sprintf();strstr();bzero();truncate();
A、 The functions in the printf() family produce output according to a format as described
below. The functions printf() and vprintf() write output to stdout, the standard out‐
put stream; fprintf() and vfprintf() write output to the given output stream;
sprintf(), snprintf(), vsprintf() and vsnprintf() write to the character string str.
B、 char *strstr(const char *haystack, const char *needle);
功能:从字符串haystack中寻找needle第一次出现的位置(不比较结束符NULL)。 说明:返回指向第一次出现needle位置的指针,如果没找到则返回NULL。
C、 bzero(buf,100);
D、 truncate(“file.txt”,3);截断,只剩3个字节
6、link();创建一个硬链接
unlink();删除一个硬链接
symlink();符号链接
readlink();读软链接
remove();rename()
三、例程
1
、/*access
用于测试对文件的访问权限,或测试文件是否存在*/
/*access.c*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
if (argc != 2)
exit(1);
int ret = access(argv[1], F_OK);
if (ret == -1) {
printf("No such file!/n");
exit(0);
} else
printf("File exist!/n");
}
2 、
/*dup.c*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
if (argc != 3)
exit(1);
int oldfd = open("/etc/passwd", O_RDONLY);
if (oldfd == -1) {
perror("open");
exit(1);
}
char buf[100];
int size = 100;
int ret;
int fd = creat(argv[2], 0666);
if (fd == -1) {
perror("creat");
exit(1);
}
//close(1);
//dup(fd);
dup2(fd, 1);
close(fd);
while (1) {
ret = read(oldfd, buf, size);
if (ret == 0)
break;
write(1, buf, ret);// 此时等价于write(fd, buf, ret); 因为此时1 和fd 指向同一个文件
}
return 0;
}
3 、
/*dup2.c*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main(void)
{
int oldfd = open("/etc/passwd", O_RDONLY);
if (oldfd == -1) {
perror("open");
exit(1);
}
int newfd = dup(oldfd);
int odd = 0;
char buf[100];
int size = 100;
int ret;
while (1) {
if (odd == 0) {
ret = read(oldfd, buf, size);
if (ret == 0)
break;
write(1, buf, ret);
odd = 1;
} else {
ret = read(newfd, buf, size);
if (ret == 0)
break;
write(1, buf, ret);
odd = 0;
}
}
return 0;
}
/*newfd=dup(oldfd);newfd 和oldfd 共享同一文件的指针和状态*/
4 、
/*chmod.c*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int ret = chmod(argv[1], 0644);
if (ret == -1) {
perror("chmod");
exit(1);
}
exit(0);
}
5 、
/*link.c*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int ret = link(argv[1], argv[2]);
if (ret == -1) {
perror("link");
exit(1);
}
exit(0);
}
6 、
/*mkdir.c*/
#include <sys/stat.h>
int main(int argc, char **argv)
{
int ret = mkdir(argv[1], 0777);
if (ret == -1) {
perror("mkdir");
return -1;
}
return 0;
}
7 、
/*readdir.c*/
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void mystat(const char *path)
{
struct stat buf;
stat(path, &buf); //读path这个文件的相关信息到buf里
printf("size=%ld/n", buf.st_size);
}
int main(int argc, char **argv)
{
DIR *dirp = opendir(argv[1]);//打开一个目录
if (dirp == NULL) {
perror("opendir");
exit(1);
}
struct dirent *dirptr;///////!!!!!
char buf[256];
char *father = argv[1];//指向要打开的目录名
while (1) {
dirptr = readdir(dirp);//读目录,返回一个交替指针
if (dirptr == NULL)
break;
if (father[strlen(father) - 1] == '/')
sprintf(buf, "%s%s",
argv[1], dirptr->d_name);
else
sprintf(buf, "%s/%s",
argv[1], dirptr->d_name);
// printf("%s/n", buf);
mystat(buf);
//printf("%s ", dirptr->d_name);
}
closedir(dirp);
exit(0);
}
/*如果输入的命令行有“/”,那么直接打印,如果没有则加上"/"打印*/
8 、
/*readlink.c*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(int argc,char** argv)
{
int ret;
char buf[50];
bzero(buf,50);//
将其初始化,
后面打印到终端的时候就不会出现乱码
ret=readlink(argv[1],buf,50);
if(ret==-1)
{
perror("readlink");
exit(1);
}
printf("%s->%s",argv[1],buf);
exit(0);
}
/* 将读到的软链接的名字存于buf 中*/
9 、
/*rename.c*/
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int ret = creat(argv[1], 0777);
if (ret == -1) {
perror("creat");
exit(1);
}
ret = rename(argv[1], argv[2]);
if (ret == -1) {
perror("rename");
return -1;
}
return 0;
}
10 、
/*stat.c*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char **argv)
{
if (argc != 2) {
fprintf(stderr, "Usags.../n");
exit(1);
}
struct stat buf;
int ret;
ret = stat(argv[1], &buf);
if (ret == -1) {
perror("stat");
exit(1);
}
printf("size=%u/n", buf.st_size);
printf("links=%d/n", buf.st_nlink);
if (S_ISREG(buf.st_mode))
printf("%s is a regular file/n", argv[1]);
else if (S_ISDIR(buf.st_mode))
printf("%s is a directory file/n", argv[1]);
else
printf("%s unrecongnized!/n", argv[1]);
char Buf[10];
bzero(Buf, 10);
if (buf.st_mode & S_IRUSR)
sprintf(Buf, "r");
else
sprintf(Buf,"-");
if (buf.st_mode & S_IWUSR)
sprintf(Buf+1, "w");
else
sprintf(Buf+1, "-");
if (buf.st_mode & S_IXUSR)
sprintf(Buf+2, "x");
else
sprintf(Buf+2, "-");
printf("mode:%s/n", Buf);
return 0;
}
11 、
/*symlink.c*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int ret = symlink(argv[1], argv[2]);
if (ret == -1) {
perror("symlink");
exit(1);
}
exit(0);
}
12 、
/*truncate.c*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char **argv)
{
int ret = truncate(argv[1], atoi(argv[2]));
if (ret == -1) {
perror("truncate");
exit(1);
}
exit(0);
}