Solved

writing from a pointer to a file

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now