首页 > 系统相关 >Linux文件IO

Linux文件IO

时间:2025-02-17 22:29:20浏览次数:6  
标签:文件 const int 写入 char fd IO Linux

1.打开文件:open

2.读写文件:read/write

3.关闭文件:close

open

函数接口:
       int open(const char *pathname, int flags);
       int open(const char *pathname, int flags, mode_t mode);
功能:打开一个文件
参数:
        pathname:打开的文件名
        flags:打开方式
                O_RDONLY : 只读方式,文件必须存在
                O_WRONLY :只写方式
                O_RDWR:      读写方式

               O_CREAT:文件不存在时创建
               O_TRUNC:清空文件
               O_APPEND:追加
        mode:文件被创建时,需要增加的读写执行权限
               0664

返回值:
      成功:文件描述符
      失败:-1

文件描述符:操作系统为已打开文件分配的标识符
           小的,非负的整形数据
           代表了一个已打开的文件

           默认文件描述符范围:0-1023
           分配原则:最小未被使用原则
           
            系统默认已打开的三个文件对应的文件描述符:

                                       标准io (FILE *)        文件io  (fd--》int)
            标准输入设备: stdin                                  0
            标准输出设备: stdout                                1
            标准出错设备: stderr                                2
    
 文件描述符泄露:打开的文件使用完时,未及时关闭
                                1.  使用完及时关闭
                                2.  修改文件描述上限


             "r"  ---->O_RDONLY 
             "r+" ---->O_RDWR
             "w" ---->O_WRONLY | O_CREAT |O_TRUNC, 0664
             "w+"--->O_RDWR | O_CREAT | O_TRUNC,  0664
             "a"  ---->O_WRONLY | O_CREAT | O_APPEND, 0664
             "a+" ---->O_RDWR | O_CREAT | O_APPEND, 0664           

read

ssize_t read(int fd, void *buf, size_t count);
功能:从文件中读取数据
参数:
         fd:要读的文件
         buf:存储读取到的数据的内存首地址
         count:尝试(期待)读到的字节数
返回值:
    成功:实际读到的字节数
     失败:-1
   文件末尾:0

write

 ssize_t write(int fd, const void *buf, size_t count);
功能:向文件中写入数据
参数:
        fd : 要写入的文件
        buf:要写入的数据的首地址
        count:写入的字节数
返回值:
    成功:实际写入的字节数
    失败:-1

使用read和write实现copy

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include <unistd.h>
#include<stdlib.h>
int main(int argc, char const *argv[])
{
    if(argc != 3)
    {
        printf("usage:./a.out srcfile stdfile\n");
        return 1;
    }
    int f = open(argv[1],O_RDONLY);
    int p = open(argv[2],O_WRONLY | O_CREAT | O_TRUNC,0664);
    
    char *a = malloc(1024);
    int size = sizeof(a);
    while(1)
    {
    ssize_t t;
    t = read(f,a,size);
    if(t <= 0)
    {
        break;
    }
 // printf("%s\n",a);
    write(p,a,t);
    }
    close(f);
    close(p);
    free(a);
    return 0;
}

seek

文件偏移函数:
lseek();
off_t lseek(int fd, off_t offset, int whence);
功能:重新定位文件的读写位置
参数:
          fd:定位的文件
         offset:定位的偏移量
          whence:要偏移的位置 
返回值:
       成功:当前读写位置到文件开头的偏移量
       失败:-1

时间相关函数接口

时间相关:

time();
功能:获取1970-1-1 0:0:0到现在的秒数
ctime();将秒数转换成字符串时间
localtime();将秒数转换成日历时间

printf相关


       *int printf(const char *format, ...);
       将格式化的字符串打印到终端
       int fprintf(FILE *stream, const char *format, ...);
       将格式化后的字符串写入到文件流指针所对用的文件里
       int dprintf(int fd, const char *format, ...);
        将格式化后的字符串写入到文件描述符所对用的文件里
       *int sprintf(char *str, const char *format, ...);
       将格式化后的字符串写入到str所指向的内存空间

标签:文件,const,int,写入,char,fd,IO,Linux
From: https://blog.csdn.net/Ctrlcctrlv001/article/details/145681257

相关文章