?
Solved

writing from a pointer to a file

Posted on 2014-04-14
3
Medium Priority
?
710 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 1002 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 498 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 1002 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

650 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