1.运用缓存区刷新,实现简易进度条
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
int main(int argc, const char *argv[])
{
char arr[12]={'_','_','-','_','_','-','_','_','-','_','_'};
for(int i=0;i<11;i++)
{
printf("%c",arr[i]);
}
fflush(stdout);
for(int i=0;i<8;i++)
{
sleep(1);
printf("\b\b\b\b\b\b\b\b\b\b\b");
int k=(i/2)*3+i%2;
arr[k]=i+1;
for(int j=0;j<11;j++)
{
if((j+1)%3!=0&&j<=k)
{
printf("%d",arr[j]);
}
else
{
printf("%c",arr[j]);
}
}
fflush(stdout);
}
putchar(10);
sleep(5);
return 0;
}
实现效果
2.实现bmp格式图片的四倍放大
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
int main(int argc, const char *argv[])
{
FILE* fp1=fopen("./pic.bmp","r+");
FILE* fp2=fopen("./pic2.bmp","w");
while(1)
{
char ch[1];
int res=fread(ch,1,1,fp1);
if(res==0){break;}
fwrite(ch,1,1,fp2);
}
int size,width,height;
fseek(fp1,2,SEEK_SET);
fread(&size,4,1,fp1);
printf("图片大小为%d\n",size);
fseek(fp1,18,SEEK_SET);
fread(&width,4,1,fp1);
fread(&height,4,1,fp1);
printf("文件像素为%d*%d\n",width,height);
/* width*=2;height*=2;
fseek(fp2,18,SEEK_SET);
fwrite(&width,4,1,fp2);
fwrite(&height,4,1,fp2);
printf("新文件像素为%d*%d\n",width,height);*/
fseek(fp1,54,SEEK_SET);
fseek(fp2,54,SEEK_SET);
unsigned char rgb[3];
for(int i=0;i<width;i++)
{
for(int k=0;k<2;k++)
{
for(int j=0;j<height;j++)
{
fread(rgb,3,1,fp1);
fwrite(rgb,3,1,fp2);
fwrite(rgb,3,1,fp2);
}
}
}
fclose(fp2);
fclose(fp1);
return 0;
}
实现效果,不理想,半杯水