


#include
#include
#include //以上三行为open函数需包含的头文件
#include //printf函数需包含的头文件
int main(){
int fd;//文件描述符,索引值
//int open(const char *pathname, int flags);
//int open(const char *pathname, int flags, mode_t mode);
fd = open("./file1",O_RDWR);//文件名(含路径),可读可写权限
printf("fd = %d\n",fd);
return 0;
}

#include
#include
#include //以上三行为open函数需包含的头文件
#include //printf函数需包含的头文件
int main(){
int fd;
fd = open("./file1",O_RDWR);
printf("fd = %d\n",fd);
if(fd == -1){
printf("open file1 failed\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);//若文件不存在则创建
if(fd > 0){
printf("fd = %d\n",fd);
printf("create file1 success!\n");
}
}
return 0;
}





#include
#include
#include //以上三行为open函数需包含的头文件
#include //printf函数需包含的头文件
#include //write函数需包含的头文件
#include //strlen的头文件
int main(){
int fd;
char *buf = "Jessie is very kind.";
//int open(const char *pathname, int flags);
//int open(const char *pathname, int flags, mode_t mode);
fd = open("./file1",O_RDWR);
printf("fd = %d\n",fd);
if(fd == -1){
printf("open file1 failed\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);//若文件不存在则创建
if(fd > 0){
printf("fd = %d\n",fd);
printf("create file1 success!\n");
}
}
printf("open success : fd = %d\n",fd);//打开文件
//ssize_t write(int fd, const void *buf, size_t count);//write的函数原型
write(fd,buf,strlen(buf));//写入文件//在Linux中指针是固定8个字节,所以不能用sizeof
//stlen计算字符串长度
close(fd);//关闭文件
return 0;
}





#include
#include
#include //以上三行为open函数需包含的头文件
#include //printf函数需包含的头文件
#include //write函数需包含的头文件
#include //strlen的头文件
#include //malloc的头文件
int main(){
int fd;
char *buf = "Jessie is very kind.";
fd = open("./file1",O_RDWR);
printf("fd = %d\n",fd);
if(fd == -1){
printf("open file1 failed\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);//若文件不存在则创建
if(fd > 0){
printf("fd = %d\n",fd);
printf("create file1 success!\n");
}
}
printf("open success : fd = %d\n",fd);//打开文件
//ssize_t write(int fd, const void *buf, size_t count);
int n_write = write(fd,buf,strlen(buf));//存储在fd中写入所有的buf后的字节数
if(n_write != -1){
printf("write %d byte to file1\n",n_write);
}
close(fd);
fd = open("./file1",O_RDWR);//重新打开文件,光标移至头
char *readBuf;
readBuf = (char *)malloc(sizeof(char)*n_write + 1);
//ssize_t read(int fd, void *buf, size_t count);//read的函数原型
int n_read = read(fd,readBuf,n_write);//存储从fd中读出的readBuf的所有的字节数
printf("read %d ,context:%s\n",n_read,readBuf);
close(fd);//关闭文件
return 0;
}


#include
#include
#include //以上三行为open函数需包含的头文件
#include //printf函数需包含的头文件
#include //write函数需包含的头文件
#include //strlen的头文件
#include //malloc的头文件
int main(){
int fd;
char *buf = "Jessie is very kind.";
fd = open("./file1",O_RDWR);
printf("fd = %d\n",fd);
if(fd == -1){
printf("open file1 failed\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);//若文件不存在则创建
if(fd > 0){
printf("fd = %d\n",fd);
printf("create file1 success!\n");
}
}
printf("open success : fd = %d\n",fd);//打开文件
//ssize_t write(int fd, const void *buf, size_t count);//write的函数原型
int n_write = write(fd,buf,strlen(buf));//存储在fd中写入所有的buf后的字节数
if(n_write != -1){
printf("write %d byte to file1\n",n_write);
}
//close(fd);
//fd = open("./file1",O_RDWR);//重新打开文件,光标移至头
char *readBuf;
readBuf = (char *)malloc(sizeof(char)*n_write + 1);
//ssize_t read(int fd, void *buf, size_t count);//read的函数原型
//off_t lseek(int fd, off_t offset, int whence);//lseek的函数原型
//lseek(fd,0,SEEK_SET);//针对文件头的偏移值为0
lseek(fd,-20,SEEK_CUR);//光标定位至头(当前位置向左偏移20下)
//lseek(fd,-20,SEEK_END);
int n_read = read(fd,readBuf,n_write);//存储从fd中读出的readBuf的所有的字节数
printf("read %d ,context:%s\n",n_read,readBuf);
close(fd);//关闭文件
return 0;
}
#include
#include
#include //以上三行为open函数需包含的头文件
#include //printf函数需包含的头文件
#include //write函数需包含的头文件
#include //strlen的头文件
#include //malloc的头文件
int main(){
int fd;
char *buf = "Jessie is very kind.";
fd = open("./file1",O_RDWR);
int filesize = lseek(fd,0,SEEK_END);//lseek返回多少个字节
printf("file's size is :%d\n",filesize);
close(fd);//关闭文件
return 0;
}

#include
#include
#include
#include
int main(){
int fd;
fd = open("./file1",O_RDWR|O_CREAT|O_EXCL,0600);//若文件不存在则创建,已存在则出错
if(fd == -1){
printf("File1 exists.\n");
}
return 0;
}
#include
#include
#include //以上三行为open函数需包含的头文件
#include //printf函数需包含的头文件
#include //write函数需包含的头文件
#include //strlen的头文件
#include //malloc的头文件
int main(){
int fd;
char *buf = "Jessie is very kind.";
//fd = open("./file1",O_RDWR);
fd = open("./file1",O_RDWR|O_APPEND);//另起一行添加字符
printf("open success : fd = %d\n",fd);//打开文件
int n_write = write(fd,buf,strlen(buf));//存储在fd中写入所有的buf后的字节数
if(n_write != -1){
printf("write %d byte to file1\n",n_write);
}
close(fd);//关闭文件
return 0;
}


#include
#include
#include //以上三行为open函数需包含的头文件
#include //printf函数需包含的头文件
#include //write函数需包含的头文件
#include //strlen的头文件
#include //malloc的头文件
int main(){
int fd;
char *buf = "Jessie.";
fd = open("./file1",O_RDWR|O_TRUNC);//打开已有文件时,清空字符
printf("open success : fd = %d\n",fd);//打开文件
int n_write = write(fd,buf,strlen(buf));//存储在fd中写入所有的buf后的字节数
if(n_write != -1){
printf("write %d byte to file1\n",n_write);
}
close(fd);//关闭文件
return 0;
}


#include
#include
#include //以上三行为open函数需包含的头文件
#include //printf函数需包含的头文件
#include //write函数需包含的头文件
#include //strlen的头文件
#include //malloc的头文件
int main(){
int fd;
//int creat(const char *pathname, mode_t mode);
fd = creat("./file2",S_IRWXU);
return 0;
}




#include
#include
#include
#include
#include
#include
#include
int main(){
int fd;
char readBuf[128];
int n_read = read(0,readBuf,5);//从标准输入(键盘)读
int n_write = write(1,readBuf,strlen(readBuf));//写到标准输出(UNIX shell)
printf("\ndone!\n");
return 0;
}




#include
int main(int argc, char **argv){
printf("totol params: %d\n",argc);//参数总数
printf("No.1 params :%s\n",argv[0]);//参数名称,数组的形式//a.out
printf("No.2 params :%s\n",argv[1]);//src
printf("No.3 params :%s\n",argv[2]);//des()
return 0;
}

#include
#include
#include
#include
#include
#include
#include
int main(int argc, char **argv){
int fdSrc;//源文件描述符
int fdDes;//目标文件描述符
char *readBuf=NULL;
if(argc != 3){//对参数个数的判断
printf("pararm error\n");
exit(-1);
}
fdSrc = open(argv[1],O_RDWR);//打开源文件
int size = lseek(fdSrc,0,SEEK_END);//算出源文件的字节大小
readBuf=(char *)malloc(sizeof(char)*size + 8);//开辟比源文件多8个字节的大小
lseek(fdSrc,0,SEEK_SET);//光标移至源文件内容的头
int n_read = read(fdSrc, readBuf, size);//读源文件到readBuf,要用size
fdDes = open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);//打开/创建目标文件,目标文件已存在时清空内容
int n_write = write(fdDes,readBuf,strlen(readBuf));//将readBuf写入目标文件
close(fdSrc);//关闭源文件
close(fdDes);//关闭目标文件
return 0;
}

int n_read = read(fdSrc, readBuf, size);//读源文件到readBuf,要用size
fdDes = open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);//打开/创建目标文件,目标文件已存在时清空内容