Solved

writing from a pointer to a file

Posted on 2014-04-14
3
645 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

785 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