Solved

writing from a pointer to a file

Posted on 2014-04-14
3
662 Views
Last Modified: 2014-04-16
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?
0
Comment
Question by:LuckyLucks
  • 2
3 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 334 total points
ID: 39999388
There are two issues with your approach:

- You need to open your file in binary tranylation mode ("wb" instead of "w")
- You can't 'memcpy()' to a file

That should be more like

int numbytes=sizeof(float)*56;

FILE *myfile;
myfile=fopen("out.bin","wb");  // if that returns a <Bad Ptr>, be sure you have the permissions to create a file in the current folder
fwrite(memoryPtr,numbytes,1,myfile); // write the memory chunk
fclose(myfile);

Open in new window

0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 166 total points
ID: 39999508
I have never encountered a case where fopen did not work when returning a valid pointer. Try this.. When making library api calls, check to see what it returns. Notice the check on fopen() from this example taken from http://www.cplusplus.com/reference/cstdio/fopen/?kw=fopen
/* fopen example */
#include <stdio.h>
int main ()
{
  FILE * pFile;
  pFile = fopen ("myfile.txt","w");
  if (pFile!=NULL)                     // CHECK RETURN VALUE OF fopen()
  {
    fputs ("fopen example",pFile);
    fclose (pFile);
  }
  return 0;
}

Open in new window

0
 
LVL 86

Accepted Solution

by:
jkr earned 334 total points
ID: 39999751
>>I have never encountered a case where fopen did not work when returning a valid
>>pointer.

That's why I added

// if that returns a <Bad Ptr>, be sure you have the permissions to create a file in the current folder

Open in new window


This - along with File System Virtualization on Windows - can indeed cause that problem.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question