C/C++编程:读取文本&图片转为二进制流 您所在的位置:网站首页 二进制的数据转换成图片格式 C/C++编程:读取文本&图片转为二进制流

C/C++编程:读取文本&图片转为二进制流

2024-07-17 19:37| 来源: 网络整理| 查看: 265

文章目录 c:打印char*c:读取文本C++:读取文本内容C++: boost写文件c:图片转为二进制流并写入txtc++:图片与char* / char[]的转换C++: 图片与智能指针

c:打印char* void printCharPoint(char *c){ while (*c != '\0'){ printf("%d", *(c++)); } printf("\n"); } c:读取文本 #include #include // c/c++中文会乱码 int main(void) { FILE* fp = fopen("C:\\Users\\Administrator\\Documents\\GitHub\\smart\\c_hik_ren\\read_me.txt", "r"); if(!fp) { perror("File opening failed"); return EXIT_FAILURE; } int c; // 注意:int,非char,要求处理EOF while ((c = fgetc(fp)) != EOF) { // 标准C I/O读取文件循环 putchar(c); } if (ferror(fp)) puts("I/O error when reading"); else if (feof(fp)) puts("End of file reached successfully"); fclose(fp); } C++:读取文本内容 #include #include std::string getFileContent(const std::string& path) { std::ifstream file(path); std::string content((std::istreambuf_iterator(file)), std::istreambuf_iterator()); return content; } int main() { std::cout FILE *fp; // //以二进制方式打开图像 fopen_s(&fp,"C:\\Users\\Administrator\\Pictures\\test.jpg", "rb"); if(fp == NULL) { perror("File opening failed"); return EXIT_FAILURE; } fseek(fp, 0, SEEK_END); long int size = ftell(fp); rewind(fp); printf("get the size is: %ld\n", size); //根据图像数据长度分配内存buffer char* ImgBuffer=(char*)malloc( size* sizeof(char) ); //将图像数据读入buffer fread(ImgBuffer, size, 1, fp); fclose(fp); //以二进制写入方式 if ( (fp=fopen("C:\\Users\\Administrator\\Pictures\\a.txt", "wb"))==NULL) { perror("txtxFile opening failed"); exit(0); } //从buffer中写数据到fp指向的文件中 fwrite(ImgBuffer, size, 1, fp); printf("ok"); fclose(fp); free(ImgBuffer); } #include #include #include #include #define IMAGE_PATH "../" #define SAVE_IMG ".jpeg" #define SAVE_TXT ".txt" char* genRandomString(int length) { int flag, i; char* string; srand((unsigned) time(NULL )); if ((string = (char*) malloc(length)) == NULL ) { printf("Malloc failed!flag:14\n"); return NULL ; } for (i = 0; i case 0: string[i] = 'A' + rand() % 26; break; case 1: string[i] = 'a' + rand() % 26; break; case 2: string[i] = '0' + rand() % 10; break; default: string[i] = 'x'; break; } } string[length - 1] = '\0'; return string; } void saveImg(char * pre, char* ImgBuffer, int size, char *save_format){ // 生成名字 char buff[1024]=IMAGE_PATH; //第一个字符串 char *s=genRandomString(10); //第二个字符串 strcat(buff, pre); strcat(buff,s); //拼接专两个字符串,结果保存在第属一个字符串当中 strcat(buff,save_format); //以二进制写入方式 FILE *fp; if ( (fp=fopen(buff, "wb+"))==NULL) { perror("image fopen fall"); exit(0); } //从buffer中写数据到fp指向的文件中 fwrite(ImgBuffer, size, 1, fp); fclose(fp); } int main(void) { FILE *fp; // //以二进制方式打开图像 fopen_s(&fp,"C:\\Users\\Administrator\\Pictures\\qtblog1.png", "rb"); if(fp == NULL) { perror("File opening failed"); return EXIT_FAILURE; } fseek(fp, 0, SEEK_END); long int size = ftell(fp); rewind(fp); printf("get the size is: %ld\n", size); //根据图像数据长度分配内存buffer char* ImgBuffer=(char*)malloc( size* sizeof(char) ); //将图像数据读入buffer fread(ImgBuffer, size, 1, fp); fclose(fp); saveImg("ren", ImgBuffer, size, SAVE_IMG); free(ImgBuffer); } int main() { FILE *fp; // //以二进制方式打开图像 fopen_s(&fp,"C:\\Users\\Administrator\\Pictures\\test.jpg", "rb"); if(fp == NULL) { perror("File opening failed"); return EXIT_FAILURE; } char pBuffer[8]; FILE * wp = fopen("C:\\Users\\Administrator\\Pictures\\2.jpg", "wb"); while (!feof(fp)){ fread(pBuffer, 1, 3, fp); //f2 = fopen("2.jpg", "wb"); fwrite(pBuffer, 1, 3, wp); // 每次读n个字节属 } fclose(fp); fclose(wp); printf("Hello, World!\n"); return 0; } c++:图片与char* / char[]的转换 /******************************************************************************* Description: 打开系统设备文件函数 Input: IN char *pcPath 打开本地文件路径 INOUT unsigned int *pulBuflen 获取本地待写入文件大小 INOUT char **ppcBuffer 读取本地文件buffer Output: 无 Return: 获取成功返回TRUE,失败返回FALSE Caution: -------------------------------------------------------------------------------- Modification History DATE NAME DESCRIPTION -------------------------------------------------------------------------------- YYYY-MM-DD *******************************************************************************/ bool CLI_GetPicInfo(IN char *pcPath, INOUT unsigned int *pulBuflen, INOUT char **ppcBuffer) { struct stat statbuf; unsigned int ulBuflen = 0; char *pcBuffer = NULL; int lReadlen = 0; int fd = 0; if (stat(pcPath, &statbuf) return false; } pcBuffer = (char *)malloc(ulBuflen); if (NULL == pcBuffer) { return false; } memset(pcBuffer, 0, ulBuflen); /* 打开本地文件 */ fd = open(pcPath, O_RDONLY); if (fd free(pcBuffer); pcBuffer = NULL; close(fd); return false; } close(fd); *pulBuflen = ulBuflen; *ppcBuffer = pcBuffer; return true; } int main(){ unsigned int ulBuflen = 0; /* 单张图片长度*/ char *pcImgBuf = NULL; /* 单张图片buffer */ if (!CLI_GetPicInfo("/home/a.jpg", &ulBuflen, &pcImgBuf)) { printf("Failed to get pic info %s.\n", stRtDirInfo.szName); return -1; } free(pcImgBuf); pcImgBuf = NULL; } #include // ifstream, ifstream::in #include #define MAX 1024*1024 using namespace std; // 将内存中的二进制文件写成一张图片 bool write_image(const string& filename, char *pBuffer, int length){ ofstream fout(filename, ios::binary); if(!fout){ printf("can't open = %s\n", filename.c_str()); return false; } fout.write(pBuffer,length); fout.close(); return true; } bool write_image2(const string& filename, char *image_buf, int image_len){ int fd = open(filename.c_str(), O_CREAT | O_WRONLY | O_TRUNC); if (fd > 0) { int n = 0, m = 0; do { m = write(fd, image_buf + n, image_len - n); n += m; } while (n printf("can't open = %s\n", filename.c_str()); return false; } // 求图片长度 fin.seekg(0, std::ifstream::end); //将文件流指针定位到流的末尾 int length = fin.tellg(); fin.seekg(0, std::ifstream::beg); //将文件流指针重新定位到流的开始 if(length MAX){ printf("length = %d\n", length); return false; } // 读取 *out_length = length; fin.read(pBuffer, length); fin.close(); return true; } int main(){ // 读图片 char * pBuffer = new char[MAX]; int length = 0; bool succ = read_image( R"(D:\workspace\binarytree\test.jpg)", pBuffer, &length); if(succ){ write_image(R"(D:\workspace\binarytree\save.jpg)", pBuffer, length); write_image(R"(D:\workspace\binarytree\save.png)", pBuffer, length); write_image(R"(D:\workspace\binarytree\save.txt)", pBuffer, length); write_image(R"(D:\workspace\binarytree\save.bmp)", pBuffer, length); write_image(R"(D:\workspace\binarytree\save.raw)", pBuffer, length); } delete [] pBuffer; return 0; } int main11(){ // 读图片 char pBuffer[MAX] = {0}; int length = 0; bool succ = read_image( R"(D:\workspace\binarytree\test.jpg)", pBuffer, &length); if(succ){ write_image(R"(D:\workspace\binarytree\save.jpg)", pBuffer, length); write_image(R"(D:\workspace\binarytree\save.png)", pBuffer, length); write_image(R"(D:\workspace\binarytree\save.txt)", pBuffer, length); write_image(R"(D:\workspace\binarytree\save.bmp)", pBuffer, length); write_image(R"(D:\workspace\binarytree\save.raw)", pBuffer, length); } return 0; }

在创建文件流时,可以显示指定它的打开方式为ios::binary,也就是以二进制方式打开。但是,无论是否指定二进制方式打开文件,读写的最小单位都是字节。那么,它到底起到什么作用呢?

首先,介绍一下二进制方式打开与普通打开方式的区别,两者大的区别在于对换行符的处理方式不同。由于历史原因,Windows操作系统是用两个字符(\r\n)来表示换行符的;而Unix操作系统却是用单个字符(\n)来表示换行符的。因此,在创建文件流时,如果指定了以ios::binary方式打开,那么换行符就是单字符的;否则,就采用Windows操作系统的双字符。

总结来说,以ios::binary方式打开,换行符被解释成\n;反之,换行符被解释成\r\n。

所以为了兼容性,通常在Windous系统中通常使用ios::binary方式打开图像文件;在Unix(或类Unix)操作系统中,指定和不指定ios::binary方式没有区别。

C++: 图片与智能指针 #include // ifstream, ifstream::in #include #include #include #include #define MAX 1024*1024*3 using namespace std; // 将内存中的二进制文件写成一张图片 bool write_image(const string& filename, const std::unique_ptr &pBuffer, int length){ ofstream fout(filename, ios::binary); if(!fout){ printf("can't open = %s\n", filename.c_str()); return false; } fout.write(pBuffer.get(),length); fout.close(); return true; } bool read_image(const string& filename, const std::unique_ptr & pBuffer, int *out_length){ ifstream fin(filename, ifstream::in | ios::binary); if(!fin){ printf("can't open = %s\n", filename.c_str()); return false; } // 求图片长度 fin.seekg(0, std::ifstream::end); //将文件流指针定位到流的末尾 int length = fin.tellg(); fin.seekg(0, std::ifstream::beg); //将文件流指针重新定位到流的开始 if(length MAX){ printf("length = %d\n", length); return false; } // 读取 *out_length = length; fin.read(pBuffer.get(), length); fin.close(); return true; } int main(){ // 读图片 std::unique_ptr pBuffer(new char[MAX]); int length = 0; bool succ = read_image( R"(/home/oceanstar/图片/1.jpg)", pBuffer, &length); if(succ){ write_image(R"(/home/oceanstar/CLionProjects/myffmget/save.jpg)", pBuffer, length); write_image(R"(/home/oceanstar/CLionProjects/myffmget/save.png)", pBuffer, length); write_image(R"(/home/oceanstar/CLionProjects/myffmget/save.txt)", pBuffer, length); write_image(R"(/home/oceanstar/CLionProjects/myffmget/save.bmp)", pBuffer, length); write_image(R"(/home/oceanstar/CLionProjects/myffmget/save.raw)", pBuffer, length); } return 0; }

等待研究 https://www.cnblogs.com/lyj-blogs/p/10178384.html



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有