open函数

文件编程

liunx系统API:

打开:open
读写:write/read
光标定位:lseek
关闭:close

open函数:

文件描述符:读写函数根据文件描述符进行后续的操作
文件描述符:索引的作用
open函数的返回值就是文件描述符
fd为正整数,文件存在,打开成功;为-1打开失败,不存在文件

函数原型

int open(const char *pathname, int flags);
参数说明:
pathname:要打开的文件名
flags:O_RDONLY 只读打开
      O_WRONLY 只写打开
      O_RDWR  可读可写打开
      这三个权限只能指定一个
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

//int open(const char *pathname, int flags);

int main()
{
    int fd;//文件描述符,后续的读写根据文件描述符操作

    fd=open("/file1",O_RDWR);//用只读只写的方式打开文件,文件描述符是open函数的返回值   

    printf("fd=%d\n",fd);//文件存在,文件描述符是正整数,否则是负数

    return 0;
}

可选择的参数:

O_CREAT:文件不存在,创建,需要第三个参数mode,说明该文件的存取许可权限
O_EXCL:同时指定了O_CREAT,文件已经存在,打开文件失败,返回-1;
O_TRUNC:打开文件,若文件本来有内容,只读或只写打开,把内容全部干掉。
O_APPEND:每次写都加到文件的尾端
#include<stdio.h>
#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);

int main()
{
    int fd;//文件描述符

    fd=open("./file1",O_RDWR);//只读只写的方式打开文件
    if(fd==-1){//如果返回值是负数,文件不存在,打开失败
        printf("open file fail\n");
	fd=open("./file1",O_RDWR|O_CREAT,0600);//文件不存在创建文件,文件的权限是0600(可读可写)
	if(fd>0){//如果返回值是正整数,文件存在,打开成功
	    printf("open file success\n");
	}
    }
 
    printf("open success:fd=%d\n",fd);

    return 0;
}
#include<stdio.h>
#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);

int main()
{
    int fd;//文件描述符

    fd=open("./file1",O_RDWR|O_CREAT|O_EXCL,0600);//文件不存在创建文件,文件的权限是0600(可读可写),指定了O_CREAT,文件已经存在,打开文件失败,返回-1;
 
    if(fd==-1){
         printf("file1 cunzai\n");
    }

    return 0;
}
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include<string.h>
#include<stdlib.h>

//  int open(const char *pathname, int flags);
//  int open(const char *pathname, int flags, mode_t mode);
//  ssize_t write(int fd, const void *buf, size_t count);
//  含义:将缓冲区buf指针指向内存的数据,写count个字节,写进fd,fd是打开文件的文件描述符。
//  成功写入返回写入的个数,返回的值是正整数。
//ssize_t read(int fd, void *buf, size_t count);
//  含义:从fd这个文件读取count个字节到缓冲区buf指针指向的内存

int main()
{
    int fd;//文件描述符
    int n_write;//写入文件内容的个数
    char *buf="chenlichen hen shaui!";//write函数缓冲区存放的内容

    fd=open("./file1",O_RDWR|O_APPEND);//只读只写的方式打开文件,把内容写进尾端
 

    n_write=write(fd,buf,strlen(buf));//把内容写入文件
    if(n_write!=-1){//返回值不是负数,为正整数,写入文件成功
         printf("write %d byte to file\n",n_write);
    }
    
 
    close(fd);//关闭文件

    return 0;
}
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include<string.h>
#include<stdlib.h>

//  int open(const char *pathname, int flags);
//  int open(const char *pathname, int flags, mode_t mode);
//  ssize_t write(int fd, const void *buf, size_t count);
//  含义:将缓冲区buf指针指向内存的数据,写count个字节,写进fd,fd是打开文件的文件描述符。
//  成功写入返回写入的个数,返回的值是正整数。
//ssize_t read(int fd, void *buf, size_t count);
//  含义:从fd这个文件读取count个字节到缓冲区buf指针指向的内存

int main()
{
    int fd;//文件描述符
    int n_write;//写入文件内容的个数
    char *buf="test";//write函数缓冲区存放的内容

    fd=open("./file1",O_RDWR|O_TRUNC);//只读只写的方式打开文件,把原来的内容截断为0

    n_write=write(fd,buf,strlen(buf));//把内容写入文件
    if(n_write!=-1){//返回值不是负数,为正整数,写入文件成功
         printf("write %d byte to file\n",n_write);
    }
    
 
    close(fd);//关闭文件

    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值