0
点赞
收藏
分享

微信扫一扫

HTTP和HTTPS详解

Separes 2023-11-11 阅读 69

文章目录

1.文件缓冲区

1.1介绍

1.2缓冲文件系统

系统自动地在内存为程序中每一个正在使用的文件开辟一块文件缓冲区。

  • 从内存向磁盘输出数据,先送到内存中的缓冲区,缓冲区装满后一起输送到磁盘上。
  • 从磁盘向计算机读入数据,从磁盘文件中读取数据输入到内存缓冲区(充满缓冲区),从缓冲区逐个地将数据送到程序数据区(程序变量等)。
  • 缓冲区的大小根据C编译系统决定。

1.3冲刷函数fflush

int fflush( FILE *stream );
将缓冲区内数据写到stream 指定文件。
成功返回 0 
错误返回 EOF 
高版本vs无法使用
fclose关闭文件时也会刷新缓冲区

#include <stdio.h>
#include <windows.h>
int main() 
{
	FILE* pf = fopen("test7.txt", "w");
	if (!pf)
	{
		perror(fopen);
		return 1;
	}
	fputs("abcdef", pf); //数据输出到buffer
	                     //此时文件中无内容
	Sleep(10000);        //睡眠10s
	fflush(pf);          //刷新缓冲区
	Sleep(10000);        //睡眠10s
	                     //此时数据到达文件
	fclose(pf);
	pf = NULL;
    return 0;
}

1.4认识linux下的缓冲区

在这里插入图片描述

在这里插入图片描述
在linux下gcc编译这两段代码时 结果是不同的 左边先输出后睡眠 右边先睡眠后输出

确实是的 实际上printf先于sleep执行但是sleep执行完后这个程序才结束 才会输出信息

c语言存在输出缓冲区(一段内存空间) 显示器设备一般的刷新策略是行刷新 即碰到\n就把\n之前的所有的字符显示出来 所以左边代码先显示 后边代码存入到缓冲区 直到程序结束才显示

在这里插入图片描述

在这里插入图片描述

#include <stdio.h>
#include<unistd.h>
int main()
{
	printf("hello linux!");
	fflush(stdout);
	sleep(3);
	return 0
}

2.linux小程序的实现

在这里插入图片描述

在这里插入图片描述

#include <stdio.h>
#include<unistd.h>
int main()
{
	int count = 5;
	while(count)
	{
		printf("count是: %d\n",count);
		count--;
		sleep(1);
		return 0;
	}
}

可以正常输出

#include <stdio.h>
#include<unistd.h>
int main()
{
	int count = 5;
	while(count)
	{
		printf("count是: %d\r",count);
		count--;
		sleep(1);
		return 0;
	}
}

不输出任何内容

2.2倒计时程序

#include <stdio.h>
#include<unistd.h>
int main()
{
	int count = 5;
	while(count)
	{
		printf("count是: %d\r",count);
		fflush(stdout);
		count--;
		sleep(1);
		return 0;
	}
}

每执行一次printf 将要输出的信息输出到缓冲区 当执行fflush函数时 将信息从缓冲区刷到显示器 之后\r回车 光标回到行首 count–

2.3进度条小程序

sleep/usleep

在这里插入图片描述

在这里插入图片描述

代码

在这里插入图片描述

运行结果

linu-vim-c-bar

举报

相关推荐

HTTP/HTTPS详解

详解HTTP/HTTPS协议

HTTP、HTTPS协议详解

HTTP/HTTPS协议详解

HTTP 和 HTTPS

http和https

0 条评论