linux文件操作API 函数open read write close

来源:互联网 发布:五毛特效是什么软件 编辑:程序博客网 时间:2024/05/20 12:23

原文地址: http://blog.csdn.net/u014650722/article/details/51563679
1. open()函数

功能描述:用于打开或创建文件,在打开或创建文件时可以指定文件的属性及用户的权限等各种参数。

所需头文件:

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

函数原型:int open(const char *pathname,int flags,int perms)

参数:

pathname:被打开的文件名(可包括路径名如”dev/ttyS0”)

flags:文件打开方式,

O_RDONLY:以只读方式打开文件

O_WRONLY:以只写方式打开文件

O_RDWR:以读写方式打开文件

O_CREAT:如果改文件不存在,就创建一个新的文件,并用第三个参数为其设置权限

O_EXCL:如果使用O_CREAT时文件存在,则返回错误消息。这一参数可测试文件是否存在。此时open是原子操作,防止多个进程同时创建同一个文件

O_NOCTTY:使用本参数时,若文件为终端,那么该终端不会成为调用open()的那个进程的控制终端
O_TRUNC:若文件已经存在,那么会删除文件中的全部原有数据,并且设置文件大小为0
O_APPEND:以添加方式打开文件,在打开文件的同时,文件指针指向文件的末尾,即将写入的数据添加到文件的末尾

O_NONBLOCK: 如果pathname指的是一个FIFO、一个块特殊文件或一个字符特殊文件,则此选择项为此文件的本次打开操作和后续的I/O操作设置非阻塞方式。

O_SYNC:使每次write都等到物理I/O操作完成。
O_RSYNC:read 等待所有写入同一区域的写操作完成后再进行
在open()函数中,falgs参数可以通过“|”组合构成,但前3个标准常量(O_RDONLY,O_WRONLY,和O_RDWR)不能互相组合。

perms:被打开文件的存取权限,可以用两种方法表示,可以用一组宏定义:S_I(R/W/X)(USR/GRP/OTH),其中R/W/X表示读写执行权限,

USR/GRP/OTH分别表示文件的所有者/文件所属组/其他用户,如S_IRUUR|S_IWUUR|S_IXUUR,(-rex——),也可用八进制800表示同样的权限

返回值:

成功:返回文件描述符

失败:返回-1

2. close()函数

功能描述:用于关闭一个被打开的的文件

所需头文件:

 #include <unistd.h>

函数原型:int close(int fd)

参数:fd文件描述符

函数返回值:0成功,-1出错

3. read()函数

功能描述: 从文件读取数据。
所需头文件: #include <unistd.h>

函数原型:ssize_t read(int fd, void *buf, size_t count);

参数:
fd: 将要读取数据的文件描述词。
buf:指缓冲区,即读取的数据会被放到这个缓冲区中去。
count: 表示调用一次read操作,应该读多少数量的字符。

返回值:返回所读取的字节数;0(读到EOF);-1(出错)。

以下几种情况会导致读取到的字节数小于 count :

A. 读取普通文件时,读到文件末尾还不够 count 字节。例如:如果文件只有 30 字节,而我们想读取 100
字节,那么实际读到的只有 30 字节,read 函数返回 30 。此时再使用 read 函数作用于这个文件会导致 read 返回 0 。
B. 从终端设备(terminal device)读取时,一般情况下每次只能读取一行。
C. 从网络读取时,网络缓存可能导致读取的字节数小于 count字节。
D. 读取 pipe 或者 FIFO 时,pipe 或 FIFO 里的字节数可能小于 count 。
E. 从面向记录(record-oriented)的设备读取时,某些面向记录的设备(如磁带)每次最多只能返回一个记录。
F. 在读取了部分数据时被信号中断。
读操作始于 cfo 。在成功返回之前,cfo 增加,增量为实际读取到的字节数。

  1. write()函数

功能描述: 向文件写入数据。
所需头文件: #include

#include <stdio.h>#include <string.h>#include <stdlib.h>  #include <unistd.h>#include <fcntl.h>#include<sys/types.h>#include<sys/stat.h>#include <errno.h>#define BUFFER_SIZE 128                               //每次读写缓存大小,影响运行效率#define SRC_FILE_NAME "src_file.txt"              //源文件名#define DEST_FILE_NAME "dest_file.txt"          //目标文件名#define OFFSET 0                                             //文件指针偏移量int main(){      int src_file,dest_file;      unsigned char src_buff[BUFFER_SIZE];      unsigned char dest_buff[BUFFER_SIZE];      int real_read_len = 0;      char str[BUFFER_SIZE] = "this is a testabout\nopen()\nclose()\nwrite()\nread()\nlseek()\nend of the file\n";      //创建源文件      src_file=open(SRC_FILE_NAME,O_RDWR|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);        if(src_file<0)      {            printf("open file error!!!\n");            exit(1);      }     //向源文件中写数据    write(src_file,str,sizeof(str));     //创建目的文件     dest_file=open(DEST_FILE_NAME,O_RDWR|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);        if(dest_file<0)      {          printf("open file error!!!\n");            exit(1);      }      lseek(src_file,OFFSET,SEEK_SET);//将源文件的读写指针移到起始位置      while((real_read_len=read(src_file,src_buff,sizeof(src_buff)))>0)      {        printf("src_file:%s",src_buff);            write(dest_file,src_buff,real_read_len);      }    lseek(dest_file,OFFSET,SEEK_SET);//将目的文件的读写指针移到起始位置    while((real_read_len=read(dest_file,dest_buff,sizeof(dest_buff)))>0);//读取目的文件的内容    printf("dest_file:%s",dest_buff);      close(src_file);      close(dest_file);      return 0;}

结果 如下:

src_file:this is a test aboutopen()close()write()read()lseek()end of the filedest_file:this is a test aboutopen()close()write()read()lseek()end of the file

函数实例2

#include <stdio.h>#include <string.h>#include <stdlib.h>  #include <unistd.h>#include <fcntl.h>#include<sys/types.h>#include<sys/stat.h>#include <errno.h>#define BUFFER_SIZE 1024#define OFFSET 0                          //文件指针偏移量#define IP_FILE "ip.txt"int creat_fd,write_fd,open_fd;char readbuf[BUFFER_SIZE];char writebuf[]="192.168.1.1";int main(int argc, char *argv[]){int ret=1;int file_len=0;#if 0/* 导入参数 */ //para1:command name para2:filenameif (argc != 2){printf("argc number error!need 2,support %d\n",argc);exit(1);}#endif//creat filecreat_fd=open(IP_FILE,O_RDWR|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); if(creat_fd<0)      {        printf("creat serverip file error!!!\n");        exit(1);      }//write data to filewrite_fd=write(creat_fd,writebuf,sizeof(writebuf));if(write_fd<0)      {        printf("write data to serverip.txt error!!!\n");        exit(1);      }//open file//open_fd= open(argv[1],O_RDWR);open_fd= open(SERVERIP_FILE,O_RDWR);if(open_fd<0){printf("read file error!!!\n");exit(1);}//测量文件大小//off_t lseek(int fd, off_t offset,int whence);file_len=lseek(open_fd,OFFSET,SEEK_END);//返回值为文件大小+offsetlseek(open_fd,OFFSET,SEEK_SET);         //重定位文件开始printf("file size=%d\n",file_len);//read file//ssize_t read(int fd, void *buf, size_t count); //文件读写位置会随读取到的字节移动,如果返回0,表示已到达文件尾或是无可读取的数据while(ret){ret=read(open_fd,readbuf,BUFFER_SIZE);if(ret == -1){printf("read file error!!!\n");exit(1);}file_len-=ret;}printf("there are %d byte(s) data left without read\n", file_len);printf("%s\n",readbuf);//close file close(open_fd);return 0;}
阅读全文
0 0