linux 文件编程

open函数:

函数作用:打开和创建文件

#include<fcntl.h>
int open(constcharpathname,intflags);
int open(constchar
pathname,intflags,mode_t mode);
返回值:成功则返回文件描述符,否则返回-1
pathname :是待打开/创建文件的POSIX路径名(如/home/user/a.cpp)
flags 用于指定文件的打开/创建模式,这个参数可由以下常量(定义于fcntl.h)通过逻辑位或逻辑构成
第三个参数仅当创建新文件时(即 使用了O_CREAT 时)才使用,用于指定文件的访问权限位
O_RDONLY 只读模式
O_WRONLY 只写模式
O_RDWR 读写模式
打开/创建文件时,至少得使用上述三个常量中的一个。以下常量是选用的:
O_APPEND每次写操作都写入文件的末尾
O_CREAT如果指定文件不存在,则创建这个文件
O_EXCL如果要创建的文件已存在,则返回-1,并且修改errno的值
O_TRUNC如果文件存在,并且以只写/读写方式打开,则清空文件全部内容(即将其长度截短为0)
O_NOCTTY如果路径名指向终端设备,不要把这个设备用作控制终端。
O_NONBLOCK如果路径名指向FIFO/块文件/字符文件,则把文件的打开和后继I/O

代码示例:
打开/创建file1文件

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
int main(){
    int fd;
    fd = open("./file1", O_RDWR);
    if(fd == -1){
          printf("open F\n");
        fd =  open("./file1",O_RDWR|O_CREAT,0600);
        if(fd > 0){
               printf("creat success!!!\n");
        }
    }
    printf("fd = %d\n",fd);
    return 0 ;
}

代码示例:
O_EXCL如果要创建的文件已存在,则返回-1,并且修改errno的值
打开/创建file1文件

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
int main(){
    int fd;
    fd =  open("./file1",O_RDWR|O_CREAT|O_EXCL,0600);
    if(fd == 0){
               printf("file1  exist!!!\n");
    }
    printf("fd = %d\n",fd);
    
    return 0 ;
}

create函数:

int create (const char *filename,mode_t mode)
filename :想要创建的文件名(包含路径、缺省为当前路径)
mode :创建模式 //可读可写可执行

常见的创建模式:
宏表示 数字
S_IRUSR 4 可读
S_IWUSR 2 可写
S_IXUSR 1 可执行
S_IRWXU 7 可读、写、执行

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
int main(){
    int fd;
    fd =  create("./file2",S_IRWXU);
    return 0 ;
}

write函数:

头文件:<unistd.h>
write有两种用法。一种是:ssize_t write(int fd, const void buf, size_t nbyte);
fd:文件描述符;
buf:指定的缓冲区,即指针,指向一段内存单元;
nbyte:要写入文件指定的字节数;
返回值:写入文档的字节数(成功);-1(出错)
write函数把buf中nbyte写入文件描述符handle所指的文档,成功时返回写的字节数,错误时返回-1.
另一种是: write(const char
str,int n)
str是字符指针或字符数组,用来存放一个字符串。n是int型数,它用来表示输出显示字符串中字符的个数。
write(“string”,strlen(“string”);表示输出字符串常量

代码示例:
写入一段字符串到文件file1

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
int main(){
        int fd;
        char *buf="mading in china!!!!"; 
        fd = open("./file1", O_RDWR);
        if(fd == -1){
                printf("open F\n");
                fd =  open("./file1",O_RDWR|O_CREAT,0600);
                if(fd > 0){
                        printf("creat success!!!\n");
                }
        }
        printf("fd = %d\n",fd);

        write( fd, buf, strlen(buf));
        close(fd);//关闭文件
        return 0 ;
}

O_APPEND每次写操作都写入文件的末尾
代码示例:
写入一段字符串到文件file1,

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
int main(){
        int fd;
        char *buf="this is O_APPEND";
        fd =  open("./file1",O_RDWR|O_APPEND,0600)    
        printf("fd = %d\n",fd);
        write( fd, buf, strlen(buf));
        close(fd);
        return 0 ;
}

read函数:

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

返回值:
成功返回读取的字节数,出错返回-1并设置errno,如果在调read之前已到达文件末尾,则这次read返回0。

count是请求读取的字节数,读上来的数据保存在缓冲区buf中,同时文件的当前读写位置向后移
read函数返回时,返回值说明了buf中前多少个字节是刚读上来的。有些情况下,实际读到的字节数(返回值)会小于请求读的字节数count,例如:读常规文件时,在读到count个字节之前已到达文件末尾。例如,距文件末尾还有30个字节而请求读100个字节,则read返回30,下次read将返回0

代码示例:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
int main(){
        int fd;
        
        char *buf="mading in china!!!!";
        fd = open("./file1", O_RDWR);
        if(fd == -1){
                printf("open F\n");
                fd =  open("./file1",O_RDWR|O_CREAT,0600);
                if(fd > 0){
                        printf("creat success!!!\n");
                }
        }
        printf("fd = %d\n",fd);
        int n_write = write( fd, buf, strlen(buf));
        close(fd);
        
        fd = open("./file1", O_RDWR);//重新打开文件
        
        char *readbuf;
        readbuf=(char *)malloc(sizeof(char)*n_write+1);
        int n_read = read( fd, readbuf,n_write );
        printf("readbuf = %s,count = %d\n ",readbuf,n_read); 
        close(fd);
        return 0 ;
}

close函数:

#include <unistd.h>
int close(int fd);
关闭一个打开的文件

lseek函数:

头文件:#include <sys/types.h> #include <unistd.h>
用 法: off_t lseek(int fd, off_t offset, int whence);
//将文件读写指针相对whence移动offset个字节

lseek是一个用于改变读写一个文件时读写指针位置的一个系统调用。指针位置可以是绝对的或者相对的。

参数:
EEK_SET 将读写位置指向文件头后再增加offset个位移量。
SEEK_CUR 将读写位置当前位置增加offset个位移量。
SEEK_END 将读写位置指向文件尾后再增加offset个位移量。

返回值:
当调用成功时则返回目前的读写位置,也就是距离文件开头多少个字节。若有错误则返回-1,errno 会存放错误代码。
可能设置erron的错误代码:
EBADF: fildes不是一个打开的文件描述符。
ESPIPE:文件描述符被分配到一个管道、套接字或FIFO。
EINVAL:whence取值不当。

示例代码:
计算文件大小:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main(){
      int fd;
      char *buf = "mading in China";
      fd=open("./file",O_RDWR);
      int filesize = lseek(fd ,0 ,SEEK_END);
      printf("flie's size : %d \n",filesize);
      close(fd);
      return 0} 

文件光标移动:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
int main(){
        int fd;
        char *buf="mading in china!!!!";
        fd = open("./file1", O_RDWR);
        if(fd == -1){
                printf("open F\n");
                fd =  open("./file1",O_RDWR|O_CREAT,0600);
                if(fd > 0){
                        printf("creat success!!!\n");
                }
        }
        printf("fd = %d\n",fd);

        int n_write = write( fd, buf, strlen(buf));

        //    close(fd);
        //    fd = open("./file1", O_RDWR);
        //    off_t lseek(int fd, off_t offset, int whence);
        // lseek(fd, -19, SEEK_CUR);
        char *readbuf;
        readbuf=(char *)malloc(sizeof(char)*n_write+1);
        lseek(fd, -19, SEEK_CUR);
        int n_read = read( fd, readbuf,n_write );

        printf("readbuf = %s,count = %d\n ",readbuf,n_read);
        return 0 ;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值