Alex_Baskeyfield
asked on
Binary File IO in Visual C++
Hi,
I hope someone can help me as im new to programming and i've hit a brick wall. I want to write the following bytes to a file 0x42, 0x4D, 0x82, 0x88. The bytes form the first part of a windows BMP headder file so need to be exact.
So far i've got the code below but it actually produces the following in the file 0x42, 0x00, 0x00, 0x00, 0x4D, 0x00, 0x00, 0x00, 0x82, 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00,
How do I stop it from putting in the extra 3 bytes in between values?
FILE* f = fopen("grab.bmp", "wb");
if( f == NULL )
{ printf("Error opening file\n");
// Some other error handling code
}
// Create an integer array to write
int headder[] = {0x42, 0x4D, 0x82, 0x88};
// Write headder to file
int headderWritten = fwrite(headder, sizeof(int), 4, f);
if( fclose(f) != 0 )
{
printf("Error closing file\n");
}
Many thanks,
Alex Baskeyfield
-------------------------- ---------- ---------- ---------- --
No trees were killed during the sending of this message
but a large number of electrons were terribly inconvienced
-------------------------- ---------- ---------- ---------- ---
p.s. My appologies about the points for the message, its all the points i have left :(
I hope someone can help me as im new to programming and i've hit a brick wall. I want to write the following bytes to a file 0x42, 0x4D, 0x82, 0x88. The bytes form the first part of a windows BMP headder file so need to be exact.
So far i've got the code below but it actually produces the following in the file 0x42, 0x00, 0x00, 0x00, 0x4D, 0x00, 0x00, 0x00, 0x82, 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00,
How do I stop it from putting in the extra 3 bytes in between values?
FILE* f = fopen("grab.bmp", "wb");
if( f == NULL )
{ printf("Error opening file\n");
// Some other error handling code
}
// Create an integer array to write
int headder[] = {0x42, 0x4D, 0x82, 0x88};
// Write headder to file
int headderWritten = fwrite(headder, sizeof(int), 4, f);
if( fclose(f) != 0 )
{
printf("Error closing file\n");
}
Many thanks,
Alex Baskeyfield
--------------------------
No trees were killed during the sending of this message
but a large number of electrons were terribly inconvienced
--------------------------
p.s. My appologies about the points for the message, its all the points i have left :(
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by: Salte
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Tinchos
EE Cleanup Volunteer