首页 > 其他分享 >文件IO使用复习

文件IO使用复习

时间:2023-06-20 17:12:32浏览次数:26  
标签:文件 复习 int fd IO include open

2023/6/20 文件IO的使用快速复习相关参数,后续有新内容在添加

特点

  1. 没有缓冲机制
  2. 围绕文件描述符(非负整数int),依次分配
  3. 默认打开三个文件描述符 0标准输入 1标准输出 2标准错误
  4. 可以操作除d以外的任意类型文件
  5. 文件IO是在POSIX中定义的输入输出,不是C库中的输入输出

函数接口

int open(const char *pathname, int flags);//打开  返回值成功为文件描述符
//pathname 文件路径名   flags为打开文件的方式 O_RONLY 只读,O_WRONLY 只写
ssize_t read(int fd, void *buf, size_t count);//读
ssize_t write(int fd, const void *buf, size_t count);//写
int close(int fd);
off_t lseek(int fd, off_t offset, int whence);  //设置偏移量
//注意点返回值为文件的当前位置

功能实现

读写操作

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main()
{
    int fd=open("text.txt",O_RDWR);
    if(fd<0){ perror ("open err");return -1;}
	char buf[32]="";
    
	read(fd,buf,32);
    printf("%s\n",buf);
    write(fd,"hello",5);

    close(fd);
    return 0;
}

循环读源文件写新文件

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc,char const *argv[])
{
    int fd_src,fd_new;
    char buf[32]="";
    fd_src = open(argv[1], O_RDONLY);
    fd_new = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0666);//TRUNC 清空
    if(fd_src<0||fd_new<0){ perror("open");return -1;}
	while(1)
        {
            int s=read(fd_src,buf,32);//s为实际读到的个数
            if(s==0)break;
            write(fd_new,buf,s);
        }
    close(fd_src);
    close(fd_new);
    return 0;
    
}

标签:文件,复习,int,fd,IO,include,open
From: https://www.cnblogs.com/moveddown/p/17494117.html

相关文章

  • 列出包含某文件的目录
    FileFilterfilterForImageFolders=newFileFilter(){publicbooleanaccept(Filefolder){try{//Checkingonlydirectories,sincewearecheckingforfileswithin......
  • vue3+vite 动态引用静态资源,动态引入assets文件夹图片的几种方式
    可以参考这个回答,亲测有用 https://blog.csdn.net/weixin_43743175/article/details/125892613 ......
  • 批量打印文件doc,设置几分,powershell实现
    $folderPath="C:\path\to\folder"$printCopies=3Get-ChildItem-Path$folderPath-Filter*.doc|ForEach-Object{for($i=0;$i-lt$printCopies;$i++){Start-Process-FilePath$_.FullName-VerbPrint}}#一定要指定默认打印机......
  • 【ETL工具将数据源抽取到HDFS作为高可靠、高吞吐量的分布式文件系统存储】
    ETL工具的安装与配置常见的ETL工具包括ApacheNifi、Talend、Informatica、Datastage等。不论使用哪个工具,将数据源抽取到HDFS作为高可靠、高吞吐量的分布式文件系统存储是ETL工具的一项基本功能。基于Talend工具):1.下载Talend工具安装包在Talend官网上下载适合自己的TalendOp......
  • 执行cnpm install 时报错:randomUUID is not a function
    啊,熟悉的气息! TypeError:randomUUIDisnotafunction搜了一下得知:npm.taobao.org和registry.npm.taobao.org将在2022.06.30号正式下线和停止DNS解析。新域名切换规则:npm.taobao.org=>npmmirror.comregistry.npm.taobao.org=>registry.npmmirror.com ......
  • 永久设置最大同时打开文件数量ulimite的方法
    1.临时设置[root@host2~]#ulimit-n102402.永久设置在/etc/security/limits.conf 中加入*       soft  nofile     65535*       hard  nofile     65535[root@host2~]#tail/etc/security/limits.conf #*......
  • Optional 教程
    1、isPresent使用isPresent方法来判断非空,isPresent相当于!=nullisPresent返回一个booleanOptional<Student>optional=Optional.ofNullable(newStudent("王五",80));if(optional.isPresent()){//将输出:student1不为空的操作System.......
  • 消除if else & Optional 使用
    1、三目表达式获取对象的属性,判断对象是否为空,为空返回默认值Cc=newC("c");Stringname=c!=null?c.getName():DEFAULT_NAME;2、判断不为空再赋值Useruser=userDao.getUser(id);if(user!=null){user.setName("张三");}//使用OptionalOptional.ofN......
  • 【数据库原理、编程与性能】The Relational Model
    ChapterTwo-TheRelationalModel文章目录ChapterTwo-TheRelationalModel1.CAP数据库2.RelationalAlgebra2.1集合运算(SetTheoreticOperations)2.1.1交(intersection:)2.1.2并(union:)2.1.3差(difference:)2.1.4笛卡尔积(production:)2.2专门的关系运算(NativeSpecialOperat......
  • 【AGC】云数据库返回403client token authorization fail问题
    【关键字】AGC、云数据库、403【问题描述】有开发者反馈在使用AGC云数据库,拿到access_token用户登录返回403clienttokenauthorizationfail的问题。具体如下所述:用接口:https://connect-drcn.dbankcloud.cn/agc/apigw/oauth2/v1/token拿到了access_token,但用这个token去登录时:htt......