char * block [5000]和二进制读取的问题 [英] problem with char* block[5000] and binary reading

查看:82
本文介绍了char * block [5000]和二进制读取的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我从二进制文件读取时遇到问题。我正在谷歌搜索和

搜索,但我只是无法理解,为什么这个代码不起作用。我可以

使用任何帮助。这里是源代码:


- 在这里切割 -


typedef struct pkg_ {

short int info;

char * data [5000];

} pkg;


TFileStream *文件;


void SendFile(char * FName,int status)

{

File = new TFileStream(FName,fmOpenRead);


while(File-> Position< File-> Size)

{

char * block [5000];

pkg * newPkg = new pkg;

File-> Read(block,5000);

memcpy(*(newPkg-> data),*(块),5000); //这一行

newPkg-> info = 0;

Form1-> SS1-> Socket-> Connections [0] - > SendBuf(( void *)newPkg,sizeof(newPkg));

删除newPkg;

}

pkg * newPkg = new pkg;

newPkg-> info = status;

Form1-> SS1-> Socket-> Connections [0] - > SendBuf((void *)newPkg,sizeof(newPkg ));

删除newPkg;

删除文件;

返回;

}


- 在这里切割 -


i正在尝试

memcpy(*(newPkg-> data),*(block), 5000); //这一行


并获得访问冲突消息,以及

memcpy(newPkg-> data,block,5000); //这一行

但在这种情况下是newPkg-> data似乎是一个简单的指针,而不是一个数组指针的b $ b,如block。在调试期间,显示以下信息

显示这些变量的内容:

newPkg-> data:= 0090568C

block = {: 57200A0D,:6D756C6F等等(二进制的实际内容

文件)}

juest在指令memcpy(newPkg-> data,block, 5000); //这一行" ;.

请帮帮我。

hello everyone!
i have a problem with reading from binary file. i was googling and
searching, but i just cant understand, why isnt this code working. i could
use any help. here''s the source code:

--cut here--

typedef struct pkg_ {
short int info;
char* data[5000];
} pkg;

TFileStream* File;

void SendFile(char* FName, int status)
{
File=new TFileStream(FName,fmOpenRead);

while(File->Position<File->Size)
{
char *block[5000];
pkg* newPkg=new pkg;
File->Read(block,5000);
memcpy(*(newPkg->data),*(block),5000); //this line
newPkg->info=0;
Form1->SS1->Socket->Connections[0]->SendBuf((void*)newPkg,sizeof(newPkg));
delete newPkg;
}
pkg* newPkg=new pkg;
newPkg->info=status;
Form1->SS1->Socket->Connections[0]->SendBuf((void*)newPkg,sizeof(newPkg));
delete newPkg;
delete File;
return;
}

--cut here--

i was trying
memcpy(*(newPkg->data),*(block),5000); //this line

and getting "access violation" message, as well as
memcpy(newPkg->data,block,5000); //this line
but in this case "newPkg->data" appears to be a simple pointer, not an array
of pointers, as "block". And during debugging the following information
about contents of these variables are shown:
newPkg->data:=0090568C
block={:57200A0D, :6D756C6F, etc. etc. (the actual content of a binary
file)}
juest after instruction "memcpy(newPkg->data,block,5000); //this line".
Please help me.

推荐答案

ishmael4写道:
ishmael4 wrote:
大家好!
我从二进制文件中读取有问题。我正在谷歌搜索和搜索,但我只是无法理解,为什么这个代码不起作用。我可以使用任何帮助。这里是源代码:

- 在这里 -

typedef struct pkg_ {
short int info;
char * data [ 5000];


你意识到这是一个5000个指针到char的数组,而不是5000个字节,

对吧?

} pkg ;

TFileStream *文件;

void SendFile(char * FName,int status)
{/> File = new TFileStream(FName,fmOpenRead);

while(文件 - >位置<文件 - >大小)
{
char * block [5000];


这里也是一样。

pkg * newPkg = new pkg;
File-> Read(block,5000);
的memcpy(*(newPkg->数据),*(块),5000); //这一行


newPkg只分配了5000个指针,没有数据。你取消引用

指针肯定是一个错误。取消引用块也可能是一个

错误,具体取决于TFileStream :: Read()中发生的情况。

newPkg-> info = 0;
Form1-> ; SS1-> Socket-> Connections [0] - > SendBuf((void *)newPkg,sizeof(newPkg));
删除newPkg;
}
pkg * newPkg = new pkg;
newPkg-> info = status;
Form1-> SS1-> Socket-> Connections [0] - > SendBuf((void *)newPkg,sizeof(newPkg) );
删除newPkg;
删除文件;
返回;
}


您可能要考虑使用像std这样的智能指针:: auto_ptr

而不是所有的新闻和删除。它为您提供了异常的安全性,而且没有性能成本,这意味着您需要记住的更少。

- 在这里切割 -
hello everyone!
i have a problem with reading from binary file. i was googling and
searching, but i just cant understand, why isnt this code working. i could
use any help. here''s the source code:

--cut here--

typedef struct pkg_ {
short int info;
char* data[5000];
You realize this is an array of 5000 pointers-to-char, not 5000 bytes,
right?
} pkg;

TFileStream* File;

void SendFile(char* FName, int status)
{
File=new TFileStream(FName,fmOpenRead);

while(File->Position<File->Size)
{
char *block[5000];
Same thing here.
pkg* newPkg=new pkg;
File->Read(block,5000);
memcpy(*(newPkg->data),*(block),5000); //this line
newPkg has only 5000 pointers allocated, no data. Your dereferencing of
the pointer is certainly an error. Dereferencing block may also be an
error depending on what happens in TFileStream::Read().
newPkg->info=0;
Form1->SS1->Socket->Connections[0]->SendBuf((void*)newPkg,sizeof(newPkg));
delete newPkg;
}
pkg* newPkg=new pkg;
newPkg->info=status;
Form1->SS1->Socket->Connections[0]->SendBuf((void*)newPkg,sizeof(newPkg));
delete newPkg;
delete File;
return;
}
You may want to consider using a smart pointer like std::auto_ptr
instead of all your news and deletes. It gives you exception safety at
no performance cost, and it means less for you to remember.
--cut here--



[snip]


完成。


干杯! --M


[snip]

Done.

Cheers! --M


mlimber写道:
mlimber wrote:
ishmael4写道:
ishmael4 wrote:



[snip ]


[snip]

pkg * newPkg = new pkg;
File-> Read(block,5000);
memcpy(*(newPkg-> ;数据),*(块),5000); //这一行
pkg* newPkg=new pkg;
File->Read(block,5000);
memcpy(*(newPkg->data),*(block),5000); //this line



newPkg只分配了5000个指针,没有数据。你取消引用指针肯定是一个错误。取消引用块也可能是一个
错误,具体取决于TFileStream :: Read()中发生的事情。



newPkg has only 5000 pointers allocated, no data. Your dereferencing of
the pointer is certainly an error. Dereferencing block may also be an
error depending on what happens in TFileStream::Read().



[snip]


请允许我澄清我的发言。 *(newPkg->数据)与

newPkg-> data [0]相同,但此位置不是char而是指向char的指针

并且该指针显然未初始化。或者你想要有一个

阵列的字符,或者你需要为每个指定的空间分配一些空间来指向,例如,


pkg :: pkg()

{

for(int i = 0; i< 5000; ++ i)

data [i] = new char [30];

}


pkg :: ~pkg()

{

for(int i = 0; i< 5000; ++ i)

删除数据[i];

}


干杯! --M


[snip]

Allow me to clarify my statement. *(newPkg->data) is the same as
newPkg->data[0], but this location is not a char but a pointer-to-char
and that pointer is clearly uninitialized. Either you meant to have an
array of chars or you need to allocate some space for each of those
5000 pointers to point to, e.g.,

pkg::pkg()
{
for( int i=0; i < 5000; ++i )
data[i] = new char[ 30 ];
}

pkg::~pkg()
{
for( int i=0; i < 5000; ++i )
delete data[i];
}

Cheers! --M




mlimber写道:

[snip]

mlimber wrote:
[snip]
pkg ::〜pkg()
{
for(int i = 0; i< 5000; ++ i)
删除数据[i];
}
pkg::~pkg()
{
for( int i=0; i < 5000; ++i )
delete data[i];
}




应该是:


删除[]数据[i];


干杯! --M



That should be:

delete [] data[i];

Cheers! --M


这篇关于char * block [5000]和二进制读取的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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