读exe到记事本????????? 我想把一个exe 用UltraEDIT打开时看到的十六进制数读出来写到一个txt中(以文本的格式写,如写成:4F 3A 5B 6C FF DD),我用CFile,确总是读错(或是写错),没几行代码,那位仁兄受累给写一下,先谢谢了! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 CFile file;file.Open(strfilename,CFile::modeRead|CFile::typeBinary,NULL);int len=file.GetLength();char *pbuf=(char*)malloc(len+1024);char *p=pbuf;while(p-pbuf<len){ char temp[20]; itoa(temp,*p,16); file.Write(temp,2); p++;}file.Close()free(pbuf);试试成不成,这样是最简单的写法,只是说明一下算法,有两点坏处如果可执行文件很大有可能无法分配那么大的内存每个字节写一次文件效率不是很高,建议开辟两个缓冲区每次处理一定量的字节数 sorry 临时写的,丢了一句file.Read(pbuf,len); 比较安全的写法用WIN API CreateFile/ReadFile/WriteFile函数 #include "stdio.h"FILE *fp;FILE *fp1;char buf;char buf1[ 4 ];if( ( fp = fopen( "a.exe", "rb" ) ) == NULL ) return -1;else{ if( ( fp1 = fopen( "a.txt", "wb" ) ) == NULL ) { fclose( fp ); return -2; } else { fread( &buf, 1, 1, fp ); sprintf( buf1, " %.2x", buf ); fwrite( buf1, 3, 1, fp1 ); do { fread( &buf, 1, 1, fp ); sprintf( buf1, " %.2x", buf ); fwrite( buf1, 3, 1, fp1 ); }while( !feof( fp ) ); } fclose( fp ); fclose( fp1 );} 上面的代码可以,能否用CFile,MFC写一个,谢了! to:jennyvenus(JennyVenus) 这段代码也不行,只能读对前几个值; 我也有过一样的需要,所以就自己写过一个,不过是用c写的,你看看。#include "stdafx.h"#include "stdio.h"#include "stdlib.h"#include "conio.h"int main(int argc, char* argv[]){ FILE *fp1,*fp2; unsigned char date; char txt[4]={0}; char name1[80]; char name2[80]; unsigned long int count=0; printf("请输入源文件名:"); gets(name1); if ((fp1=fopen(name1,"rb"))==NULL) { printf("\n失败!不能打开源文件!按任意键退出……"); getch(); exit(1); } printf("\n请输入目标文件名:"); gets(name2); if ((fp2=fopen(name2,"w"))==NULL) { printf("\n失败!不能打开目标文件!按任意键退出……"); fclose(fp1); getch(); exit(1); } fread(&date,1,1,fp1); while(!feof(fp1)) { count++; itoa(int(date),txt,16); fputs("0x",fp2); fputs(txt,fp2); putc(',',fp2); if(!(count%10)) fputs("\\\n",fp2); fread(&date,1,1,fp1); } fclose(fp1); fclose(fp2); printf("\n转换完毕,按任意键退出……"); getch(); return 0;} CFile file;CFile fileout;fileout.Open(strOutfilename,CFile::Create|CFile::modeRead,NULL);file.Open(strfilename,CFile::modeRead|CFile::typeBinary,NULL);int len=file.GetLength();char *pbuf=(char*)malloc(len+1024);char *p=pbuf;while(p-pbuf<len){ char temp[20]; itoa(temp,*p,16); fileout.Write(temp,2); p++;}file.Close();fileout.Close();free(pbuf);//不行么等我帮你挑试看看 CFile file; CFile fileout; fileout.Open("c:\\test.txt",CFile::modeCreate|CFile::modeWrite,NULL); file.Open("c:\\GDDemo.exe",CFile::modeRead|CFile::typeBinary,NULL); int len=file.GetLength(); //buf=(char *)malloc(len+1024); char buf[1024]; char buf2[1024]; file.Read(buf,256); for(int i=0;i<256;i++) { itoa(buf[i],buf2,16); fileout.Write(buf2,strlen(buf2)); } file.Close(); fileout.Close();我用这个方法读写了256个字节好象没问题,不过可执行文件比较大就要循环搞定了 to: akiy(宏),这段代码跟jennyvenus(JennyVenus)的问题一样,读出的 字符跟用U_edit读出的一比较,就知道,前几个值是正确的后面的值不对。谢谢各位,大家再想一下! to:sjdf() ,你的方法确实可以,谢谢!读出的结果正确,大家试试用mfc 改一下! CFile fileExe(_T("c:\\Thumbnail.exe"), CFile::modeRead | CFile::typeBinary);CStdioFile fileTxt(_T("C:\\Test.txt"), CFile::modeWrite | CFile::modeCreate);UINT nBytesRead;BYTE buf[256];nBytesRead = fileExe.Read(buf, 256);while (nBytesRead != 0){ CString strLine; for (int i = 0; i < nBytesRead; i++) { CString strTemp; strTemp.Format(_T("%02x "), buf[i]); strLine += strTemp; if (i % 16 == 15) { strLine += _T("\r\n"); fileTxt.WriteLine(strLine); strLine = _T(""); } } nBytesRead = fileExe.Read(buf, 256);}fileExe.Close();fileTxt.Flush();fileTxt.Close();我用来读了一个5k的文件都没有问题 补充一下,fileTxt.WriteLine(strLine);这一行写错了,应该是fileTxt.WriteString(strLine); 如何删除多文档里的空白无标题窗口? 最近在做一个传输程序,不是很稳定?现把代码贴上来,希望高手可以给出一些指正!谢谢! 请问CString,char*,char var[],三者之间如何转化 如何用WinInet实现http文件下载? 这几句代码有何用(菜鸟问) ActiveX技术交流 adCmdStoredProc 我怎么可以知道一个线程是否已经结束???????? Vs2008碉堡了。没有进程都能返回进程句柄?! MFC 通过FTP上传文件时,怎样设置上传的具体路径 如何循环执行命令?在线...... 如何编程对USB摄像头进行视频采集?
file.Open(strfilename,CFile::modeRead|CFile::typeBinary,NULL);
int len=file.GetLength();
char *pbuf=(char*)malloc(len+1024);
char *p=pbuf;
while(p-pbuf<len)
{
char temp[20];
itoa(temp,*p,16);
file.Write(temp,2);
p++;
}
file.Close()
free(pbuf);
试试成不成,这样是最简单的写法,只是说明一下算法,有两点坏处
如果可执行文件很大有可能无法分配那么大的内存
每个字节写一次文件效率不是很高,建议开辟两个缓冲区每次处理一定量的字节数
临时写的,丢了一句
file.Read(pbuf,len);
CreateFile/ReadFile/WriteFile函数
FILE *fp1;
char buf;
char buf1[ 4 ];
if( ( fp = fopen( "a.exe", "rb" ) ) == NULL )
return -1;
else
{
if( ( fp1 = fopen( "a.txt", "wb" ) ) == NULL )
{
fclose( fp );
return -2;
}
else
{
fread( &buf, 1, 1, fp );
sprintf( buf1, " %.2x", buf );
fwrite( buf1, 3, 1, fp1 );
do
{
fread( &buf, 1, 1, fp );
sprintf( buf1, " %.2x", buf );
fwrite( buf1, 3, 1, fp1 );
}while( !feof( fp ) );
}
fclose( fp );
fclose( fp1 );
}
这段代码也不行,只能读对前几个值;
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"int main(int argc, char* argv[])
{
FILE *fp1,*fp2;
unsigned char date;
char txt[4]={0};
char name1[80];
char name2[80];
unsigned long int count=0;
printf("请输入源文件名:");
gets(name1);
if ((fp1=fopen(name1,"rb"))==NULL)
{
printf("\n失败!不能打开源文件!按任意键退出……");
getch();
exit(1);
} printf("\n请输入目标文件名:");
gets(name2);
if ((fp2=fopen(name2,"w"))==NULL)
{
printf("\n失败!不能打开目标文件!按任意键退出……");
fclose(fp1);
getch();
exit(1);
}
fread(&date,1,1,fp1);
while(!feof(fp1))
{
count++;
itoa(int(date),txt,16);
fputs("0x",fp2);
fputs(txt,fp2);
putc(',',fp2);
if(!(count%10))
fputs("\\\n",fp2);
fread(&date,1,1,fp1);
}
fclose(fp1);
fclose(fp2);
printf("\n转换完毕,按任意键退出……");
getch();
return 0;
}
CFile fileout;
fileout.Open(strOutfilename,CFile::Create|CFile::modeRead,NULL);
file.Open(strfilename,CFile::modeRead|CFile::typeBinary,NULL);
int len=file.GetLength();
char *pbuf=(char*)malloc(len+1024);
char *p=pbuf;
while(p-pbuf<len)
{
char temp[20];
itoa(temp,*p,16);
fileout.Write(temp,2);
p++;
}
file.Close();
fileout.Close();
free(pbuf);
//不行么等我帮你挑试看看
CFile fileout;
fileout.Open("c:\\test.txt",CFile::modeCreate|CFile::modeWrite,NULL); file.Open("c:\\GDDemo.exe",CFile::modeRead|CFile::typeBinary,NULL);
int len=file.GetLength();
//buf=(char *)malloc(len+1024);
char buf[1024];
char buf2[1024]; file.Read(buf,256);
for(int i=0;i<256;i++)
{
itoa(buf[i],buf2,16);
fileout.Write(buf2,strlen(buf2));
}
file.Close();
fileout.Close();
我用这个方法读写了256个字节好象没问题,不过可执行文件比较大就要循环搞定了
谢谢各位,大家再想一下!
CStdioFile fileTxt(_T("C:\\Test.txt"), CFile::modeWrite | CFile::modeCreate);UINT nBytesRead;
BYTE buf[256];nBytesRead = fileExe.Read(buf, 256);
while (nBytesRead != 0)
{
CString strLine;
for (int i = 0; i < nBytesRead; i++)
{
CString strTemp;
strTemp.Format(_T("%02x "), buf[i]);
strLine += strTemp;
if (i % 16 == 15)
{
strLine += _T("\r\n");
fileTxt.WriteLine(strLine);
strLine = _T("");
}
} nBytesRead = fileExe.Read(buf, 256);
}fileExe.Close();
fileTxt.Flush();
fileTxt.Close();我用来读了一个5k的文件都没有问题
fileTxt.WriteLine(strLine);
这一行写错了,应该是
fileTxt.WriteString(strLine);