Solved

Writing Byte Pointer to File

Posted on 2002-07-23
6
190 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
6 Comments
 
LVL 1

Accepted Solution

by:
jemax earned 50 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
bobThere java chalenge 45 94
List out all word 7 320
sumHeights2  challenge 7 117
Problem to App 4 99
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

820 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