1. 回车和换行的差异
在输出文本时,回车和换行符的作用是非常不同的。了解它们的行为有助于我们控制输出的方式。
-
回车(
\r
):回车符将光标移到当前行的开头,但并不会自动换行。它的作用是覆盖当前行的内容。 -
换行(
\n
):换行符不仅会换行,还会将缓冲区中的内容刷新到屏幕上。
这意味着,当你使用\n
时,程序会将当前缓冲区的内容刷新到屏幕上。而当你使用\r
时,光标会回到行首,但缓冲区的内容并不会立即刷新,直到程序结束(return 0;)或者手动刷新。
2、观察回车换行现象
1.执行下面代码
#include<stdio.h>
#include<unistd.h>
int main()
{
printf("hello linux\n");
sleep(3);
return 0;
}
现象:打印完字符串,然后休眠3秒,最后结束程序。
2.执行下面代码
#include<stdio.h>
#include<unistd.h>
int main()
{
printf("hello linux\r");
sleep(3);
return 0;
}
现象:
休眠3秒,程序就结束了。
为什么使用\r就不能字符串了呢???
这会牵扯到缓冲区的知识,我们在写一个缓冲区的点来详细讲解!!!
补充查询手册:
输入命令:
mam 3 sleep # 三号手册才能查到我们想用的sleep
3. 缓冲区的工作原理
在C语言中,输出通常是通过缓冲区实现的。这意味着程序并不会立刻将所有输出内容显示到屏幕,而是将它们保存在内存中,直到达到特定的条件才会刷新到屏幕上。
-
行缓冲:标准输出流(如
stdout
)是行缓冲的。也就是说,只有在输出遇到换行符(\n
)或者缓冲区满时,内容才会被刷新。 -
手动刷新:通过调用
fflush(stdout)
,你可以手动强制刷新缓冲区,立即将内容显示到屏幕上。
这个机制在进行动态输出时特别重要。我们需要了解缓冲区的特性,才能精确控制程序的输出。
4、usleep和fflush函数
我们可以通过man手册先查询两个函数的基本用法。
1. usleep函数
功能:
以微秒为间隔暂停执行,头文件为#include<unistd.h>
2.fflush函数
功能:
重刷一个流。
stdout -- 标准输出流 -- 屏幕
stdin -- 标准输入流 -- 键盘
stderr -- 标准错误流 -- 屏幕
5、简单倒计时
通过上面的简单重刷输出流,我们可以做一个简单的倒计时程序。
要求:实现一个从10到0的倒计时效果。
#include <stdio.h>
#include <unistd.h>
int main() {
int cnt = 10;
while (cnt >= 0) {
printf("%-2d\r", cnt); // 以2位宽度打印并左对齐
fflush(stdout); // 手动刷新缓冲区
sleep(1); // 暂停1秒
cnt--;
}
printf("\n"); // 最后换行,避免被shell覆盖
return 0;
}
6、进度条
6.1、版本一
此处我们安装C语言中的分文件实现此进度条,加上我们Linux中学习的makefile工具。
首先我们创建四个文件:processbar.c processbar.h main.c makefile
processbar.c : 进度条函数的实现。
processbar.h : 进度条函数的声明,头文件包含。
main.c : 调用.h文件中的方法 。
makefile : 自动化编译。
进度条效果:
- 第一个中括号就是表示进度条。
- 第二个中括号表示进度。
- 第三个表示旋转样式。
makefile文件代码:
processbar:processbar.c main.c
gcc -o $@ $^
.PHONY:clean
clean:
rm -f processbar
processbar.h代码:
#includ<stdio.h>
#include<unistd.h>
#include<string.h>
void ProcBar();
processbar.c代码
#include "processbar.h"
//字符串长度
#define Length 101
#define Style '#' //表示进度条的符号
const char* label="|/-\\";//两个\\表示一个\,表示旋转样式
//version1
void ProcBar()
{
char bar[Length];//缓冲区长度
memset(bar,'\0',sizeof(bar));//将缓存区空间都改为\0
int len=strlen(label);//字符串长度
//循环往缓存区输入#
int cnt=0;
while(cnt<=100)
{
printf("[%-100s][%3d%%][%c]\r",bar,cnt,label[cnt%len]);
fflush(stdout);
bar[cnt++]=Style;
usleep(30000);//休眠30000微秒,秒太长了
}
printf("\n");
}
main.c代码:
#include "processbar.h"
int main()
{
ProcBar();
return 0;
}
6.2、版本二
在我们的实际生活中,进度条一般不会单独出现,常出现在下载界面和游戏界面,因此版本二通过下载场景来进行展示。
download:download.c test.c
gcc -o $@ $^
.PHONY:clean
clean:
rm -f download
download.h代码:
#pragma once
#include <stdio.h>
#include <string.h>
#include <unistd.h>
typedef void(*callback_t)(double, double);//函数指针
//void ProcBar();
void ProcBar(double total, double current);
download.c代码:
#include"download.h"
#define Length 101
#define Style '='
const char *lable = "|/-\\";
//version 2
void ProcBar(double total, double current)
{
char bar[Length];
memset(bar, '\0', sizeof(bar));
int len = strlen(lable);
int cnt = 0;
double rate = (current*100.0)/total;
int loop_count = (int)rate;
while(cnt <= loop_count)
{
bar[cnt++] = Style;
//usleep(20000);
}
printf("[%-100s][%.1lf%%][%c]\r", bar, rate, lable[cnt%len]);
fflush(stdout);
}
test.c代码:
#include"download.h"
double bandwidth = 1024*1024*1.0;
//download
void download(double filesize,callback_t cb)
{
double current = 0.0;
printf("download begin, current: %lf\n", current);
while(current <= filesize)
{
cb(filesize, current);
//从网络中获取数据
usleep(100000);
current += bandwidth;
}
printf("\ndownload done, filesize: %lf\n",filesize);
}
int main()
{
download(100*1024*1024,ProcBar);
download(2*1024*1024,ProcBar);
return 0;
}