系统调用方式访问文件
2015-10-02 15:23
357 查看
linux 系统中访问文件的方法:1.linux系统调用 2.基于C语言的访问
系统调用-创建
int creat(const char *filename, mode_t mode)
-filename:要创建的文件名(包含路径,缺省为当前路径)
-mode:创建模式
常见创建模式: S_IRUSR 可读
S_IWUSR 可写
S_IXUSR 可执行
S_IRWXU 可读可写可执行
除了可以使用上述宏以外,还可以直接使用数字来表示文件的访问权限:
可执行->1
可写->2
可读->4
上述值的和,如可读可写->6
无任何权限->0
文件描述
在linux系统中,所有打开的文件都对应一个文件描述符。文件描述符的本质是一个非负整数。当打开一个文件时,该整数由系统来分配。文件描述符的范围是0-OPEN_MAX。早起的unix版本OPEN_MANX=19,即允许每个进程同时打开20个文件,现在很多系统则将其增加至1024
系统调用-打开
int open(const char *pathname, int flags)
int open(const char * pthname, int flags, mode_t mode)
-pathname:要打开的文件名(包含路径,缺省为当前路径)
-flags:打开标志(具体查baidu),如O_CREAT,如果使用了O_CREAT标志,这时需要使用int open(const char * pthname, int flags, mode_t mode)函数,其意义是如果路径下没有该文件,则自己创建文件,(否则直接打开它)。这时需要指定mode来表示文件的访问权限
系统调用-关闭
当我们操作完文件以后,需要关闭文件:int close(int fd )
这个fd是从使用open系统调用时返回的值
系统调用-读
int read (int fd, const void *buf , size_t length)
功能:
从文件描述符fd所指定的文件中读取length个字节到buf所指向的缓冲区中,返回值为实际读取的字节数
系统调用-写
int write(int fd, const void *buf,size_t length)
功能:
把length个字节从buf所指向的缓冲区中写到文件描述符fd所指向的文件中,返回值为实际写入的字节数
系统调用-定位
int lseek(int fd, offset_t offset, int whence)
功能:
把文件读写指针相对whence移动offset个字节。操作成功时,返回文件指针相对于文件头的位置
whence可使用下述值:
SEEK_SET——相对文件开头
SEEK_CUR——相对文件读写指针的当前位置
SEEK_END——相对文件末尾
offset可取负值,表示向前移动。
可用lseek(fd,0,SEEK_END)调用查看文件长度
系统调用-访问判断
判断文件是否可以进行某种操作(读,写等):
int access(const char *pathname, int mode)
-pathname:文件名称
-mode:要判断的访问权限。可以取下值或者是他们的组合。R_OK:文件可读,W_OK:文件可写,X_OK:文件可执行,F_OK文件存在
返回值:当我们测试成功时,函数返回0,否则如果一个条件不符合时,返回-1
实例分析:file_creat.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void create_file(char *filename){
if(creat(filename,0755)<0){
printf("create file %s failure!\n",filename);
exit(EXIT_FAILURE);
}else{
printf("create file %s success!\n",filename);
}
}
int main(int argc,char *argv[]){
int i;
if(argc<2){
perror("you haven't input the filename,please try again!\n");
exit(EXIT_FAILURE);
}
for(i=1;i<argc;i++){
create_file(argv[i]);
}
exit(EXIT_SUCCESS);
}
实例分析:file_open.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc ,char *argv[]){
int fd;
if(argc<2){
puts("please input the open file pathname!\n");
exit(1);
}
//如果flag参数里有O_CREAT表示,该文件如果不存在,系统则会创建该文件,、//该文件的权限由第三个参数决定,此处为0755
//如果flah参数里没有O_CREAT参数,则第三个参数不起作用.此时,如果要打//开的文件不存在,则会报错.
//所以fd=open(argv[1],O_RDWR),仅仅只是打开指定文件
if((fd=open(argv[1],O_CREAT|O_RDWR,0755))<0){
perror("open file failure!\n");
exit(1);
}else{
printf("open file %d success!\n",fd);
}
close(fd);
exit(0);
}
综合实例:file_cp.c
int main(int argc,char **argv)
{
int from_fd,to_fd;
int bytes_read,bytes_write;
char buffer[BUFFER_SIZE];
char *ptr;
if(argc!=3)
{
fprintf(stderr,"Usage:%s
fromfile tofile/n/a",argv[0]);
exit(1);
}
if((from_fd=open(argv[1],O_RDONLY))==-1)
{
fprintf(stderr,"Open %s
Error:%s/n",argv[1],strerror(errno));
exit(1);
}
if((to_fd=open(argv[2],O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))==-1)
{
fprintf(stderr,"Open %s
Error:%s/n",argv[2],strerror(errno));
exit(1);
}
while(bytes_read=read(from_fd,buffer,BUFFER_SIZE))
{
if((bytes_read==-1)&&(errno!=EINTR))
break;
else if(bytes_read>0)
{
ptr=buffer;
while(bytes_write=write(to_fd,ptr,bytes_read))
{
if((bytes_write==-1)&&(errno!=EINTR))break;
else
if(bytes_write==bytes_read) break;
else
if(bytes_write>0)
{
ptr+=bytes_write;
bytes_read-=bytes_write;
}
}
if(bytes_write==-1)break;
}
}
close(from_fd);
close(to_fd);
exit(0);
}
系统调用-创建
int creat(const char *filename, mode_t mode)
-filename:要创建的文件名(包含路径,缺省为当前路径)
-mode:创建模式
常见创建模式: S_IRUSR 可读
S_IWUSR 可写
S_IXUSR 可执行
S_IRWXU 可读可写可执行
除了可以使用上述宏以外,还可以直接使用数字来表示文件的访问权限:
可执行->1
可写->2
可读->4
上述值的和,如可读可写->6
无任何权限->0
文件描述
在linux系统中,所有打开的文件都对应一个文件描述符。文件描述符的本质是一个非负整数。当打开一个文件时,该整数由系统来分配。文件描述符的范围是0-OPEN_MAX。早起的unix版本OPEN_MANX=19,即允许每个进程同时打开20个文件,现在很多系统则将其增加至1024
系统调用-打开
int open(const char *pathname, int flags)
int open(const char * pthname, int flags, mode_t mode)
-pathname:要打开的文件名(包含路径,缺省为当前路径)
-flags:打开标志(具体查baidu),如O_CREAT,如果使用了O_CREAT标志,这时需要使用int open(const char * pthname, int flags, mode_t mode)函数,其意义是如果路径下没有该文件,则自己创建文件,(否则直接打开它)。这时需要指定mode来表示文件的访问权限
系统调用-关闭
当我们操作完文件以后,需要关闭文件:int close(int fd )
这个fd是从使用open系统调用时返回的值
系统调用-读
int read (int fd, const void *buf , size_t length)
功能:
从文件描述符fd所指定的文件中读取length个字节到buf所指向的缓冲区中,返回值为实际读取的字节数
系统调用-写
int write(int fd, const void *buf,size_t length)
功能:
把length个字节从buf所指向的缓冲区中写到文件描述符fd所指向的文件中,返回值为实际写入的字节数
系统调用-定位
int lseek(int fd, offset_t offset, int whence)
功能:
把文件读写指针相对whence移动offset个字节。操作成功时,返回文件指针相对于文件头的位置
whence可使用下述值:
SEEK_SET——相对文件开头
SEEK_CUR——相对文件读写指针的当前位置
SEEK_END——相对文件末尾
offset可取负值,表示向前移动。
可用lseek(fd,0,SEEK_END)调用查看文件长度
系统调用-访问判断
判断文件是否可以进行某种操作(读,写等):
int access(const char *pathname, int mode)
-pathname:文件名称
-mode:要判断的访问权限。可以取下值或者是他们的组合。R_OK:文件可读,W_OK:文件可写,X_OK:文件可执行,F_OK文件存在
返回值:当我们测试成功时,函数返回0,否则如果一个条件不符合时,返回-1
实例分析:file_creat.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void create_file(char *filename){
if(creat(filename,0755)<0){
printf("create file %s failure!\n",filename);
exit(EXIT_FAILURE);
}else{
printf("create file %s success!\n",filename);
}
}
int main(int argc,char *argv[]){
int i;
if(argc<2){
perror("you haven't input the filename,please try again!\n");
exit(EXIT_FAILURE);
}
for(i=1;i<argc;i++){
create_file(argv[i]);
}
exit(EXIT_SUCCESS);
}
实例分析:file_open.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc ,char *argv[]){
int fd;
if(argc<2){
puts("please input the open file pathname!\n");
exit(1);
}
//如果flag参数里有O_CREAT表示,该文件如果不存在,系统则会创建该文件,、//该文件的权限由第三个参数决定,此处为0755
//如果flah参数里没有O_CREAT参数,则第三个参数不起作用.此时,如果要打//开的文件不存在,则会报错.
//所以fd=open(argv[1],O_RDWR),仅仅只是打开指定文件
if((fd=open(argv[1],O_CREAT|O_RDWR,0755))<0){
perror("open file failure!\n");
exit(1);
}else{
printf("open file %d success!\n",fd);
}
close(fd);
exit(0);
}
综合实例:file_cp.c
int main(int argc,char **argv)
{
int from_fd,to_fd;
int bytes_read,bytes_write;
char buffer[BUFFER_SIZE];
char *ptr;
if(argc!=3)
{
fprintf(stderr,"Usage:%s
fromfile tofile/n/a",argv[0]);
exit(1);
}
if((from_fd=open(argv[1],O_RDONLY))==-1)
{
fprintf(stderr,"Open %s
Error:%s/n",argv[1],strerror(errno));
exit(1);
}
if((to_fd=open(argv[2],O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))==-1)
{
fprintf(stderr,"Open %s
Error:%s/n",argv[2],strerror(errno));
exit(1);
}
while(bytes_read=read(from_fd,buffer,BUFFER_SIZE))
{
if((bytes_read==-1)&&(errno!=EINTR))
break;
else if(bytes_read>0)
{
ptr=buffer;
while(bytes_write=write(to_fd,ptr,bytes_read))
{
if((bytes_write==-1)&&(errno!=EINTR))break;
else
if(bytes_write==bytes_read) break;
else
if(bytes_write>0)
{
ptr+=bytes_write;
bytes_read-=bytes_write;
}
}
if(bytes_write==-1)break;
}
}
close(from_fd);
close(to_fd);
exit(0);
}
相关文章推荐
- 【Android】Camera 使用浅析
- [python爬虫] Selenium定向爬取海量精美图片及搜索引擎杂谈
- Set Up VTune Amplifier(windows) 2015 for Remote (linux)Analysis
- 黑马程序员---java基础---网络编程
- 《php和mysql web开发》笔记——第9章 创建Web数据库
- thinkphp3.2.3子查询中遇到的错误
- 走向知识经济新时代
- 人工智能homework1_____c++ 回溯法解决数据(以命令行形式读入txt,输出在txt)
- Oracle RAC体系结构介绍
- JAVA——GUI鼠标事件
- 安卓控件使用系列11:ToggleButton开关控件的使用
- 二叉树的下一个结点
- linux下文件的特殊权限s和t
- java的访问权限
- C++学习指导
- 优先级队列
- 西方经济学流派
- JAVA——GUI窗口实现文本转移
- ubuntu下Android studio布局文件无法可视化问题-Could not initialize class com.android.layoutlib.bridge.impl.Render
- 7-v4l2——format negotiation 格式协商