我想把一个exe 用UltraEDIT打开时看到的十六进制数读出来写到一个txt中(以文本的格式写,如写成:4F 3A 5B 6C FF DD),我用CFile,确总是读错(或是写错),没几行代码,那位仁兄受累给写一下,先谢谢了!

解决方案 »

  1.   

    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);
    试试成不成,这样是最简单的写法,只是说明一下算法,有两点坏处
    如果可执行文件很大有可能无法分配那么大的内存
    每个字节写一次文件效率不是很高,建议开辟两个缓冲区每次处理一定量的字节数
      

  2.   

    sorry 
    临时写的,丢了一句
    file.Read(pbuf,len);
      

  3.   

    比较安全的写法用WIN API 
    CreateFile/ReadFile/WriteFile函数
      

  4.   

    #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 );
    }
      

  5.   

    上面的代码可以,能否用CFile,MFC写一个,谢了!
      

  6.   

    to:jennyvenus(JennyVenus) 
    这段代码也不行,只能读对前几个值;
      

  7.   

    我也有过一样的需要,所以就自己写过一个,不过是用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;
    }
      

  8.   

    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);
    //不行么等我帮你挑试看看
      

  9.   

    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个字节好象没问题,不过可执行文件比较大就要循环搞定了
      

  10.   

    to: akiy(宏),这段代码跟jennyvenus(JennyVenus)的问题一样,读出的 字符跟用U_edit读出的一比较,就知道,前几个值是正确的后面的值不对。
    谢谢各位,大家再想一下!
      

  11.   

    to:sjdf() ,你的方法确实可以,谢谢!读出的结果正确,大家试试用mfc 改一下!
      

  12.   

    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的文件都没有问题
      

  13.   

    补充一下,
    fileTxt.WriteLine(strLine);
    这一行写错了,应该是
    fileTxt.WriteString(strLine);