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

建材建设网站建设wap网站

建材建设网站,建设wap网站,做网站注意事项,做建筑材料的网站文件的写入 需要包含的头文件 #include unistd.h 相关的API函数 ssize_t write(int fd, const void *buf, size_t count); 参数说明 int fd #xff1a;文件描述符const void *buf #xff1a;一个无类型的指针buf#xff0c;是一个缓冲区size_t count#xf…文件的写入 需要包含的头文件 #include unistd.h 相关的API函数 ssize_t write(int fd, const void *buf, size_t count); 参数说明 int fd 文件描述符const void *buf 一个无类型的指针buf是一个缓冲区size_t count要写入文件的大小 可以理解为write函数就是将count大小的数据从buf缓冲区写到fd对应的文件里去 返回值成功则返回 写入的字节数失败则返回 -1 实操演示 1. 将上节的demo1.c 复制一份并重命名为 demo2.c 2. 同样使用man函数来查看write函数相关信息man 2 write 3. 在demo2.c中修改代码并保存退出 #include sys/types.h #include sys/stat.h #include fcntl.h #include stdio.h #include unistd.h #include string.hint main() {int fd; // file descriptionchar *buf mjmmjm;fd open(./file1,O_RDWR|O_CREAT, 0600);printf(file description %d, open successfully!\n,fd);write(fd, buf, strlen(buf)); //对于字符串常量来说使用sizeof求出的是系统用多少字节表示一个字符型指针必须使用strlen才可以表示字符串的有效长度close(fd); //close after writing return 0; } 注释中提到过这里再强调一下 对于字符串常量来说使用sizeof求出的是系统用多少字节表示一个字符型指针必须使用strlen才可以表示字符串的有效长度 详见C语言的字符串_mjmmm的博客-CSDN博客 4. 运行程序并查看file1: 可见成功写入 文件的读取 需要包含的头文件 #include unistd.h 相关的API函数 ssize_t read(int fd, void *buf, size_t count); 参数说明 int fd 文件描述符const void *buf 一个无类型的指针buf是一个缓冲区size_t count要读取文件的大小 可以理解为read函数就是从fd对应的文件中读取count大小的数据放到buf缓冲区 返回值成功则返回 读取到的字节数失败则返回 -1 这里涉及到一个从哪里开始读的问题答案是从光标处开始读 实操演示 1. 将demo2.c 复制一份并重命名为 demo3.c 2.  同样可以使用 man 2 read: 3. 在demo3.c中修改代码并保存退出 #include sys/types.h #include sys/stat.h #include fcntl.h #include stdio.h #include unistd.h #include string.h #include stdlib.hint main() {int fd; // file descriptionchar *buf mjmmjm;char *buf_read;fd open(./file1,O_RDWR|O_CREAT, 0600);printf(file description %d, open successfully!\n,fd);int count_write write(fd, buf, strlen(buf));if(count_write ! -1){printf(%d bytes data has been written,count_write);buf_read (char *)malloc(sizeof(char)*count_write 1); //为野指针开辟空间防止段错误且由于malloc返回值是void类型的指针所以需要强制类型转换成char类型的指针}else{printf(fail to write);exit(-1);}int count_read read(fd, buf_read, count);if(count_read ! -1){printf(%d bytes data has been read, context:%s\n,count_read, buf_read);}else{printf(fail to read);}close(fd); //close after reading return 0; } 4. 删除file1避免之前写入过导致结果不准确并运行代码 可见虽然代码的逻辑似乎没有问题但是却一个字节都没有读到原因就是刚刚提到的读取文件存在一个“从哪里开始读”的问题而由于刚刚对文件进行write操作之后光标处于文件的末尾所以从此时如果从光标处read注定是无法读到任何信息的。 解决这个问题的方法有两种 将光标移到头再读将文件重新打开 5. 完善代码 使用“重新打开文件”的方法解决问题 这种方式比较好实现但是效率不高而且思路有些反人类 #include sys/types.h #include sys/stat.h #include fcntl.h #include stdio.h #include unistd.h #include string.h #include stdlib.hint main() {int fd; // file descriptionchar *buf mjmmjm;char *buf_read;fd open(./file1,O_RDWR|O_CREAT, 0600);printf(file description %d, open successfully!\n,fd);int count_write write(fd, buf, strlen(buf));if(count_write ! -1){printf(%d bytes data has been written\n,count_write);buf_read (char *)malloc(sizeof(char)*count_write 1);}else{printf(fail to write\n);exit(-1);}close(fd); //关闭文件fd open(./file1,O_RDWR, 0600); //重新打开文件int count_read read(fd, buf_read, count_write);if(count_read ! -1){printf(%d bytes data has been read, context:%s\n,count_read, buf_read);}else{printf(fail to read\n);}close(fd); //close after reading return 0; } 实现效果 使用“移动光标”的方法解决问题 关于光标的控制 需要包含的头文件 #include sys/types.h #include unistd.h 相关的API函数 off_t lseek(int fd, off_t offset, int whence); 参数说明 int fd 文件描述符off_t offset偏移多少个字节int whence光标偏移位置 其中whence可以设置为 以下3个值中的一个 SEEK_SET文件开头SEEK_CUR光标当前位置SEEK_END文件尾 可以理解为lseek函数就是将光标移到whenceoffset的位置 返回值成功则 返回从文件开始的偏移了多少字节发生错误时返回 -1 #include sys/types.h #include sys/stat.h #include fcntl.h #include stdio.h #include unistd.h #include string.h #include stdlib.hint main() {int fd; // file descriptionchar *buf mjmmjm;char *buf_read;fd open(./file1,O_RDWR|O_CREAT, 0600);printf(file description %d, open successfully!\n,fd);int count_write write(fd, buf, strlen(buf));if(count_write ! -1){printf(%d bytes data has been written\n,count_write);buf_read (char *)malloc(sizeof(char)*count_write 1);}else{printf(fail to write\n);exit(-1);}lseek(fd, 0, SEEK_SET); //将光标移到最开头int count_read read(fd, buf_read, count_write);if(count_read ! -1){printf(%d bytes data has been read, context:%s\n,count_read, buf_read);}else{printf(fail to read\n);}close(fd); //close after reading return 0; } 实现效果
http://www.zqtcl.cn/news/743087/

相关文章:

  • 专业提供网站建设服务包括做解析视频网站违法莫
  • 天津工程建设协会网站wordpress 自由评论
  • 南同网站建设hr系统管理软件排名
  • 水果网店网站建设策划书做企业形象网站
  • 小清新博客网站软件公司有哪些部门
  • 企业网站托管一年多少钱想学电商运营在哪里学
  • 网站建设自评报告手机电商平台怎么做的
  • 安阳网站建设优化免费的免抠图素材网站
  • 网站主机有什么用seo网站课程
  • 网站关键词优化软件网站的二次开发
  • 网站建设技术服务费怎么入账杭州网站推广与优化
  • 咨询类网站建设方案书重庆360网络推广
  • 简单网站模板下载wordpress调用数据库字段
  • 万网空间最多放几个网站好的网站首页的特点
  • .net做网站安全吗wordpress取消邮件验证
  • 沈阳做网站推广唐山网站怎么做seo
  • 网站备案说主体已注销刷关键词指数
  • 学做网站教学百度网盘动软代码生成器 做网站
  • 长辛店网站建设手机评测网站
  • 网站建设公司选哪个好软件开发
  • 隐形眼镜网站开发的经济效益莘县网站开发
  • 开创集团网站建设如何在学校网站上做链接
  • 上海优秀网站设计百度投诉中心人工电话号码
  • 卖建材的网站有哪些跨境电商工具类产品的网站
  • 做毕业网站的周记网站开发项目书
  • 门户网站价格仿站工具下载后咋做网站
  • 国外优秀ui设计网站常州网站建设电话
  • 大连手机网站建设做外贸无网站如何做
  • 做旅游门票网站需要什么材料人工智能培训机构哪个好
  • 免费的网站程序个人网站可以做论坛么