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(FNa me,fmOpenRead);
while(File->Position<Fil e->Size)
{
char *block[5000];
pkg* newPkg=new pkg;
File->Read(block,500 0);
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,500 0); //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:=009056 8C
block={:57200A0 D, :6D756C6F, etc. etc. (the actual content of a binary
file)}
juest after instruction "memcpy(new Pkg->data,block,500 0); //this line".
Please help me. 20 3038
ishmael4 wrote: 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(FNa me,fmOpenRead);
while(File->Position<Fil e->Size) { char *block[5000];
Same thing here.
pkg* newPkg=new pkg; File->Read(block,500 0); 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::Re ad().
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]
Done.
Cheers! --M
mlimber wrote: ishmael4 wrote:
[snip] pkg* newPkg=new pkg; File->Read(block,500 0); 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::Re ad().
[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 wrote:
[snip] pkg::~pkg() { for( int i=0; i < 5000; ++i ) delete data[i]; }
That should be:
delete [] data[i];
Cheers! --M
ishmael4 wrote: 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];
This creates an array of 5000 char pointers, not a buffer of 5000 chars.
Try: char data[5000];
--
λz.λi.i(i((λ n.λm.λz.λi.n z(λq.mqi))((λ n.λz.λi.n(nzi )i)(λz.λi.i(( (λn.λz.λi.n
(nzi)i)(λz.λi .i(iz)))zi)))(( λn.λz.λi.n(n zi)i)(λz.λi.i (iz)))zi))
ishmael4 wrote: 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(FNa me,fmOpenRead);
while(File->Position<Fil e->Size) { char *block[5000]; pkg* newPkg=new pkg; File->Read(block,500 0); 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,500 0); //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:=009056 8C block={:57200A0 D, :6D756C6F, etc. etc. (the actual content of a binary file)} juest after instruction "memcpy(new Pkg->data,block,500 0); //this line". Please help me.
Almost certainly you want
char block[5000];
not
char *block[5000];
and
typedef struct pkg_ {
short int info;
char data[5000];
} pkg;
not
typedef struct pkg_ {
short int info;
char* data[5000];
} pkg;
john
> Almost certainly you want char block[5000];
not
char *block[5000];
and
typedef struct pkg_ { short int info; char data[5000]; } pkg;
not
typedef struct pkg_ { short int info; char* data[5000]; } pkg;
john
fixed sourcecode:
--cut here--
typedef struct pkg_ {
short int info;
char data[5000];
} pkg;
void SendFile(char* FName, int status)
{
File=new TFileStream(FNa me,fmOpenRead);
while(File->Position<Fil e->Size)
{
pkg* newPkg=new pkg;
File->Read(newPkg->data,5000);
newPkg->info=0;
Form1->SS1->Socket->Connections[0]->SendBuf((void* )newPkg,sizeof( pkg));
delete newPkg;
}
pkg* newPkg=new pkg;
newPkg->info=status;
Form1->SS1->Socket->Connections[0]->SendBuf((void* )newPkg,sizeof( pkg));
delete newPkg;
delete File;
return;
}
--cut here--
this one works correctly with txt files, but not with binary. For some
reason,
File->Read(newPkg->data,5000); reads data from binary only when data is
char* data[5000]; (but not inside the structure[?!?]). Here's info about
File->Read():
--begin here--
virtual int __fastcall Read(void *Buffer, int Count);
Description
Use Read to read data from the resource associated with the handle stream
when the number of bytes in the file is not known. Read transfers up to
Count bytes from the resource, starting at the current position, and then
advances the current position in the resource by the number of bytes
actually transferred. Read returns the number of bytes actually transferred,
which may be less than Count if the end of file marker is encountered.
All other data-reading methods of a handle stream (ReadBuffer,
ReadComponent) call Read to do the actual reading.
--end here--
I've read a few times, but i can't figure out, why Buffer has to be char*
buffer[count], and for some reason it has to be outside the structure (see
the previous code). But maybe my english isnt well enough to understand the
description of this function. Summing up, i need to load a block of binary
data and it works only with char* bufer[5000] that is outside the structure.
i cant copy it to similar variable inside the structure. char buffer[5000]
works with textfiles (debug info: newPkg->data:="blahbla hblah"), but doesnt
with binary. sorry if it seems a bit messed up, but i am working on this
project since a week 10 hours a day and i grow depressed about it. thanks in
advance for any help.
ishmael4
ishmael4 wrote: fixed sourcecode:
--cut here--
typedef struct pkg_ { short int info; char data[5000]; } pkg;
This typedef method is C-style and unnecessary in C++. Just write:
struct pkg {
short int info;
char data[5000];
} pkg;
void SendFile(char* FName, int status) { File=new TFileStream(FNa me,fmOpenRead);
Why is File a file-scope variable if you always new and delete it here.
Why not just create an object on the stack like this:
TFileStream File( FName, fmOpenRead );
[snip] this one works correctly with txt files, but not with binary. For some reason, File->Read(newPkg->data,5000); reads data from binary only when data is char* data[5000]; (but not inside the structure[?!?]). Here's info about File->Read():
--begin here--
virtual int __fastcall Read(void *Buffer, int Count);
Description
Use Read to read data from the resource associated with the handle stream when the number of bytes in the file is not known. Read transfers up to Count bytes from the resource, starting at the current position, and then advances the current position in the resource by the number of bytes actually transferred. Read returns the number of bytes actually transferred, which may be less than Count if the end of file marker is encountered.
All other data-reading methods of a handle stream (ReadBuffer, ReadComponent) call Read to do the actual reading.
--end here--
I've read a few times, but i can't figure out, why Buffer has to be char* buffer[count], and for some reason it has to be outside the structure (see the previous code). But maybe my english isnt well enough to understand the description of this function. Summing up, i need to load a block of binary data and it works only with char* bufer[5000] that is outside the structure. i cant copy it to similar variable inside the structure. char buffer[5000] works with textfiles (debug info: newPkg->data:="blahbla hblah"), but doesnt with binary. sorry if it seems a bit messed up, but i am working on this project since a week 10 hours a day and i grow depressed about it. thanks in advance for any help.
You might need to pass a flag into the TFileStream constructor to tell
it to operate in binary mode, but this class isn't standard C++, is
unfamiliar to me, and is, strictly speaking, off-topic in this forum.
Could you dump that TFileStream and use the standard <fstream> instead.
It would be much easier for us to help you get it to work in binary or
text mode.
Cheers! --M
mlimber wrote: This typedef method is C-style and unnecessary in C++. Just write:
struct pkg { short int info; char data[5000]; } pkg;
Correction: delete the "pkg" after the "}".
"ishmael4" <is******@wp.pl > wrote in message
news:dg******** **@atlantis.new s.tpi.pl... typedef struct pkg_ { short int info; char data[5000];
Don't use magical constants define the buffer size somewhere before the
definition of pkg. For example:
size_t const bufferSize = 5000;
Use only bufferSize in the code.
} pkg;
Also, you don't need the 'typedef struct' idiom in C++. Just do
struct pkg { /* ... */};
void SendFile(char* FName, int status) { File=new TFileStream(FNa me,fmOpenRead);
while(File->Position<Fil e->Size)
Himmm... You are not incrementing File->Position by one in the loop. Your
test above is not safe. There is a danger of attempting to read more than
there is in the file.
{ pkg* newPkg=new pkg; File->Read(newPkg->data,5000);
There you go! What if File->Position was one less than File->Size? You would
attempt to read more. The documentation below for Read says that what it
returns is the number of bytes actually transfered. Don't ignore that value.
Also, chances are, if 0-based, Position will probably never equal Size. It's
range will be [0,Size-1].
newPkg->info=0; Form1->SS1->Socket->Connections[0]->SendBuf((void* )newPkg,sizeof( pkg)); delete newPkg; } pkg* newPkg=new pkg; newPkg->info=status; Form1->SS1->Socket->Connections[0]->SendBuf((void* )newPkg,sizeof( pkg)); delete newPkg; delete File; return; }
--cut here--
this one works correctly with txt files, but not with binary. For some reason, File->Read(newPkg->data,5000); reads data from binary only when data is char* data[5000]; (but not inside the structure[?!?]). Here's info about
char vs. char* has nothing directly related to the problem. It seems to work
for you probably because sizeof(char*) is likely 4 times sizeof(char) on
your system. Allocating more than what you need is hiding the bug.
Here is the bit about the return value of Read:
Read returns the number of bytes actually transferred, which may be less than Count if the end of file marker is encountered.
Ali This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Dariusz |
last post by:
Below is part of a code I have for a database. While the database table is
created correctly (if it doesn't exist), and data is input correctly into
the database when executed, I have a problem when reading out the data into
the PHP variables / array.
It should be displaying the information out in the following way, using
the PHP array:
n_date, n_headline, n_summarytext, n_fulltext
|
by: fabrice |
last post by:
Hello,
I've got trouble reading a text file (event viewer dump) by using the
getline() function...
After 200 - 300 lines that are read correctly, it suddenly stops
reading the rest of the file...
Thank you to all of you who can help me with this one...
|
by: Boris Wilhelms |
last post by:
Hello all,
at first, sorry for my bad English, I’ll give my best
We have a strange problem reading Text- and VarChar-Fields.
Our configuration:
-Windows 2003 Server
-MySQL Server 3.23.36
-MyODBC 3.51
|
by: Jens Schreiber |
last post by:
Helo,
I'm using 4.1.8-standard on linux and get this:
CREATE TABLE test (field1 CHAR(20) BINARY NOT NULL, field2 CHAR(20)
NOT NULL) TYPE=MyISAM;
INSERT INTO test VALUES('xxx','yyy')
SELECT * FROM test WHERE field1=field2
ERROR 1267 (HY000): Illegal mix of collations (latin1_bin,IMPLICIT)
|
by: The_Kingpin |
last post by:
Hi again guys,
I've decided to cut my project in section and I found it way easier like
this. I'm having a little problem reading struct in a file though. I think
after this I'll be able to handle it on my own.
Right now the file is opened correctly and I'm able to read each line and
print them in a new file. My problem is to insert each struct (which are
made of 6 consecutive lines) as an item in my array.
| |
by: Brad |
last post by:
I'm having a problem reading a resource stream using the following syntax:
Dim resStream As System.IO.TextReader = New _
System.IO.StreamReader(Me.GetType.Assembly.GetManifestResourceStream("myname
space.script.js"))
I receive the error: Value cannot be null. Parameter name: stream
I think the underlying problem is that the name of the assembly
|
by: Manfred Braun |
last post by:
Hi All,
I have a problem reading queue-messages async. My QueueReader has a Start()
and a Stop() method and if my app starts, it calls Start(). The problem is,
that there are possibly several hundreds of messages in the queue, which I
do not want to have processed by the async messagehandler at startup. So in
Start(), I create a new thread, which's method just simply
enums the queue-messages . At the end
of this thread, I fire an...
|
by: andreas.fabri |
last post by:
I have a problem reading integers separated by commas with VC8
This program:
___________________________
// read.C
#include <iostream>
int main()
|
by: ashitpro |
last post by:
Hi All,
I am working on ext3 file system.
I am trying to read group descriptor from disk.
I am able to read the superblock structure successfully.
But I am not getting successful to get the group descriptor. Here is my code. Please check out what is missing.
#include<linux/ext3_fs.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |