rt,用c++如何读取二进制文件到一个string类型中,
急求,急求
2条回答 默认 最新
- 枫舞的季节 2017-06-03 04:14关注
如下代码,先在string 中分配大小,然后读取,如果要显示字符串中的内容,需要将内存中的内容转换为16进制或者其他的显示方式。
#include <stdio.h> #include <string> using namespace std; int main(int argc , char** argv) { string s; FILE *fp = fopen("c:\\test\\xx.exe","rb"); if(fp) { fseek(fp,0,SEEK_END); int len = ftell(fp); fseek(fp,0,SEEK_SET); s.resize(len); fread((void*)s.data(),1,len,fp); fclose(fp); } else { printf("fopen error\n"); } return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用