## 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