文件读取,最后某些字节没有被读出来

swift19221 2010-07-21 01:28:28
下面的程序是将二进制文件中的每个字节变成16进制字符串,
比如文件中只有2个字节:0x43 0x50,z 则文本串为:4350
这个程序有个毛病,就是最后不足 READ_BUF_SIZE 的字节没有被读取,
请大家指导一下问题在哪里?


#include <stdio.h>
#include <string.h>
#define READ_BUF_SIZE (100)
void main()
{
FILE *fp, *fp2;
unsigned char c,c1,c2;
char hexTab[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char bufRead[READ_BUF_SIZE + 1] = {0};
char bufWrite[READ_BUF_SIZE*2 + 1] ={0};
int i=0,j=0;
int iReadCnt =0;

fp=fopen("F:\\1.rar","rb");
fp2=fopen("F:\\11.txt","w");

fseek(fp,0,SEEK_END);
printf("\n len=%d\n",ftell(fp));
//getchar();
rewind(fp);
while(!feof(fp))
{
memset(bufRead,0,READ_BUF_SIZE + 1);
iReadCnt = fread(bufRead, 1, READ_BUF_SIZE, fp);
for(i=0;i< iReadCnt;i++)
{
c=bufRead[i];

bufWrite[i*2]=hexTab[c>>4];
bufWrite[i*2+1]=hexTab[c & 0x0f];
//c1=hexTab[c>>4];
//fputc(c1,fp2);

//c1=hexTab[c & 0x0f];
//fputc(c1,fp2);
}
printf("\n readCnt=%d,",iReadCnt);
iReadCnt=fputs(bufWrite,fp2);
printf(" WriteCnt=%d\n\n",iReadCnt);
fputc('\n',fp2);

printf("\n %d:%s\n", j, bufWrite);
j++;

}
fclose(fp);
fclose(fp2);

printf("\ndone,j=%d!\n",j);
}
...全文
116 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
swift19221 2010-07-21
  • 打赏
  • 举报
回复
正解!
这是运行记录:
9:2DAFFECC71547075F3573AF1E7EA545BAB2633EB5
E1A3379C4CDBE5CA0605B509FA10196CCF631BA34E18
F155B59B57D887FE38ABC61EB765F506632FF5E74AF

readCnt=23, WriteCnt=0


10:A48B6DB9412A3A0000BF8867F6A9FFD4C43D7B00
0E1A3379C4CDBE5CA0605B509FA10196CCF631BA34E1
AF155B59B57D887FE38ABC61EB765F506632FF5E74AF

其实都读出来了,每次写入文件后没有清除写入缓存,
所以才造成误解,而且是多谢内容了。

只需要增加“memset(bufWrite,0,READ_BUF_SIZE*2 + 1);
”就行了。
兄台真是细心啊。

[Quote=引用 3 楼 slsnake 的回复:]
C/C++ code

#include <stdio.h>
#include <string.h>
#define READ_BUF_SIZE (100)
void main()
{
FILE *fp, *fp2;
unsigned char c,c1,c2;
char hexTab[]={'0','1','2','3','4','5','6','7',……
[/Quote]
swift19221 2010-07-21
  • 打赏
  • 举报
回复
多谢各位大侠的指导!
赵4老师 2010-07-21
  • 打赏
  • 举报
回复
fread returns the number of full items actually read, which may be less than count if an error occurs or if the end of the file is encountered before reaching count. Use the feof or ferror function to distinguish a read error from an end-of-file condition. If size or count is 0, fread returns 0 and the buffer contents are unchanged.

The fread function reads up to count items of size bytes from the input stream and stores them in buffer. The file pointer associated with stream (if there is one) is increased by the number of bytes actually read. If the given stream is opened in text mode, carriage return–linefeed pairs are replaced with single linefeed characters. The replacement has no effect on the file pointer or the return value. The file-pointer position is indeterminate if an error occurs. The value of a partially read item cannot be determined.

SLSnake 2010-07-21
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <string.h>
#define READ_BUF_SIZE (100)
void main()
{
FILE *fp, *fp2;
unsigned char c,c1,c2;
char hexTab[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char bufRead[READ_BUF_SIZE + 1] = {0};
char bufWrite[READ_BUF_SIZE*2 + 1] ={0};
int i=0,j=0;
int iReadCnt =0;

fp=fopen("1.txt","rb");
fp2=fopen("11.txt","w+");

fseek(fp,0,SEEK_END);
int len = ftell(fp);
printf("\n len=%d\n",len);
//getchar();
rewind(fp);
while(!feof(fp))
{
memset(bufRead,0,READ_BUF_SIZE + 1);
if(len >= READ_BUF_SIZE)
{
iReadCnt = fread(bufRead, 1, READ_BUF_SIZE, fp);
len -= READ_BUF_SIZE;
}
else
{
iReadCnt = fread(bufRead, 1, len, fp);
}
for(i=0;i< iReadCnt;i++)
{
c=bufRead[i];

bufWrite[i*2]=hexTab[c>>4];
bufWrite[i*2+1]=hexTab[c & 0x0f];
//c1=hexTab[c>>4];
//fputc(c1,fp2);

//c1=hexTab[c & 0x0f];
//fputc(c1,fp2);
}
printf("\n readCnt=%d,",iReadCnt);
iReadCnt=fputs(bufWrite,fp2);
printf(" WriteCnt=%d\n\n",iReadCnt);
fputc('\n',fp2);

printf("\n %d:%s\n", j, bufWrite);
memset(bufWrite,0,READ_BUF_SIZE*2 + 1);
j++;

}
fclose(fp);
fclose(fp2);

printf("\ndone,j=%d!\n",j);
}

改成这样就行了
SLSnake 2010-07-21
  • 打赏
  • 举报
回复
vc6测试结果
文件被全部读出了,只是后面不足READ_BUF_SIZE长度的内容在读取得时候
长度不足的地方还是上次读取的值,所以看起来还像没读完.
你要把读出的长度和文件长度比较的,最后一次读多长是算出来的
太乙 2010-07-21
  • 打赏
  • 举报
回复
/* feof example: byte counter */
#include <stdio.h>
int main ()
{
FILE * pFile;
long n = 0;
pFile = fopen ("myfile.txt","rb");
if (pFile==NULL) perror ("Error opening file");
else
{
while (!feof(pFile)) {
fgetc (pFile);
n++;
}
fclose (pFile);
printf ("Total number of bytes: %d\n", n-1);
}
return 0;
}

69,370

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧