Solved

writing from a pointer to a file

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 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.

707 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