首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

一个棘手有关问题,大家帮小弟我看看程序

2012-04-10 
一个棘手问题,大家帮我看看程序#include stdio.h main(){FILE*in,*outcharch,infile[10],outfile[10]p

一个棘手问题,大家帮我看看程序
#include "stdio.h "
main()
{   FILE   *in,*out;
    char   ch,infile[10],outfile[10];
    printf( "   infile   name:\n ");
    scanf( "   %s ",infile);
    printf( "outfile   name:\n ");
    scanf( "   %s ",outfile);
    if   ((in=fopen   (infile, "rb "))==NULL)
          {printf( "cant   open   infile\n ");
            exit(0);
          }
    if   ((out=fopen(outfile, "wb "))==NULL)
          {printf( "cant   open   outfile\n ");
            exit(0);
          }
    while(!feof(in))fputc   (fgetc(in),out);
    fclose(in);
    fclose(out);
    }
用上面这个程序,我把一个wav语音文件内容打开,并复制到txt中,但是看到的都是乱码,请问高手,有没有办法把二进制数显示出来,并保存。

[解决办法]
// 我们把每行输出结果分成3部分,本别处理,统一输出
// buf1 编号;buf2 数据; buf3 字符
// 将整理后的信息全部放到str中,供下面的程序使用
void data_format(char *str, unsigned char *buf, int len)
{
int i = 0;
int pos = 0;
char buf1[100] = {0};
char buf2[100] = {0};
char buf3[100] = {0};
char str0[2048] = {0};

while(pos < len)
{
pos++;

// 如果是ASCII字符
if (buf[0] > = 33 && buf[0] <= 126)
{
buf3[i] = buf[0];
}
else
{
buf3[i] = '. ';
}

i++;

sprintf(buf2, "%s%02X ", buf2, (unsigned char)buf[0]);

if(pos%16 == 0)
{
buf3[i] = 0;
i = 0;
sprintf((char *)buf1, "%09Xh: ", pos - 16);
sprintf(str0, "%-13s%-49s%-17s\n ", buf1, buf2, buf3);
strcat(str, str0);
buf2[0] = 0;
buf3[0] = 0;
}

buf++;
}
if (pos%16 != 0)
{
buf3[i] = 0;
sprintf((char *)buf1, "%09Xh: ", pos-pos%16);
sprintf(str0, "%-13s%-49s%-17s\n ", buf1, buf2, buf3);
strcat(str, str0);
}

return;
}

输出的样子如下:
000000000h: 00 12 79 D0 76 AB 00 08 7C ED DD 00 08 00 45 00 ..y.v...|.....E.
000000010h: 00 28 E5 BC 40 00 7F 06 CC 02 0A DC 02 52 0A DC .(..@........R..
000000020h: 32 07 08 8F 00 16 F5 3C 5E AB FF 66 51 3B 50 10 2...... <^..fQ;P.
000000030h: 3A F4 7D A0 00 00 00 00 00 00 00 00 :.}.........

[解决办法]
赞LS的,如果楼主要简单的话
#include "stdio.h "
void main()
{
FILE *in,*out;
char infile[100],outfile[100];
int i = 1;

printf( " infile name:\n ");
scanf( " %s ",infile);
printf( "outfile name:\n ");
scanf( " %s ",outfile);
if ((in=fopen (infile, "rb "))==NULL)
{
printf( "cant open infile\n ");
exit(0);
}
if ((out=fopen(outfile, "w "))==NULL)
{
printf( "cant open outfile\n ");
exit(0);
}
while(!feof(in))
{
fprintf(out, "0x%02x ", fgetc(in));
if ((i++) % 15 == 0)
fprintf(out, "\n ");
}
fclose(in);
fclose(out);
}

热点排行