fopen和fclose
错误码
fgetc和fputc
fputs
fgets
缓冲区
#include
#include
#include
int main(int argc, const char *argv[])
{
//--------------------------------------验证行缓存-------------------------
//这种情况不会刷新缓存区
// printf("hello word");
// while(1);
//1.遇到换行符时会刷新缓存区
// printf("hello word\n");
// while(1);
//2.关闭文件指针时会刷新缓存区
// printf("helllo word");
// fclose(stdout);
// while(1);
//3.程序结束后也会刷新缓存区
// printf("hello word");
//4.输入输出切换时也会刷新缓存区
// int num;
// scanf("%d",&num);
// printf("%d\n",num);
// while(1);
//5.使用fflush函数刷新缓存区
// printf("hello word");
// fflush(stdout);
// while(1);
//6.缓存区已满
// for ( int i = 0; i < 1025; i++ )
// {
// printf("A");
// }
// while(1);
//---------------------------------------验证全缓存--------------------------
FILE*fp;//定义一个文件指针
//以只写的形式打开一个文件
if ((fp = fopen("04test.txt","w")) == NULL)
{
perror("fopen error");
return -1;
}
//1.关闭文件指针时会刷新缓存区
// fputs("helllo word\n",fp);
// fclose(fp);
// while(1);
//2.程序结束后也会刷新缓存区
// fputs("helllo word\n",fp);
//3.使用fflush函数刷新缓存区
// fputs("helllo word\n",fp);
// fflush(fp);
// while(1);
// 4.输入输出切换时也会刷新缓存区
// char s[128];
// fputs("hello word\n",fp);
// fgets(s,sizeof(s),fp);
// printf("%s\n",s);
// while(1);
//5.当全缓存满时也会刷新缓存区
for(int i = 0; i < 5000; i++)
{
fputc('A',fp);
}
while(1);
return 0;
}
时间函数和sprintf
snprintf
fscanf和fprintf
fread和fwrite
1.读写字符串
读写整形变量
读取结构体信息
feof
关于光标的函数
#include
#include
typedef struct{
unsigned int img_size; //图片的大小
unsigned int img_width;//图片的宽
unsigned int img_high; //图片的高
unsigned short img_bitcount; //一个像素点占用的bit(24bit)
}image_info_t;
typedef struct{
unsigned char b;
unsigned char g;
unsigned char r;
}point_t;
void show_image_info(image_info_t *info)
{
printf("size = %d,width = %d,high = %d,bitcount = %d\n",
info->img_size,info->img_width,info->img_high,info->img_bitcount);
}
void get_image_info(FILE*fp,image_info_t *info)
{
fseek(fp,2,SEEK_SET);
fread(&info->img_size,1,4,fp); //图片的大小
fseek(fp,18,SEEK_SET);
fread(&info->img_width,1,4,fp); //图片的宽
fread(&info->img_high,1,4,fp); //读取图片的高
fseek(fp,2,SEEK_CUR);
fread(&info->img_bitcount,1,2,fp); //像素点占用的bit
}
void copy_image_file(FILE*sfp,FILE*dfp)
{
int ret;
char buf[1024] = {0};
while(!(feof(sfp)||ferror(sfp))){
ret = fread(buf,1,sizeof(buf),sfp);
fwrite(buf,1,ret,dfp);
}
return;
}
void set_image_mosaic(FILE *fp,image_info_t *info,int x,int y)
{
int i,j,k,w;
point_t color = {0,0,0xff};
char *buffer = (char *)malloc((info->img_width)*(info->img_high)*3);
//point_t arr[612][440];
//1.将图像读取回来
fseek(fp,54,SEEK_SET);
fread(buffer,1,(info->img_size-54),fp);
//2.修改
//i:整体的高/10
//j:整体的宽除以10
//k:块的高
//w:块的宽
for(i=0;iimg_high/y;i++){
for(j=0;jimg_width/x;j++){
//读取小方块中最左上角的像素点
color = *(point_t *)(buffer+(j*3*x)+(i*y*info->img_width*3));
for(k=0;kimg_width*3)+
(j*3*x)+(i*y*info->img_width*3)) = color;
}
}
}
}
//3.重新将图像写回去
fseek(fp,54,SEEK_SET);
fwrite(buffer,1,(info->img_size-54),fp);
}
int main(int argc, char const *argv[])
{
FILE *sfp,*dfp;
int size;
image_info_t info;
char new_name[20] = {0};
if(argc != 2){
fprintf(stderr,"input error,try again\n");
fprintf(stderr,"usage:./a.out xxxx.bmp\n");
return -1;
}
//1.打开文件并拷贝文件 milaoshu.bmp
if((sfp = fopen(argv[1],"r"))==NULL)
PRINT_ERR("open error");
//构造一个新图片的字符串 new_milaoshu.bmp
snprintf(new_name,sizeof(new_name),"new_%s",argv[1]);
//打开新图片,如果不存在就创建,如果存在就清空
if((dfp = fopen(new_name,"w+"))==NULL)
PRINT_ERR("open error");
//图片的拷贝,将milaoshu.bmp-->new_milaoshu.bmp
copy_image_file(sfp,dfp);
//2.获取图片前54个字节中有用的信息
get_image_info(dfp,&info);
show_image_info(&info);
//3.尝试打马赛克
//10,10:代表的是打马赛克每个小方块的大小
//10*3= 30
//10 = 10行
set_image_mosaic(dfp,&info,10,10);
//4.关闭源文件和目标文件
fclose(sfp);
fclose(dfp);
return 0;
}