linux 进度条源代码:
1. 回车:光标倒回到起始位置
换行:光标直接移动到下一行不移到下一行的起始位置
2 缓冲区:
fflush()会强迫将缓冲区内的数据写回参数stream 指定的文件中. 如果参数stream 为NULL,fflush()会将所有打开的文件数据更新
举个例子:
#include<string.h>
#include<stdio.h>
#include<conio.h>
#include<io.h>
void flush(FILE *stream);
int main(void)
{
FILE *stream;
charmsg[]="Thisisatest";
/*createafile*/
stream=fopen("DUMMY.FIL","w");
/*writesomedatatothefile*/
fwrite(charmsg,strlen(charmsg),1,stream);
clrscr();
printf("PressanykeytoflushDUMMY.FIL:");
getch();
/*flushthedatatoDUMMY.FILwithout\
closingit*/
flush(stream);
printf("\nFilewasflushed,Pressanykey\toquit:");
getch();
return0;
}
void flush(FILE* stream)
{
int duphandle;
/*flushthestream'sinternalbuffer*/
fflush(stream);
/*makeaduplicatefilehandle*/
duphandle=dup(fileno(stream));
/*closetheduplicatehandletoflushtheDOSbuffer*/
close(duphandle);
linux 进度条源码
#include<stdio.h>
#include<unistd.h>
int main()
{
int i=0;
char bar[102];
const char *lable="-\\|/";
bar[0]='\0';
while(i<=100)
{
printf("[%-100s][%%%d] [%c]\r",bar,i,lable[i%4]);
bar[i++]='#';
fflush(stdout);
bar[i]='\0';
usleep(8000);
}
printf("\n");
return 0;
}
m标签:bar,stream,进度条,printf,linux,编写,fflush,include,proc From: https://blog.51cto.com/u_12704841/5753054
makefile
proc_bar:proc.c
gcc proc.c -o proc_bar
.PHONY:clean
clean:
rm -f proc_bar