码迷,mamicode.com
首页 > 其他好文 > 详细

PosixIO

时间:2016-08-09 18:55:33      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

1.打开文件

int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);

int creat(const char *pathname, mode_t mode);

pathname:要打开的文件名
flags:打开标志,告诉系统,是以什么样的方式打开件
必须包含以下三个标志之一
O_RDONLY 只读
O_WRONLY 只写
O_RDWR 可读可写
还可以按位或 (|) 以下的标志:
O_APPEND 追加
O_CREAT 文件不存在则创建
O_EXCL 与O_CREAT搭配使用,存在则报错,可用来判断文件是否存在
O_TRUNC 文件存在且允许写,则清空文件
....

mode:操作模式,权限
当创建文件时,需要指定的文件权限
mode可用一组宏定义:
S_I(R/W/X)(USR/GRP/OTH)

S_IRUSR S_IWUSR S_IXUSR
S_IRGRP S_IWGRP S_IXGRP
S_IROTH S_IWOTH S_IXOTH
R/W/X:分别表示读、写、执行权限
USR/GRP/OTH:分别表示用户、组、其它
例如:S_IRUSR | S_IWUSR
表示设置文件所有者可读可写属性

八进制表示法:600也表示用户可读可写

返回值:成功返回新的文件描述符
失败返回-1


2.关闭文件
int close(int fd);
功能:用于关闭fd指定的文件描述符,也就是让文件描述符fd不再关联任何一个文件,以便下次使用
返回值:
成功返回0
失败返回-1

3.读写文件
ssize_t read(int fd, void *buf, size_t count);
功能:从指定的文件中读取指定大小的数据
fd:文件描述符,从哪里读
buf:缓冲区的首地址,数据放到哪里去
count:读取的数据的大小
返回值:
成功返回读取到的字节数,返回0表示读到文件尾
失败返回 -1
ssize_t write(int fd, const void *buf, size_t count);
功能:将指定的数据写入到指定的文件中
fd:文件描述符,写到哪里去
buf:缓冲区的首地址,数据从哪里来
count:写入的数据大小

返回值:成功返回写入数据大小,返回0表示没有写入
失败返回-1
4.定位
off_t lseek(int fd, off_t offset, int whence);
功能:用于调整文件的读写位置
fd:文件描述符,表示在哪个文件中操作
whence:
SEEK_SET 文件开头位置

SEEK_CUR 文件当前位置

SEEK_END 文件末尾位置
offset:偏移量

返回值:成功返回距离文件开头位置的偏移量
失败返回-1

./copy oldfile newfile

argc:3
argv:("./copy","oldfile","newfile")
argv[1] ->oldfile
argv[2] ->newfile

 

测试标准IO和系统IO的写效率:
往文件中写0-100万个整数

----------------------
1.文件属性
int stat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *path, struct stat *buf);
stat 与 fstat基本相同,只有指定文件的方式不一样
lstat当path是符号链接时,返回这个链接本身的状态,而不是其指向的文件的状态。

功能:用来把path指定的文件(包括目录和设备)的状态及属性保存到buf所指向的结构体中去。

struct stat {
dev_t st_dev; /* 容纳该文件的设备ID */
ino_t st_ino; /* inode number */
mode_t st_mode; /* 文件权限及类型 */
nlink_t st_nlink; /* 硬链接数 */
uid_t st_uid; /* 用户ID */
gid_t st_gid; /* 组ID */
dev_t st_rdev; /* 设备ID(如果是设备) */
off_t st_size; /* 文件大小,单位是字节 */
blksize_t st_blksize; /* 针对文件系统进行IO操作时的最优 块大小,一般是4096*/
blkcnt_t st_blocks; /* 表示分配给文件的总块数,块大小为512字节 */
time_t st_atime; /* 最后访问时间 */
time_t st_mtime; /* 最后修改时间 */
time_t st_ctime; /* 最后状态改变时间 */

};

文件类型用宏来解析:
S_ISREG(m) 是不是普通文件

S_ISDIR(m) 是不是目录

S_ISCHR(m) 是不是字符设备

S_ISBLK(m) 是不是块设备

S_ISFIFO(m) 是不是管道文件

S_ISLNK(m) 是不是符号链接

S_ISSOCK(m) 是不是socket文件

文件权限用位与判断:
S_IFMT
S_IRUSR
S_IWUSR
S_IXUSR

S_IRGRP
S_IWGRP
S_IXGRP

S_IROTH
S_IWOTH
S_IXOTH

if((stbuf.st_mode & S_IRUSR)
{
//表示用户有读权限
}

返回值:成功返回 0
失败返回 -1

getpwuid()//通过uid获取用户名
getgrgid()//通过gid获取用户所在组名


2.access函数
int access(const char *pathname, int mode);
pathname:文件名
mode:
F_OK 判断文件是否存在
R_OK 判断文件是否可读
W_OK 判断文件是否可写
X_OK 判断文件是否可执行
功能:用于判断文件是否存在以及是否拥有指定的权限
返回值:成功返回0
失败返回-1

3.chmod和truncate
int chmod(const char *path, mode_t mode);
int fchmod(int fd, mode_t mode);
函数功能:用于修改文件权限
path:文件的路径
mode:文件的新权限,如664

int truncate(const char *path, off_t length);
int ftruncate(int fd, off_t length);
功能:用于将指定的文件截取到指定的长度
path:文件的路径
length:文件的最新大小
注:
如果文件变小,则文件的数据会丢失
如果文件变大,扩展的部分用\0填充
返回值:成功返回0
失败返回-1

4.umask函数
mode_t umask(mode_t mask);
功能:用于设置参数指定的权限屏蔽字
返回之前的权限屏蔽字
该函数针对文件的创建有效


6.目录
1.打开目录
DIR *opendir(const char *name);
功能:用于打开指定的目录
成功返回目录指针
失败返回NULL

2.读目录
struct dirent *readdir(DIR *dirp);
功能:用于读取指定目录的内容
返回下一个目录项,读完或出错返回NULL

struct dirent {
ino_t d_ino; /* inode number */
unsigned char d_type; /* type of file; not supported
by all filesystem types */
char d_name[256]; /* filename */
};


3.关闭目录
int closedir(DIR *dirp);



/home/csgec/aa.txt

#include <libgen.h>

char *dirname(char *path);

char *basename(char *path);


作业:
0.ls -l
1.求目录下所有普通文件的大小
2.求一目录下图片文件的数量(bmb,jpg)

 

 

/*************************************************************************
> File Name: access.c
> Author: csgec
> Mail: longer.zhou@gmail.com
> Created Time: Tue 02 Aug 2016 04:22:56 PM CST
************************************************************************/

#include<stdio.h>
#include<unistd.h>

int main()
{
if(access("access.c",F_OK) == 0)
{
printf("file is ok\n");
}
if(access("access.c",W_OK) == 0)
{
printf("W_OK\n");
}
}

 

 

/*************************************************************************
> File Name: open.c
> Author: csgec
> Mail: longer.zhou@gmail.com
> Created Time: Tue 02 Aug 2016 10:30:46 AM CST
************************************************************************/

#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc,char *argv[])
{
//close(1);
int fd = open("/home/csgec/a.txt",O_RDWR | O_CREAT | O_TRUNC,0644);
if(fd == -1)
{
perror("open");
return -1;
}
printf("fd = %d\n",fd);

int res = close(fd);
if(-1 == res)
{
perror("close");
}
printf("close success!\n");


}

 

 

/*************************************************************************
> File Name: readdir.c
> Author: csgec
> Mail: longer.zhou@gmail.com
> Created Time: Tue 02 Aug 2016 05:03:15 PM CST
************************************************************************/

#include<stdio.h>
#include<dirent.h>
int main()
{
//1.
DIR *dirp = opendir(".");
if(dirp == NULL)
{
perror("opendir");
return -1;
}

//2.

struct dirent * ent;
while( (ent=readdir(dirp)) != NULL )
printf("%s \n", ent->d_name);

//3.
closedir(dirp);
return 0;
}

 

 

/*************************************************************************
> File Name: stat.c
> Author: csgec
> Mail: longer.zhou@gmail.com
> Created Time: Tue 02 Aug 2016 03:16:03 PM CST
************************************************************************/

#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int main()
{
struct stat stbuf;
int res = stat("stat.c",&stbuf);
if(res == -1)
{
perror("stat");
}
int size = stbuf.st_size;
printf("size = %d\n",size);

int blocks = stbuf.st_blocks;
int blocksize = stbuf.st_blksize;

printf("blocks = %d\n",blocks);
printf("blksize = %d\n",blocksize);

return 0;
}

 

 

/*************************************************************************
> File Name: testf.c
> Author: csgec
> Mail: longer.zhou@gmail.com
> Created Time: Tue 02 Aug 2016 02:23:20 PM CST
************************************************************************/

#include<stdio.h>

int main()
{
FILE * fp = fopen("aa.txt","w");
if(fp == NULL)
{
perror("fopen");
return -1;
}
int i;
for(i = 0; i < 10000000; i++)
{
fwrite(&i,sizeof(int),1,fp);
}
fclose(fp);
return 0;

}

 

 

/*************************************************************************
> File Name: testp.c
> Author: csgec
> Mail: longer.zhou@gmail.com
> Created Time: Tue 02 Aug 2016 02:27:23 PM CST
************************************************************************/

#include<stdio.h>
#include<fcntl.h>

int main()
{
int fd = open("bb.txt",O_WRONLY | O_CREAT,0644);
if(fd == -1)
{
perror("open");
return -1;
}
int i;
for(i = 0; i < 10000000; i++)
{
write(fd,&i,sizeof(i));

}

close(fd);

return 0;
}

 

 

/*************************************************************************
> File Name: truncate.c
> Author: csgec
> Mail: longer.zhou@gmail.com
> Created Time: Tue 02 Aug 2016 04:32:17 PM CST
************************************************************************/

#include<stdio.h>

int main(int argc,char ** argv)
{
if(argc < 3)
{
printf("Usage:%s path len\n",argv[0]);
return -1;
}
int len = atoi(argv[2]);

if(truncate(argv[1],len) == -1)
{
perror("truncate");
}


return 0;
}

 

PosixIO

标签:

原文地址:http://www.cnblogs.com/liudehao/p/5754002.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!