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

苏州最大的网站建设公司网站建设经验王者荣耀恺和

苏州最大的网站建设公司,网站建设经验王者荣耀恺和,织梦做博客类网站,北京装修公司口碑排行目录 换行符刷新缓冲区(c和c通用) 输入事件到来(c和c通用) 控制符flush(c特有) 控制符endl(c特有) c库函数:fflush刷新缓冲区(c特有) sync(linux) fsync(linux) fdatasync (linux) 在使用printf或者cout等将数据输出到显示器上时&…

目录

换行符刷新缓冲区(c和c++通用)

输入事件到来(c和c++通用)

控制符flush(c++特有)

控制符endl(c++特有)

c库函数:fflush刷新缓冲区(c特有)

sync(linux)

fsync(linux)

fdatasync (linux)


 在使用printf或者cout等将数据输出到显示器上时,数据会先进入对应的输出缓冲区,只有当缓冲区被刷新时,数据才会真正来到显示器。

换行符刷新缓冲区(c和c++通用)

#include <iostream>
#include <stdio.h>
#include <unistd.h>
using namespace std;// 换行符刷新缓冲区
int main()
{// cout不会直接将“123”输出到显示器上,而是将其发送到输出缓冲区// 输出缓冲区收到换行符后,会刷新输出缓冲区至显示器上// 也可以从面向对象的角度思考一下这个问题:// 我想要输出一些数据到显示器上// cout用于将数据输出到显示器上// 可是我想一次发一个完整的数据给显示器,那么什么时候才是这个完整的数据呢// "\n"告诉cout这个完整的数据我写完了,你去发给显示器吧cout << 123;sleep(3);cout << "\n";sleep(3);return 0;
} 

输入事件到来(c和c++通用)

#include <iostream>
#include <stdio.h>
#include <unistd.h>
using namespace std;// 输入事件到来
int main()
{// 当需要输入时,会刷新输出缓冲区至显示器上cout << "enter a number: ";int a;cin >> a;return 0;
} 

控制符flush(c++特有)

#include <iostream>
#include <stdio.h>
#include <unistd.h>
using namespace std;// 控制符flush
int main()
{cout << 123;sleep(3);// cout << flush; //--->实际上ostream类对插入运算符<<进行了重载。cout << flush被替换成flush(cout)flush(cout);sleep(3);return 0;
} 

控制符endl(c++特有)

#include <iostream>
#include <stdio.h>
#include <unistd.h>
using namespace std;// 控制符endl
int main()
{// endl也会刷新cout对应的输出缓冲区至显示器上,并添加换行符'\n'cout << 123;sleep(3);cout << endl;sleep(3);return 0;
} 

c库函数:fflush刷新缓冲区(c特有)

#include <iostream>
#include <stdio.h>
#include <unistd.h>
using namespace std;// c库函数:fflush刷新缓冲区
// int fflush(FILE *stream);
int main()
{// endl也会刷新cout对应的输出缓冲区至显示器上,并添加换行符'\n'printf("123");sleep(3);fflush(stdout);sleep(3);return 0;
}

sync(linux)

        将所有文件已修改的数据从内核缓冲区写回存储设备;将所有文件的元数据(如文件大小、权限、时间戳等)从内核缓冲区写回存储设备。

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/sync.h>int main() {int fd = open("test.txt", O_WRONLY | O_CREAT, 0644);if (fd == -1) {perror("open");return 1;}const char *message = "Hello, sync!";// write并不会把数据直接写入磁盘,而是写入到该文件对应的内核缓冲区ssize_t bytes_written = write(fd, message, sizeof(message) - 1);if (bytes_written == -1) {perror("write");close(fd);return 1;}// 调用sync函数确保数据写入磁盘// 刷新所有内核缓冲区,包括这个文件和其他文件对应的内核缓冲区// 会刷新每个文件对应的内核缓冲区至各自对应的磁盘区域上sync();close(fd);return 0;
}

fsync(linux)

        将特定一个文件已修改的数据从内核缓冲区写回存储设备;将特定一个文件的元数据(如文件大小、权限、时间戳等)从内核缓冲区写回存储设备。

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>int main() {int fd = open("test_fsync.txt", O_WRONLY | O_CREAT, 0644);if (fd == -1) {perror("open");return 1;}const char *message = "Hello, fsync!";ssize_t bytes_written = write(fd, message, sizeof(message) - 1);if (bytes_written == -1) {perror("write");close(fd);return 1;}// 调用fsync函数确保该文件对应内核缓冲区数据和元数据写入磁盘if (fsync(fd) == -1) {perror("fsync");close(fd);return 1;}close(fd);return 0;
}

        这里,在写入数据后,我们调用 fsyncfsync 会确保不仅文件内容(数据)被写入磁盘,而且文件的元数据,如文件大小、权限、时间戳等也被同步更新到磁盘。这对于需要严格一致性的应用场景非常重要,比如文件系统的元数据更新操作。

fdatasync (linux)

        只保证将特定一个文件对应的内核缓冲区中的数据写入到磁盘。

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>int main() {int fd = open("test_fdatasync.txt", O_WRONLY | O_CREAT, 0644);if (fd == -1) {perror("open");return 1;}const char *message = "Hello, fdatasync!";ssize_t bytes_written = write(fd, message, sizeof(message) - 1);if (bytes_written == -1) {perror("write");close(fd);return 1;}// 调用fdatasync函数确保该文件内核缓冲区数据写入磁盘if (fdatasync(fd) == -1) {perror("fdatasync");close(fd);return 1;}close(fd);return 0;
}
http://www.yayakq.cn/news/887154/

相关文章:

  • 购物网站开发需要什么技术网站备案前置审批表格
  • 福州网站网站建设dt网站设计
  • 网站做cdn需要注意什么意思自己做网站需不需要钱
  • 手机如何访问电脑做的asp网站wp做购物网站
  • 宣传册设计及网站建设开发平台为用户提供了哪四类
  • 网站服务器查询柒比贰wordpress主题
  • 什么专业会制作网站关于网站建设的投标书
  • 一个人做两个博客网站搜索引擎查重
  • 怎样自己做网络推广网站怎么做签到网站
  • 做商城网站建设哪家好wordpress 权限 分类
  • 网站建设宗旨熊猫办公ppt模板下载
  • 泰州建站免费模板国际最新局势最新消息
  • 网站后台管理系统 模板wordpress 按分类显示
  • 网站套餐到期什么意思哪些网站做推广性价比高
  • 开个网站建设公司多少钱接单网app下载安装
  • 网页和网站区别保定网站设计公司排名
  • 自己建网站的详细步骤上海的招聘网站有哪些
  • 设计企业网站微博推广效果怎么样
  • 自己怎么开网站做销售成都设计公司排行
  • 深圳专门做写字楼的网站西宁公司官方网站建设
  • 网站修改关键字网店装修教程免费
  • 四平做网站佳业首页东莞高端网站定制
  • dx网站是哪家公司做的东莞定制网站开发
  • php 手机网站cms系统网站开发人员定罪
  • 网站建设 培训 南充天津企业如何建网站
  • 公司介绍简历模板西安seo学院
  • 北京工程建设交易中心网站免费查询企业电话
  • 哪个公司做视频网站重庆丙图网络科技有限公司
  • 客户制作网站时的问题dw软件免费下载
  • 答题卡在线制作网站动漫制作需要学什么