Link to home
Start Free TrialLog in
Avatar of LuckyLucks
LuckyLucks

asked on

writing from a pointer to a file

Hi

 I am trying to write a certain number of bytes, pointed by a pointer, p, to a file called myfile.

int numbytes=sizeof(float)*56;

FILE *myfile;
myfile=fopen("out.txt","w");  //returns a <Bad Ptr>
memcpy(myfile,memoryPtr,numbytes);

I noticed that myfile has a value 0x57251448 but it also says <Bad Ptr>. However, i read some confusing remarks on the web saying that might not necessarily mean something bad i,e <Bad Ptr> might just mean NULL pointer.

However, when i proceed with that in mind and I do memcpy, Visual Studio crashes and I am sent to some dbgheap.c file where I get an error at

/* check if the heap was not allocated by _aligned routines */
        if ( nBlockUse == _NORMAL_BLOCK)
        {
            if ( CheckBytes((unsigned char*)((uintptr_t)pUserData & ~(sizeof(uintptr_t) -1)) -nAlignGapSize,_bAlignLandFill, nAlignGapSize))
            {
                // We don't know (yet) where (file, linenum) pUserData was allocated
                _RPT1(_CRT_ERROR, "The Block at 0x%p was allocated by aligned routines, use _aligned_free()", pUserData);
                errno = EINVAL;
                return;
            }


What's the best way to write a chunk of memory , pointed to by a pointer,  to a file? And what is wrong with the current way I am doing things?
SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial