malloc'd char * to String的问题 [英] Problems with a malloc'd char* to String

查看:56
本文介绍了malloc'd char * to String的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hiho,


这里我的新手问题(Win32 GUI):


我正在以二进制模式读取文件到a char *命名缓冲区。我使用了

malloc(filesize)来提供所需的空间。

缓冲区中的filedata似乎没问题。我可以将变量缓冲区写回文件

,内容正常。所以我想直到这里一切都很顺利...


但现在我想把缓冲区内容写入Memo-Box进行显示。

它有效。 ..但只显示前4或5个字母。我不知道

如何将整个缓冲区变量放入MemoBox的字符串

组件中进行显示。

这是部分我的代码:


..

..

..

int fsize = FileSizeByName (文件); // Filesize

char * buffer =(char *)malloc(fsize); //为filedata分配缓冲区

fseek(filereader,SEEK_SET,0);

fread(buffer,fsize,1,filereader); //读取缓冲区中的二进制数据

Memo1-> Text = buffer; // Memo1将缓冲区显示为String

free(缓冲区);

..

..

..


我希望我说明问题是对的,有人知道什么是错的。


提前谢谢!!


Axel

解决方案

> Hiho,


这里我的新手问题(Win32 GUI):

我正在以二进制模式将文件读取到char *命名缓冲区。我使用了malloc(filesize)来提供所需的空间。
中的filedata缓冲区似乎没问题。我可以将变量缓冲区写回文件
并且内容正常。所以我想直到这里一切都很顺利...

但是现在我想将缓冲区内容写入Memo-Box进行显示。
它有效......但只有前4个或者显示5个字母。我不知道如何将整个缓冲区变量放入MemoBox
组件的字符串中进行显示。
以下是我的代码部分:

> int fsize = FileSizeByName(file); file:// Filesize
char * buffer =(char *)malloc(fsize); file://为
filedata fseek(filereader,SEEK_SET,0)分配缓冲区;
fread(buffer,fsize,1,filereader); file://读取缓冲区中的二进制数据
Memo1-> Text = buffer; file:// Memo1将缓冲区显示为String
free(缓冲区);

我希望我说明问题是对的,有人知道什么是



错误。


看到Memo1->文本之后我认为你正在使用带有

VCL控件的Borland C ++ Builder。 Memo控件和FileSizeByName()函数不是C ++标准的一部分。特定于Borland编译器和

VCL库的问题应该转到

newsgroups.borland.com新闻服务器上的一个新闻组,因为该小组仅讨论

标准C ++语言。另请参阅: http://www.slack.net/~shiva/welcome .txt

http: //home.wanadoo.nl/efx/c++-faq/

虽然很难在不知道什么的情况下判断出了什么问题

正在从文件中读取我怀疑你的问题是从文件读取的

字符串不是零终止。下面的(未经测试的)代码

应该解决这个问题:


int fsize = FileSizeByName(file);

char * buffer = new char [fsize + 1];

fseek(filereader,SEEK_SET,0);

fread(buffer,fsize,1,filereader);

buffer [fsize] = 0; //零终止字符串。

Memo1-> Text =缓冲区;

delete []缓冲区;


HTH

-

Peter van Merkerk

peter.van.merkerk(at)dse.nl


Peter van Merkerk写道:


看到Memo1-> Text后我觉得你正在使用带有VCL控件的Borland C ++ Builder 。 Memo控件和FileSizeByName()函数不是C ++标准的一部分。特定于Borland编译器和
VCL库的问题应该转到
newsgroups.borland.com新闻服务器上的一个新闻组,因为该小组仅讨论
标准C ++语言。另请参阅: http://www.slack.net/~shiva/welcome .txt
http:// home。 wanadoo.nl/efx/c++-faq/

虽然很难在不知道究竟是从文件中读取什么的情况下告诉出了什么问题但我怀疑你的问题是从文件读取的
字符串不是零终止。下面的(未经测试的)代码应该解决这个问题:

int fsize = FileSizeByName(file);
char * buffer = new char [fsize + 1];
fseek(filereader,SEEK_SET,0);
fread(buffer,fsize,1,filereader);
buffer [fsize] = 0; //零终止字符串。
Memo1-> Text =缓冲区;
删除[]缓冲区;

HTH
-
Peter van Merkerk
peter.van.merkerk(at)dse.nl






感谢您的想法。请忘记Memo1-> Text..it也可以是一个简单的String-type变量。 Memo1仅用于测试

缓冲区包含的内容。稍后它会是这样的:


String str = buffer;


最后的目标是使用base64编写str。但那不是现在的问题。


但str只包含char *的第​​一个查看字符。您的解决方案

带来相同的结果。 :-(


Axel


>


感谢您的想法。请忘记Memo1-> Text..it也可以是一个简单的String类型变量.Memo1仅用于测试
缓冲区包含的内容。稍后它将是喜欢:

String str = buffer;

最后的目标是使用base64编写str。但这不是问题。

但str只包含char *的第​​一个查看字符。你的解决方案带来了相同的结果。: - (




我在你发布的(不完整的)代码中看不到明显的错误。你是否确认文件中没有0个字节?你是否打开了文件

二进制或文本模式?如果你发布表现出问题的最小但完整的b $ b(== compilable)代码会有所帮助。


-

Peter van Merkerk

peter.van.merkerk(at)dse.nl

Hiho,

here my Newbie question (Win32 GUI):

I''m reading a file in binary mode to a char* named buffer. I used
malloc(filesize) to make the needed space avaiable. The filedata in the
buffer seems to be ok..I can write the variable buffer back to a file
and the contents is ok. So I think until here all goes fine...

But now I want write the buffer contents to a Memo-Box for displaying.
It works...but only the first 4 or 5 letters are shown. I have no idea
how to get the whole buffer-variable into the String of the MemoBox
component for displaying.
Here is the part of my code:

..
..
..
int fsize = FileSizeByName(file); //Filesize
char *buffer = (char*)malloc(fsize); //allocate buffer for filedata
fseek(filereader, SEEK_SET, 0);
fread(buffer,fsize,1,filereader); //read binary data in buffer
Memo1->Text = buffer; //Memo1 displays buffer as String
free(buffer);
..
..
..

I hope I illustrated the problem right and somebody has an idea whats wrong.

Thanks in advance!!

Axel

解决方案

> Hiho,


here my Newbie question (Win32 GUI):

I''m reading a file in binary mode to a char* named buffer. I used
malloc(filesize) to make the needed space avaiable. The filedata in the buffer seems to be ok..I can write the variable buffer back to a file
and the contents is ok. So I think until here all goes fine...

But now I want write the buffer contents to a Memo-Box for displaying.
It works...but only the first 4 or 5 letters are shown. I have no idea
how to get the whole buffer-variable into the String of the MemoBox
component for displaying.
Here is the part of my code:
.
int fsize = FileSizeByName(file); file://Filesize
char *buffer = (char*)malloc(fsize); file://allocate buffer for filedata fseek(filereader, SEEK_SET, 0);
fread(buffer,fsize,1,filereader); file://read binary data in buffer
Memo1->Text = buffer; file://Memo1 displays buffer as String
free(buffer);
.
I hope I illustrated the problem right and somebody has an idea whats


wrong.

After seeing Memo1->Text I think you are using Borland C++ Builder with
VCL controls. The Memo control and the FileSizeByName() function are not
part of the C++ standard. Questions specific to the Borland compiler and
VCL library should go to one of the newsgroups on the
newsgroups.borland.com newsserver, as this group only discusses the
standard C++ language. See also: http://www.slack.net/~shiva/welcome.txt
and http://home.wanadoo.nl/efx/c++-faq/.

Though it is difficult to tell what goes wrong without knowing what
exactly is being read from the file I suspect your problem is that the
string read from the file is not zero terminated. The (untested) code
below should fix that:

int fsize = FileSizeByName(file);
char *buffer = new char[fsize+1];
fseek(filereader, SEEK_SET, 0);
fread(buffer,fsize,1,filereader);
buffer[fsize] = 0; // Zero terminate string.
Memo1->Text = buffer;
delete []buffer;

HTH
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl


Peter van Merkerk wrote:


After seeing Memo1->Text I think you are using Borland C++ Builder with
VCL controls. The Memo control and the FileSizeByName() function are not
part of the C++ standard. Questions specific to the Borland compiler and
VCL library should go to one of the newsgroups on the
newsgroups.borland.com newsserver, as this group only discusses the
standard C++ language. See also: http://www.slack.net/~shiva/welcome.txt
and http://home.wanadoo.nl/efx/c++-faq/.

Though it is difficult to tell what goes wrong without knowing what
exactly is being read from the file I suspect your problem is that the
string read from the file is not zero terminated. The (untested) code
below should fix that:

int fsize = FileSizeByName(file);
char *buffer = new char[fsize+1];
fseek(filereader, SEEK_SET, 0);
fread(buffer,fsize,1,filereader);
buffer[fsize] = 0; // Zero terminate string.
Memo1->Text = buffer;
delete []buffer;

HTH
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl


Hi,

thanks for your ideas. Please forget the Memo1->Text..it could be a
simple String-type variable too. Memo1 is only used for testing what
buffer contains. Later it will be something like:

String str = buffer;

And very later the end-goal is to code str with base64. But thats not
the problem at the moment.

But str only contains the first view chars of char*. Your solution
brings the same result. :-(

Axel


> Hi,


thanks for your ideas. Please forget the Memo1->Text..it could be a
simple String-type variable too. Memo1 is only used for testing what
buffer contains. Later it will be something like:

String str = buffer;

And very later the end-goal is to code str with base64. But thats not
the problem at the moment.

But str only contains the first view chars of char*. Your solution
brings the same result. :-(



I cannot see obvious errors in the (incomplete) code you posted. Are you
sure the file doesn''t have 0 bytes in it? Did you open the file in
binary or text mode? It would help if you post minimal but complete
(==compilable) code that exhibits the problem.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl


这篇关于malloc'd char * to String的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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