?
Solved

Writing Byte Pointer to File

Posted on 2002-07-23
6
Medium Priority
?
193 Views
Last Modified: 2013-11-20
Hi,

I want to write
byte *a;
to a file. how can i write ?
 
0
Comment
Question by:dhirajbhise
[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
6 Comments
 
LVL 1

Accepted Solution

by:
jemax earned 150 total points
ID: 7173659
Hi,

ptr itself
 WriteFile (f, (LPCVOID)a, sizeof(LPVOID),...)
value at *a
 WriteFile (f, (LPCVOID)*a, sizeof(BYTE),...)

HTH,
jemax
0
 
LVL 1

Expert Comment

by:ackava
ID: 7174030
/*
    The following code is to write byte * b into file
    NOTE : nSize is size of bytes pointed by *b
*/

int nSize = ...;

FILE * fp = fopen("yourfilename.ext","wb");
if(fp==NULL)
{
    // OOPS SOME ERROR
}
else
{
    fwrite(&nSize,4,1,fp);
    fwrite(b,nSize,1,fp);
    fclose(fp);
}


/*
    The following code is to read same thing from file
*/
int nSize = ...;

FILE * fp = fopen("yourfilename.ext","wb");
if(fp==NULL)
{
    // OOPS SOME ERROR
}
else
{
    fread(&nSize,4,1,fp);
    b = new byte [nSize];
    fread(b,nSize,1,fp);
    fclose(fp);

    // now you can use byte * b
}
0
 
LVL 1

Expert Comment

by:ackava
ID: 7174032
Sorry a minor bug in my previous post, use this.

/*
   The following code is to write byte * b into file
   NOTE : nSize is size of bytes pointed by *b
*/

int nSize = ...;

FILE * fp = fopen("yourfilename.ext","wb");
if(fp==NULL)
{
   // OOPS SOME ERROR
}
else
{
   fwrite(&nSize,4,1,fp);
   fwrite(b,nSize,1,fp);
   fclose(fp);
}


/*
   The following code is to read same thing from file
*/
int nSize = ...;

FILE * fp = fopen("yourfilename.ext","rb");
if(fp==NULL)
{
   // OOPS SOME ERROR
}
else
{
   fread(&nSize,4,1,fp);
   b = new byte [nSize];
   fread(b,nSize,1,fp);
   fclose(fp);

   // now you can use byte * b
}
0
Industry Leaders: 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!

 
LVL 8

Expert Comment

by:fl0yd
ID: 7174401
A short note: Why are you writing a POINTER to a file? Unless you are using this for diagnostics it carries very little information. The next time you run your application, this pointer is completely worthless since it most definately changed. If you are using MSVC and run your application in debug mode it will be the same to make debugging easier; in release build, however, it will point to a different location in memory.
0
 
LVL 1

Expert Comment

by:kender_a
ID: 7176613
fl0yd,

Is the feature of the MSVC debug mode documented, or it's only and observation? (I observed this myself.)

Thanks,
Kender
0
 
LVL 8

Expert Comment

by:fl0yd
ID: 7177395
I read it somewhere -- don't remember where, though. I think it was some official ms paper, but don't hold me responsible for it ;)
0

Featured Post

Independent Software Vendors: 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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

765 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