c++怎么使用文件 *** 作保存图片

c++怎么使用文件 *** 作保存图片,第1张

C/C++ *** 作的图片一般都是位图,即bmp文件,其他文件都是通过各种压缩算法进行有损压缩或者无损压缩得到的(如jpg)。

bmp文件格式百度可以搜索到,按照它的格式读取就行了,写入文件也按照位图格式写入文件,并将文件保存为bmp格式的就能打开了

#include <stdio.h>

#include <stdlib.h>

#include <windows.h>

int file_size(char* filename)//获取文件名为filename的文件大小。

{

FILE *fp = fopen(filename, "rb")//打开文件。

int size

if(fp == NULL) // 打开文件失败

return -1

fseek(fp, 0, SEEK_END)//定位文件指针到文件尾。

size=ftell(fp)//获取文件指针偏移量,即文件大小。

fclose(fp)//关闭文件。

return size

}

int main ()

{

int size=0

size=file_size("qw")

printf("%d\n",size)

FILE * pFile,*qw

char *buffer=(char*)malloc(sizeof(char)*size)

qw   =fopen("qw","r")

pFile = fopen ( "qwe" , "wb" )

printf("%d==\n",pFile)

printf("%d\n",size)

fread(buffer,1,size,qw)

fwrite (buffer , sizeof(byte), size , pFile )

fclose (pFile)

rename("qwe","Groot.jpg")

return 0

}

扩展资料:

c语言读取TXT文件:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_LINE 1024

int main()

{

char buf[MAX_LINE]  /*缓冲区*/

FILE *fp            /*文件指针*/

int len             /*行字符个数*/

if((fp = fopen("test.txt","r")) == NULL)

{

perror("fail to read")

exit (1)

}

while(fgets(buf,MAX_LINE,fp) != NULL)

{

len = strlen(buf)

buf[len-1] = '\0'  /*去掉换行符*/

printf("%s %d \n",buf,len - 1)

}

return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11815468.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存