fread只给出了前4个字节? [英] fread gives only the first 4 bytes ??

查看:137
本文介绍了fread只给出了前4个字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hiho,


我想将二进制文件的内容读入字符串。它可以工作,但

只有前4个字符在读取后留下,而字符串大小是,

当然是4个字节。

我从教程中得到这个方法,虽然它有效......但是有什么问题?

String''FileContent'的buffersize不正常吗?在fread之前,''FileContent''的'/ b $ b'大小是正确的,但是在操作之后它被截断了

到4.


Codepart

--------------------------------------


int fsize;

CString FileContent;

LPTSTR pStr;

FILE * fl = fopen(ff ,rb;;

if(!fl)

{

//错误信息

}

其他

{

fseek(fl,0,SEEK_END);

fsize = ftell(fl);

pStr = FileContent.GetBufferSetLength((int)fsize + 1);

fseek(fl,0,SEEK_SET);

c_edit1.SetWindowText (itoa(fsize,buf,100));

fsize = fread(pStr,1,fsize,fl);

*(pStr + fsize)='' \\ 0'';

FileContent.ReleaseBuffer(-1);

}

fclose(fl);


--------------------------------------

感谢您的帮助!


Axel

解决案




Axel写道:


Hiho,

我想看看二进制文件的内容为字符串。它有效,但是只有前4个字符在读取后留下,而字符串大小是,当然是4个字节。




什么那是二进制数据吗?

可能是里面有二进制0字节吗?


-

Karl Heinz Buchegger
kb******@gascad.at


Karl Heinz Buchegger写道:


Axel写道:

Hiho,

我想将二进制文件的内容读入字符串。它有效,但是只有前4个字符在读取后留下,而字符串大小是,当然是4个字节。



什么在那个二进制数据?
它可能是一个二进制0字节吗?




无论我使用什么文件都没关系..我总是得到一个4字节的结果。但是

这里是一个示例文件(仅限第一个字符):


II *? ? t? ? ?? ?一个? ??? ? ó??? ? ? ??? ? ? ??? ?


看起来好笑......这是另一个文件:


???à?JFIF ?? d d?ì?Ducky? ? P ?? ?Adobedà??? ? ????????????????????? b b b b b b b b b b b b .. .. .. .. .. .. .. .. .. .. ..



Axel




Axel写道:


Karl Heinz Buchegger写道:


Axel写道:

Hiho,

我想阅读的内容将二进制文件转换为字符串。它有效,但是只有前4个字符在读取后留下,而字符串大小是,当然是4个字节。



什么在那个二进制数据?
它可能是一个二进制0字节吗?



我用的是什么文件并不重要..我总是得到一个4字节的结果。但是
这里是一个示例文件(仅限第一个字符):

II *? ? t? ? ?? ?一个? ??? ? ó??? ? ? ??? ? ? ??? ?

看起来很有趣......这是另一个文件:

????JFIF ?? d d?ì?Ducky? ? P ?? ?Adobedà??? ? ?????????????


也许二进制0字节是问题..我不知道。




给自己一个十六进制编辑器(或写一个:-)

使用二进制文件时,这个工具绝对是*必须*。


-

Karl Heinz Buchegger
kb ****** @ gascad.at


Hiho,

I want to read the content of a binary file into a string. It works, but
only the first 4 chars are left after the reading and the stringsize is,
of course, 4 bytes.
I got this method from a tutorial and though it works...what''s wrong? Is
the buffersize for the String ''FileContent'' not ok? Before fread the
size of ''FileContent'' is correct, but after the operation its truncated
to 4.

The Codepart
--------------------------------------

int fsize;
CString FileContent;
LPTSTR pStr;
FILE *fl = fopen(ff,"rb");
if(!fl)
{
//Error Message
}
else
{
fseek(fl, 0, SEEK_END);
fsize = ftell(fl);
pStr = FileContent.GetBufferSetLength((int)fsize + 1);
fseek(fl, 0, SEEK_SET);
c_edit1.SetWindowText(itoa(fsize, buf, 100));
fsize = fread(pStr, 1, fsize, fl);
*(pStr + fsize) = ''\0'';
FileContent.ReleaseBuffer(-1);
}
fclose(fl);

--------------------------------------

Thanks for your help!

Axel

解决方案



Axel wrote:


Hiho,

I want to read the content of a binary file into a string. It works, but
only the first 4 chars are left after the reading and the stringsize is,
of course, 4 bytes.



What''s in that binary data?
Could it be that there is a binary 0 byte in it?

--
Karl Heinz Buchegger
kb******@gascad.at


Karl Heinz Buchegger wrote:


Axel wrote:

Hiho,

I want to read the content of a binary file into a string. It works, but
only the first 4 chars are left after the reading and the stringsize is,
of course, 4 bytes.


What''s in that binary data?
Could it be that there is a binary 0 byte in it?



It does not matter what file i use..I always get a 4 byte result. But
here is an example file (the first chars only):

II*   t     á   ó   ?     

Looks funny...here is another file:

???à JFIF  d d ?ì Ducky   P ?? Adobe dà ?? ? 

Maybe the binary 0 byte is the problem..i dont know.

Axel




Axel wrote:


Karl Heinz Buchegger wrote:


Axel wrote:

Hiho,

I want to read the content of a binary file into a string. It works, but
only the first 4 chars are left after the reading and the stringsize is,
of course, 4 bytes.


What''s in that binary data?
Could it be that there is a binary 0 byte in it?



It does not matter what file i use..I always get a 4 byte result. But
here is an example file (the first chars only):

II*   t     á   ó   ?     

Looks funny...here is another file:

???à JFIF  d d ?ì Ducky   P ?? Adobe dà ?? ? 

Maybe the binary 0 byte is the problem..i dont know.



Get yourself a hex editor (or write one :-)
This tools is an absolute *must* when working with binary files.

--
Karl Heinz Buchegger
kb******@gascad.at


这篇关于fread只给出了前4个字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆