急问关于BYTE *、char *的转化问题,谢谢!

sct02 2004-04-07 11:30:40
我程序里的一段代码,

BYTE *pbBuffer;
DWORD dwCount;
DWORD dwBlockLen = 某一定值;

if(pbBuffer = (BYTE *)malloc(dwBufferLen))
{
printf("Memory has been allocated for the buffer. \n");
}
else
{
MyHandleError("Out of memory. \n");
}

dwCount = fread(pbBuffer,1,dwBlockLen,hSource);
//hSource指向源文件

printf("See what have benn read1...\n\n");
getch();
ShowBytes(pbBuffer,dwCount);
printf("Press...\n\n");
getch();

printf("See what have benn read2...\n\n");
cout.write((const char *)pbBuffer,strlen((const char *)pbBuffer));
printf("Press...\n\n");
getch();

// Define the ShowBytes function.
void ShowBytes(BYTE *s, DWORD len)
{
DWORD TotalChars = 0;
DWORD ThisLine = 0;
while(TotalChars < len)
{
if(ThisLine > 70)
{
ThisLine = 0;
printf("\n");
}

printf("%c",s[TotalChars]);

TotalChars++;
ThisLine++;
}
printf("\n");
} // End of ShowBytes.
------------------------------------
运行结果:
See what have benn read1...

ip地址:210.43.110.88
255.255.255.0
210.43.111.253
202.197.96.1
2
02.197.96.5
202.197.96.11
202.197.96.13
proxy.hnu.cn

Press...

See what have benn read2...

ip地址:210.43.110.88
255.255.255.0
210.43.111.253
202.197.96.1
202.197.96.5
202.197.96.11
202.197.96.13
proxy.hnu.cn
屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯
屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯
屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯
屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯
屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯屯Press...

---------------------------------
ShowBytes函数正确显示了源文件的内容,但为什么转成const char *之后显示
出来多了那些“屯屯屯屯......”呢?
急问,谢谢!


...全文
34 点赞 收藏 14
写回复
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
shines77 2004-04-07
这句写错了, 改一下:
pbBuffer[dwCount] = '\0'; //字符串结束标记

另外当fread返回的字节数少于要读取的字节数时就有错误发生了,
可以用feof 或者 ferror函数来获得错误,所以上面还是改为这样

if(dwCount <= 0) // 等于0还是允许的
{
//.....
}
回复
shines77 2004-04-07
你查查dwCount的值, 很可能dwCount读取的字节为0, 实在不行就这样写吧,
注意, 已经包括上面的全部, 这样写安全也规范些.

BYTE *pbBuffer = NULL;
DWORD dwCount = 0;
DWORD dwBlockLen = 某一定值;

if(pbBuffer = (BYTE *)malloc(dwBufferLen+1))
{
printf("Memory has been allocated for the buffer. \n");
}
else
{
MyHandleError("Out of memory. \n");
return;
}

memset(pbBuffer, 0, dwBufferLen) //先初始化

dwCount = fread(pbBuffer,1,dwBlockLen,hSource);
//hSource指向源文件
if(dwCount < 0) // 等于0还是允许的
{
MyHandleError("read file failed. \n");
return;
}

pbBuffer[dwCount] = '/0'; //字符串结束标记

printf("See what have benn read1...\n\n");
getch();
ShowBytes(pbBuffer,dwCount);
printf("Press...\n\n");
getch();

printf("See what have benn read2...\n\n");
cout.write((const char *)pbBuffer,strlen((const char *)pbBuffer));
printf("Press...\n\n");
getch();
回复
wltsui 2004-04-07
你还是自己在debug模式下认真调试,查看每个变量的变法,应该能找到问题的根源。
回复
sct02 2004-04-07
先memset(pbBuffer, 0, dwBufferLen)
再dwCount = fread(pbBuffer,1,dwBlockLen, hSource);
结果也是:

......
See what have benn read2...

Press...
怎么会这样呢?晕啊

回复
sct02 2004-04-07
ymbymb(毛病大哥)
pbBuffer[dwCount] = 0; //OK
加了这句之后:
See what have benn read1...

ip地址:210.43.110.88
255.255.255.0
210.43.111.253
202.197.96.1
202.197.96.5
202.197.96.11
202.197.96.13
proxy.hnu.cn

Press...

See what have benn read2...

Press...
什么也没有啊,为什么?
回复
sct02 2004-04-07
puhuofeie(扑火飞蛾) 是指这样做吗?
cout.write((const char *)pbBuffer,strlen((const char *)pbBuffer)+1);
这样做还是不行啊
回复
ymbymb 2004-04-07
dwCount = fread(pbBuffer,1,dwBlockLen,hSource);
//加上一句
pbBuffer[dwCount] = 0; //OK
回复
lsaturn 2004-04-07
你最好memset(pbBuffer, 0, dwBufferLen)先
debug版本里面是用cc来填充未初始化的字段的
回复
酒红绿叶 2004-04-07
转成Char的时候,多申请一个字节!

用来装/我记得好像char型的字符串使用这个结尾的,
不然末尾就会出现乱码!!

试试,看看行不行!!
回复
sct02 2004-04-07
请各位帮帮忙!
回复
sct02 2004-04-07
OK ,谢谢啦!结帖
回复
sct02 2004-04-07
大家看看
回复
shines77 2004-04-07
为什么会分行, 你自己看看ShowBytes()就知道了, 70个字符就分行
真怀疑代码是不是你自己写的????

多个0个问题是你把
pbBuffer[dwCount] = '/0'; //字符串结束标记

改为
pbBuffer[dwCount] = '\0'; //字符串结束标记
或者
pbBuffer[dwCount] = 0; //字符串结束标记


// Define the ShowBytes function.
void ShowBytes(BYTE *s, DWORD len)
{
DWORD TotalChars = 0;
DWORD ThisLine = 0;
while(TotalChars < len)
{
if(ThisLine > 70)
{
ThisLine = 0;
printf("\n");
}

printf("%c",s[TotalChars]);

TotalChars++;
ThisLine++;
}
printf("\n");
} // End of ShowBytes.
回复
sct02 2004-04-07
把这段代码写成程序:
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <windows.h>
#include <wincrypt.h>

void ShowBytes(BYTE *s, DWORD len);

void main(void)
{
FILE *hSource;
BYTE *pbBuffer = NULL;
DWORD dwCount = 0;
DWORD dwBlockLen = 1000;
DWORD dwBufferLen = 1200;

if(hSource = fopen("ip.txt","rb"))
{
printf("The source file,%s,is open. \n","ip.txt");
}
else
{
printf("\nError opening source file!");
getch();
return;
}

// Allocate memory.
if(pbBuffer = (BYTE *)malloc(dwBufferLen+1))
{
printf("Memory has been allocated for the buffer. \n");
}
else
{
printf("Out of memory. \n");
getch();
return;
}

memset(pbBuffer,0,dwBufferLen); //先初始化

dwCount = fread(pbBuffer,1,dwBlockLen,hSource);
if(dwCount < 0) // 等于0还是允许的
{
printf("read file failed. \n");
getch();
return;
}

pbBuffer[dwCount] = '/0'; //字符串结束标记
printf("See what have benn read1...\n\n");
getch();
ShowBytes(pbBuffer,dwCount);
printf("Press...\n\n");
getch();

printf("See what have benn read2...\n\n");
cout.write((const char *)pbBuffer,strlen((const char *)pbBuffer));
cout<<"\n12345.....";

}

// Define the ShowBytes function.
void ShowBytes(BYTE *s, DWORD len)
{
DWORD TotalChars = 0;
DWORD ThisLine = 0;
while(TotalChars < len)
{
if(ThisLine > 70)
{
ThisLine = 0;
printf("\n");
}

printf("%c",s[TotalChars]);

TotalChars++;
ThisLine++;
}
printf("\n");
} // End of ShowBytes.
---------------------------------------------
文件ip.txt的内容:
ip地址
210.43.110.88
255.255.255.0
210.43.111.253
202.197.96.1
202.197.96.5
202.197.96.11
202.197.96.13
proxy.hnu.cn
------------------------------------------
运行结果:
The source file,ip.txt,is open.
Memory has been allocated for the buffer.
See what have benn read1...

ip地址
210.43.110.88
255.255.255.0
210.43.111.253
202.197.96.1
202
.197.96.5
202.197.96.11
202.197.96.13
proxy.hnu.cn
Press...

See what have benn read2...

ip地址
210.43.110.88
255.255.255.0
210.43.111.253
202.197.96.1
202.197.96.5
202.197.96.11
202.197.96.13
proxy.hnu.cn0
12345.....
F:\毕业设计\try\try192\Debug>
------------------------------------
读出来的根源文件都有些不一样:
read1...:
202
.197.96.5

read2...:
proxy.hnu.cn0

为什么read1的那里会分行,read2后面多了一个0呢?
怎么才能读出根源文件一样的呢?


回复
发动态
发帖子
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
社区公告
暂无公告