Linux中文件操作,open、read、write、lseek的用法

Linux中文件操作,open、read、write、lseek的用法
概述:
操作文件原理:打开/创建文件 --> 读取文件/写入文件 -->关闭文件
1、 在Linux当中操作文件,一般都是open打开一个文件,得到文件描述符,然后进行读写操作,后close关闭文件即可。

一、打开/创建文件

1.open函数
打开成功返回值大于0,否则小于0

//open函数需要的包含头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
        open函数的原型  
     
        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 :要创建的文件名字(包括路径)
        mode     :创建模式  //可读可写可执行
       

open函数demo:

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

int main()
{
   // int open(const char *pathname, int flags, mode_t mode);
   int fd;
     fd = open("./fide1",O_RDWR|O_CREAT,0600);

     if(fd>0){

       printf("open susess\n");
      }else{

       printf("open daffeat\n");
     }
    system("pause");
    return 0;        
      宏表示:   数字           
      S_IRUSR      4     可读
      S_IWUSR      2      可写
      S_IXUSR      1      可执行
      S_IRWXU      7      可读可写可执行

Flags权限:
O_RDONLY 只读打开 O_WRONLY只写打开 O_RDWR 可读可写打开

以上这三种中应当只选一种打开,下列常数是可选:

1、 O_CREAT 若文件不存在则创建,使用之后,需要同时说明第 三个参数mode,用其说明新文件的存取的权限。

2、O_EXCL 若同时指定O_CREAT,而文件已存在创建失败,即是返回值-1。

3、O_APPEND 每次写时加入文件的尾端。

4、O_TRUNC 如果存在文件内容的,而又只读只写成功打开,则 将其内容截短为0。

二、读/写文件
终端命令行 ls –l 列出文件的清单

 -rwxr-xr-x 1 CLC book  8544 1223 14:09 a.out
  -rwxr-xr-x 1 CLC book  8838 1215 19:49 changfile
  -rwxrw-rw- 1 CLC book  6097 1210 23:47 chenji2.c
  -rw------- 1 CLC book  6097 1214 17:09 chenji3.c
 -rwxr--r-- 1 CLC book  6097 1214 16:55 chenji.c
 -rwxr-xr-x 1 CLC book  9045 1222 11:06 clinet1.0
 -rw-r--r-- 1 CLC book  2334 1222 10:34 clinet.c

2、write()函数
读和写成功的返回值是文件的大小,失败返回-1

 #include <unistd.h>
        ssize_t write(int fd, const void *buf, size_t count);
            //  const void *buf  缓存区
             // size_t count  写去多大的字节  

参照demo:

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

int main()
{
   // int open(const char *pathname, int flags, mode_t mode);
  //  ssize_t write(int fd, const void *buf, size_t count); 
   int fd;
   int n_write;
   char *buf ="wo shi chengxuyuan";

   fd = open("./file1",O_RDWR|O_CREAT,0600);

   if(fd>0){

        printf("open susess\n");
        n_write  = write(fd,buf,strlen(buf));
        close(fd);

   }else{

     printf("open daffeat\n");
   }
    system("pause");
    return 0;
}
                                                                                                                                   

3、read()函数

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

参照demo:

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

int main()
{
   int fd;
   int n_read;
   int n_len;
   
        fd = open("./file1",O_RDWR|O_CREAT,0600);

        n_len = lseek(fd,0,SEEK_END);

        lseek(fd,0,SEEK_SET);

        char *buf = (char*)malloc(sizeof(char)*n_len);

        n_read  = read(fd,buf,n_len);
        printf("file1:%s\n",buf);
          
        system("pause");                                                                                                                                                                   
        return 0;          

4、lseek() 函数,光标定位

#include <sys/types.h>
#include <unistd.h>
     Off_t  seek (int fd, off_t offset, int whence);
                         
    /* off_t offset,:光标偏移量
    whence :1、SEEK_SET    定位到头位置
            2、SEEK_ENG  点位到尾巴位置
            3、SEEK_CUR  光标当前位置*/

参照如下的demo:利用光标偏移可以算出文件的大小

     fd = open("./file1",O_RDWR|O_CREAT,0600);   //打开或创建件
        int n_len = lseek(fd,0,SEEK_END);      //光标移至尾巴
        lseek(fd,0,SEEK_SET);                 //光标移至头
        close(fd);
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值