当前位置: 首页 > news >正文

网站建立免费安徽省住房城乡建设厅网站

网站建立免费,安徽省住房城乡建设厅网站,怎样做自己的 优惠卷网站,濮阳建网站#x1f4d8;北尘_#xff1a;个人主页 #x1f30e;个人专栏:《Linux操作系统》《经典算法试题 》《C》 《数据结构与算法》 ☀️走在路上#xff0c;不忘来时的初心 文章目录 一、C语言IO1、写文件2、读文件3、stdin stdout stderr 二、系统文件I/O1、写文件… 北尘_个人主页 个人专栏:《Linux操作系统》《经典算法试题 》《C》 《数据结构与算法》 ☀️走在路上不忘来时的初心 文章目录 一、C语言IO1、写文件2、读文件3、stdin stdout stderr 二、系统文件I/O1、写文件2、读文件 三、接口介绍四、文件描述符fd五、文件描述符的分配规则六、重定向 一、C语言IO 1、写文件 #include stdio.h #include string.h int main() {FILE *fp fopen(myfile, w);if(!fp){printf(fopen error!\n);}const char *msg hello bit!\n;int count 5;while(count--){fwrite(msg, strlen(msg), 1, fp);}fclose(fp);return 0; }2、读文件 #include stdio.h #include string.h int main() {FILE *fp fopen(myfile, r);if(!fp){printf(fopen error!\n);}char buf[1024];const char *msg hello bit!\n;while(1){//注意返回值和参数此处有坑仔细查看man手册关于该函数的说明ssize_t s fread(buf, 1, strlen(msg), fp);if(s 0){buf[s] 0;printf(%s, buf);}if(feof(fp)){break;}}fclose(fp);return 0; }3、stdin stdout stderr C默认会打开三个输入输出流分别是stdin, stdout, stderr 仔细观察发现这三个流的类型都是FILE*, fopen返回值类型文件指针 二、系统文件I/O 1、写文件 #include stdio.h #include sys/types.h #include sys/stat.h #include fcntl.h #include unistd.h #include string.h int main() {umask(0);int fd open(myfile, O_WRONLY|O_CREAT, 0644);if(fd 0){perror(open);return 1;}int count 5;const char *msg hello bit!\n;int len strlen(msg);while(count--){write(fd, msg, len);//fd: 后面讲 msg缓冲区首地址 len: 本次读取期望写入多少个字节的数 据。 返回值实际写了多少字节数据}close(fd);return 0; }2、读文件 #include stdio.h #include sys/types.h #include sys/stat.h #include fcntl.h #include unistd.h #include string.h int main() {int fd open(myfile, O_RDONLY);if(fd 0){perror(open);return 1;}const char *msg hello bit!\n;char buf[1024];while(1){ssize_t s read(fd, buf, strlen(msg));//类比writeif(s 0){printf(%s, buf);}else{break;}}close(fd);return 0; }三、接口介绍 使用man2号手册 #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); pathname: 要打开或创建的目标文件 flags: 打开文件时可以传入多个参数选项用下面的一个或者多个常量进行“或”运算构成flags。 参数:O_RDONLY: 只读打开O_WRONLY: 只写打开O_RDWR : 读写打开这三个常量必须指定一个且只能指定一个O_CREAT : 若文件不存在则创建它。需要使用mode选项来指明新文件的访问权限O_APPEND: 追加写返回值成功新打开的文件描述符失败-1四、文件描述符fd Linux进程默认情况下会有3个缺省打开的文件描述符分别是标准输入0 标准输出1 标准错误2. 0,1,2对应的物理设备一般是键盘显示器显示器 而现在知道文件描述符就是从0开始的小整数。当我们打开文件时操作系统在内存中要创建相应的数据结构来描述目标文件。于是就有了file结构体。表示一个已经打开的文件对象。而进程执行open系统调用所以必须让进程和文件关联起来。每个进程都有一个指针*files, 指向一张表files_struct,该表最重的部分就是包涵一个指针数组每个元素都是一个指向打开文件的指针所以本质上文件描述符就是该数组标。所以只要拿着文件描述符就可以找到对应的文件 五、文件描述符的分配规则 直接看代码 #include stdio.h #include sys/types.h #include sys/stat.h #include fcntl.h int main() {int fd open(myfile, O_RDONLY);if(fd 0){perror(open);return 1;}printf(fd: %d\n, fd);close(fd);return 0; }发现输入3 关闭0或者2 #include stdio.h #include sys/types.h #include sys/stat.h #include fcntl.h int main() {close(0);//close(2);int fd open(myfile, O_RDONLY);if(fd 0){perror(open);return 1;}printf(fd: %d\n, fd);close(fd);return 0; }发现是结果是 fd: 0 或者 fd 2 可见文件描述符的分配规则在files_struct数组当中找到当前没有被使用的最小的一个下标作为新的文件描述符。 六、重定向 那如果关闭1呢看代码 #include stdio.h #include sys/types.h #include sys/stat.h #include fcntl.h #include stdlib.h int main() {close(1);int fd open(myfile, O_WRONLY|O_CREAT, 00644);if(fd 0){perror(open);return 1;}printf(fd: %d\n, fd);fflush(stdout);close(fd);exit(0); }此时我们发现本来应该输出到显示器上的内容输出到了文件 myfile 当中其中fd1。这种现象叫做输出重定向。常见的重定向有:, , 那重定向的本质是什么呢 使用 dup2 系统调用 #include stdio.h #include unistd.h #include fcntl.h int main() {int fd open(./log, O_CREAT | O_RDWR);if (fd 0) {perror(open);return 1;}close(1);dup2(fd, 1);for (;;) {char buf[1024] {0};ssize_t read_size read(0, buf, sizeof(buf) - 1);if (read_size 0) {perror(read);break;}printf(%s, buf);fflush(stdout);}return 0; }
http://www.hkea.cn/news/14259919/

相关文章:

  • 使用wordpress需要懂什么语言搜索引擎优化涉及的内容
  • 如何建设废品网站福田补贴每人9000元
  • 3030wa网站开发学校wordpress 按钮特效
  • 搭建一个公司网站中国十大杰出建筑师
  • 汉阴县住房和城乡建设局网站有趣网址之家
  • 化工网站建设推广用返利网站做爆款
  • 优质高职院校建设网站网页源代码怎么打开快捷键
  • 注册网站费属于什么费用尚硅谷前端培训多少钱
  • 怎样建设自己的网站的视频苏州公司建站
  • pageadmin做网站大数据营销的弊端
  • 建设一个网站需要什么设备网站建设和执纪监督
  • 备案 网站名称怎样在百度上注册自己的店铺
  • 电子商务网站建设需求个人名义做网站能备案吗
  • 建设银行网站信息补充WordPress文章批量生成器
  • 淘宝购物返利网站建设app旅游网站开发的目的
  • 手机网站的尺寸做多大的广州达美网站建设
  • 社区网站建设申请报告seo建站网络公司
  • 肉部网站建设包括哪些郑州网站建设公司排行
  • 网站怎么做区域性优化曲靖网站建设公司靖网站建设
  • 墨尔本网站建设示范校建设验收网站
  • seo站点是什么意思做短视频网站好
  • 如何制作app软件演示教程seo优化交流
  • 一个做智能化的网站有哪些网上服务办事大厅
  • 网站设计便宜通用网址通用网站查询
  • 网上拿货做哪个网站好办公室装修报价表
  • 自定义wordpress首页标题seo如何网站正常更新
  • 做嫒嫒网站竞价推广平台
  • 微网站难做么上海网站的优化公司
  • 网站查看动漫制作专业研究生考啥
  • 淘客怎样做网站上海全网营销推广