Linux文件操作

## linux文件的操作

**OPEN函数**
open函数(const char *pathname, int flags)
open函数(const char *pathname, int flags, mide_t mode)

> flags的三个参数
O-RDONLY      0
O_WRONLY     1
O_RDWR         2
> pathname    
文件名
> mode 参数
网络参考mode 参数说明,文件的读写权限,****四位可用数字相加代表权限
**CLOSE函数**
close( int fd )


实例
include<unistd.h>
include<stdlib.h>
include<sys/types.h>
include<sys/stat.h>
inculde<fcntl.h>
include<stdio.h>
int main(void)
{
   int temp;
   temp=open("./examtest" , O_RDWR|P_CREAT , S_IRWXU);
   printf("%d\n",temp);
   temp=close(temp);
   printf("%d/n",temp);
   exit(0);
}
**creat 函数**
int creat(const char *pathname , mode_t mode );
**write函数**
ssize_t write (int fd , void *buf , siz_t count)
 **read函数**
 ssize_t read(int fd , void *buf , size_t count ) ; 
 **实例**
 #include <fcntl.h>
 #include<unistd.h>
 #include<stdio.h>
 #define PERMS 0666
 #defineDUMMY 0
 #define MAXSIZE 1024
 int main (int argc .char *argv[])
 {
     int sourcefileID ,targetfileID;
     int readNO=0;
     char WRBuf[MAXSIZE];
     if(argc!=3)  //如果命令行参数不正确`
     {
        printf("run error\n");
        return 1;
     }
    if((sourcefileID=open(*(argv+1),O_RDONLY,DUMMY))==-1)
    {
      printf("Source file open error!\n");
      return 2; 
     }
    if ((targetfileID=open(*(argv+2),0_WRONLY|O_CREAT, PAEMS)==-1)
    {
      printf(“Target file open error!\n”);
      return 3;
   }
    while((readNO=read(sourcefileID, WRBuf ,MAXSIZE))>0)
    if(write (targetfileID,WRBuf,readNO)!=readNO)
    {
      printf("Target file write error!\n");
      return 4;
    }
    close(sourcefileID);
    close(targetfileID);
    return 0;
}

xxx@xxx:    gcc -g wxam3read.c -o examcopy
xxx@xxx:   ./examcopy lstest.txt copytest.txt

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值