• Advertisement

Archived

This topic is now archived and is closed to further replies.

fucking fread( );

This topic is 5344 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all; I''m having a very strange problem here... Here is some part of the code: ... profiles = new cPROFILE[number_of_profiles]; fread(profiles,1,sizeof(cPROFILE)*number_of_profiles,file); delete []profiles; ... Of course the line "delete []profiles;" is not there, but if I put it there it generates an Acess Violation Error...but when i take off the fread line, the error disappears. May it be a compiler error? Thanx all

Share this post


Link to post
Share on other sites
Advertisement
i am almost pretty sure that your problem is in the call to fread try
fread (profiles,sizeof(cPROFILE),number_of_profiles,file);

hope that helps

Share this post


Link to post
Share on other sites
I''ve already tried using
fread(profiles,sizeof(cPROFILE),number_of_profiles,file);
but it didn''t solve my problem...

Share this post


Link to post
Share on other sites
shouldnt it be:
fread(&profiles[0], sizeof(cPROFILE)*number_of_profiles, 1, file);
?

Share this post


Link to post
Share on other sites
the only thing the two separate parameters do is if it reaches the end of file early, it reads the last whole structure before the end, but internally it just multiplies and reads. 1, x works fine, i use that all the time.

Share this post


Link to post
Share on other sites
There is no pointers inside cPROFILE....and even using &profiles[0] (i used it before), it doesn''t work....when i inspect profiles (with quickwatch), it is allocated and with the correct values...so the fread() is working, as well as new()...i think i''ll try to copy this part in other application and see the results...

Share this post


Link to post
Share on other sites
heh, well:

(profiles == &profiles[0]) == true

edit: forgot one 's'

[edited by - angry on July 5, 2003 2:16:25 AM]

Share this post


Link to post
Share on other sites
If cPROFILE has a virtual destructor, this code will most definitely fail. You''re smashing the v-table.

MSN

Share this post


Link to post
Share on other sites
If cProfile has any virtual functions, they will be completely trashed by freading like that. Same reason why you can''t use memset() on a class type.

Share this post


Link to post
Share on other sites
When you get the access violation, trace back into the CRT source code and try to see whats happening. If the file isn''t open, you''ll be dereferencing a NULL pointer, etc.
Also, does it work if you read just one profile? What about number_of_profiles-1 ?

Share this post


Link to post
Share on other sites
Thanx msn12b and sbennett ...
The cPROFILE class had a virtual destructor...when i took it off he error disappeared...i''m using memcpy in some parts of the program ...is there some problem or it can generate some errors too?

Share this post


Link to post
Share on other sites
You don''t want to use memcpy because it is a raw memory copy, while your class has hidden data like the vtable that you don''t have control over. And it''s not good practice. It''s much better you create your own assignment operator and ust something like std::copy instead.

Share this post


Link to post
Share on other sites
i whiped this up real quick and tested it with g++ 3.xx
try implementing this method, its clean and will alow you to expand later on.

this avoids you clearing the vftable, reading raw data into a classes memory space is a big no no, dont get into that habit.

hope this helps
-danushka


#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <iostream>

const char _Junkfile[] = "junk.bin";
const int _numInts = 5;
//--------------------------------------------------

// test class


class CTest
{
public:
int _data;

public:
CTest() {}
CTest(FILE *file) : _data(-1)
{
if( file )
fread(& _data, sizeof(int), 1, file);

std::cout << _data << std::endl;
}
~CTest() {};
};

//--------------------------------------------------

// create a test file with some test data


void WriteJunk()
{
FILE *junk = fopen(_Junkfile, "wb");

for(int i=0; i< _numInts; i++)
{
int _temp = i * 3;
fwrite(& _temp, sizeof(int), 1, junk);
}

fclose(junk);
}

//--------------------------------------------------

// entry point


int main(int argc, char **argv)
{
WriteJunk();

FILE *file = fopen(_Junkfile, "rb");
if( !file ) std::cout << "ERROR OPENING FILE!" << std::endl;

///////////////////////////////////////////

// heres the thing, create an array and contruct

// it with the file pointer


CTest *array = new CTest[_numInts](file);

///////////////////////////////////////////


std::cout << "Done reading." << std::endl;
delete [] array;
std::cout << "Safe Delete []" << std::endl;

fclose(file);
return 0;
}




A GOOD friend will come bail you out of jail...
but, a TRUE friend will be sitting next to you saying, "Damn, we fucked up."
Ingite 3D Game Engine Home -- Just click it


[edited by - silvermace on July 5, 2003 9:48:28 PM]

Share this post


Link to post
Share on other sites
I am the only one who is just a little annoyed by this thread''s title? Some days I wonder how posters like this can successfully code with such a limited intellect.

Share this post


Link to post
Share on other sites
doctorsixstring, i agree the title is a little crude, but i dont think you should grill his intelectual ablities based on 2 words.





A GOOD friend will come bail you out of jail...
but, a TRUE friend will be sitting next to you saying, "Damn, we fucked up."
Ingite 3D Game Engine Home -- Just click it

Share this post


Link to post
Share on other sites
While I don''t necessarily think you can base a person''s intellect on their grammar, I''m also a little put off by the title.

I would never let my son visit this site when language such as this is allowed to be used...


- Houdini

Share this post


Link to post
Share on other sites
quote:
Original post by Houdini
While I don't necessarily think you can base a person's intellect on their grammar, I'm also a little put off by the title.

I would never let my son visit this site when language such as this is allowed to be used...



I don't consider this thread's title to be bad grammar, but bad taste. I generally tolerate bad language when confined to a thread's text (but I don't particularly like it), but I find it inexcusable to place it in a thread's title for the world to see.

And before anyone starts up about how I have diverted this thread off-topic, consider that perhaps the original poster should have thought twice about starting a thread which such a title.

-Mike

[edited by - doctorsixstring on July 8, 2003 3:08:51 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by doctorsixstring
I am the only one who is just a little annoyed by this thread''s title? Some days I wonder how posters like this can successfully code with such a limited intellect.
A little over-sensitive here, huh? Oh momma, he said the ''f'' word. Don''t lose your sleep.

Share this post


Link to post
Share on other sites
fuck is a very versatile word. you can make entire sentences with it, excluding a few stupid words like ''the''.

Share this post


Link to post
Share on other sites
quote:
Original post by civguy
quote:
Original post by doctorsixstring
I am the only one who is just a little annoyed by this thread''s title? Some days I wonder how posters like this can successfully code with such a limited intellect.
A little over-sensitive here, huh? Oh momma, he said the ''f'' word. Don''t lose your sleep.


it''s unnecessary and inappropriate and annoys me.

Share this post


Link to post
Share on other sites

  • Advertisement