?
Solved

Binary File IO in Visual C++

Posted on 2003-03-31
3
Medium Priority
?
246 Views
Last Modified: 2013-11-15
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 :(

0
Comment
Question by:Alex_Baskeyfield
[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
3 Comments
 
LVL 12

Accepted Solution

by:
Salte earned 100 total points
ID: 8238171
well, for one thing you write the values 4 bytes at a time so o fcourse you get those extra 0 bytes.

You can do one of the following ways to solve the problem:

1. Write one byte at a time.
2. Write the 4 bytes in one bunch.

Method 1 is essentially just looping writing one byte each time, but it's just as easy to use method 2 so I will show method 2 here.

Your main problem is that you declare the array as an array of 'int'. int is 32 bit or 4 bytes so each value stored in the array is 4 bytes. If you want them to be bytes declare the array as an array of bytes instead:

unsigned char headder[4] = {0x42, 0x4D, 0x82, 0x88};

Then write it like this:

int headderwritten = fwrite(headder, sizeof(char), 4, fp);

or some such.

However, a better way is to use the BMP structs defined in windows and so you use something like (I am not sure of the name of the struct etc but you have it in your online help):

BITMAPFILEHEADER bmphdr;

// fill in the struct propertly...
bmphdr.iSignature = 0x88824d42;

etc...

then:

fwrite(bmphdr,1,sizeof(bmphdr),fp);

should work.

Alf
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9566528
No comment has been added lately, so it's time to clean up this TA.
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
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

777 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