linux文件操作函数

1 open函数

头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
两种函数原型

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

返回值:fd文件描述符
pathname:要打开或要创建的文件名字
flag:打开的方式,权限,有以下几种方式:
O_RDONLY:只读打开;
O_WRONLY:只写打开;
O_RDWR:读写;
O_APPEND:打开时光标到文件末端,写入时从文件末端开始;
O_CREAT:当文件不存在时,创建一个新文件,只有文件不存在才会创建;
O_EXCL:同时和O_CREAT使用,而文件已经存在时,则会报错(返回值-1),用于测试一个文件是否存在,如果不存在则创建文件。
O_TRUNC:如果打开的文件有内容,则会将文件内容截短为0,相当于删除原来文件内容。
mode:使用O_CREAT时,给创建的文件权限。
4:读权限
2:写权限
1:可执行
0600给用户可读可写权限。

  1 #include <sys/types.h>
  2 #include <sys/stat.h>
  3 #include <fcntl.h>
  4 #include <stdio.h>
  5 int main()
  6 {
  7  int fd;
  8  fd= open("./file1",O_RDWR);
  9  if(fd==-1)
 10  {
 11  printf("open file1 failed\n");
 12 
 13  fd=open("./file1",O_RDWR|O_CREAT,0600);
 14  if(fd>0){
 15   printf("create file1 sucess\n");
 16  }
 17  }
 18 
 19   return 0;
 20 
 21 
 22 }

2.write函数

#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);

fd:文件描述符
buf:写入的内容
count:写入的多少字节

  1 #include <sys/types.h>
  2 #include <sys/stat.h>
  3 #include <fcntl.h>
  4 #include <stdio.h>
  5 #include <unistd.h>
  6 #include <string.h>
  7 
  8 
  9 int main()
 10 {
 11  int fd;
 12  char *buf="chen zeyu shuai";
 13  fd= open("./file1",O_RDWR);
 14  if(fd==-1)
 15  {
 16  printf("open file1 failed\n");
 17 
 18  fd=open("./file1",O_RDWR|O_CREAT,0600);
 19  if(fd>0){
 20   printf("create file1 sucess\n");
 21  }
 22  }
 23  printf("open file1 sucess  fd=%d\n",fd);
 24  
 25   write(fd,buf,strlen(buf));
 26   close(fd);//关闭文件函数
 27   return 0;
 28 }
      

3.read函数

   #include <unistd.h>
   ssize_t read(int fd, void *buf, size_t count);

fd:文件描述符
buf:读出内容存放处
count:读出多少字节

  1 #include <sys/types.h>
  2 #include <sys/stat.h>
  3 #include <fcntl.h>
  4 #include <stdio.h>
  5 #include <unistd.h>
  6 #include <string.h>
  7 #include <stdlib.h>
  8 
  9 
 10 int main()
 11 {
 12  int fd;
 13  char *buf="chen zeyu shuai";
 14  fd= open("./file1",O_RDWR);
 15  if(fd==-1)
 16  {
 17  printf("open file1 failed\n");
 18 
 19  fd=open("./file1",O_RDWR|O_CREAT,0600);
 20  if(fd>0){
 21   printf("create file1 sucess\n");
 22  }
 23  }
 24  printf("open file1 sucess  fd=%d\n",fd);
 25
 26 int n_write=write(fd,buf,strlen(buf));
 27 if(n_write!=-1)
 28 {
 29 
 30 printf("write %d byte to file1\n",n_write);
 31 }
 32 char *readbuf;
 33 readbuf=(char*)malloc(sizeof(char)*n_write+1);
 34 int n_read=read(fd,readbuf,n_write);//返回值为读出多少字节数,读取失败返回-1
 35 printf("read %d context:%s\n",n_read,readbuf);
 36 close(fd);
 37   return 0;
 38 }
执行文件会读出为0个字节,因为文件打开,写入后,此时光标处于文件末端,读取函数读取光标后面的字节为0

4.lseek函数

  #include <sys/types.h>
  #include <unistd.h>
  off_t lseek(int fd, off_t offset, int whence);

fd:文件描述符;
offset:光标偏移值;
where:光标起始处
SEEK_SET:光标处于文件头
SEEK_CUR:当前位置
SEEK_END: 光标处于文件尾

 32 char *readbuf;
 33 readbuf=(char*)malloc(sizeof(char)*n_write+1);
 34 lseek(fd,0,SEEK_SET);//在读取前,将光标移动到文件头
 35 int n_read=read(fd,readbuf,n_write);//返回值为读出多少字节数,读取失败返回-1
 36 printf("read %d context:%s\n",n_read,readbuf);

5.creat函数

创建一个文件和open 的O_CREAT 效果一样

      #include <sys/types.h>
      #include <sys/stat.h>
      #include <fcntl.h>
     int creat(const char *pathname, mode_t mode);

pathname:文件路径与名字(缺省为当前路径)
mode:创建模式,给文件权限

S_IRUSR 4 可读
S_IWUSR 2 可写
S_IXUSR 1 可执行
S_IRWXU 7 可读写执行

  1 #include <sys/types.h>
  2 #include <sys/stat.h>
  3 #include <fcntl.h>
  4 #include <stdio.h>
  5 #include <unistd.h>
  6 #include <string.h>
  7 #include <stdlib.h>
  8 
  9 
 10 int main()
 11 {
 12  int fd;
 13  char *buf="chen zeyu shuai";
 14  fd=creat("/home/chen/xt/file2",S_IRWXU);
 15   return 0;
 16 
 17 
 18 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值