You are here:

C++/binary file

Advertisement


Question
Hi,  I would like to insert a string into an image file and make sure the computer thought it was an image file.  I googled "file headers" and found a page that said that the first four bytes  in hexadecimal of a jpeg file are "FF D8 FF E0" then you skip two byts and then you have 4A 46 49 46 00.  I would like to put this at the beginning of my image file followed by my message.  I am using the dev c++ compiler for windows vista.  So far I have this "
#include <iostream>
#include <fstream>
using namespace std;
int main() {
char* message = "Hello";

ofstream myfile("data.jpg", ios::out | ios::binary);
myfile.write(message,(sizeof(message)+1));
myfile.close();
}
Can you point me in the right direction thanks a lot for listening

Answer
Define arrays, or simply write these bytes

const unsigned char part1[] = { 0xFF, 0xD8, 0xFF, 0xE0 };
const unsigned char part2[] = { 0x4A, 0x46, 0x49, 0x46, 0x00 };

I'm not sure why you have to skip 2 bytes - why not write them (they must mean something).

Example:

myfile.write(part1,sizeof part1);

Bill

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bill A

Expertise

I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.

Experience

I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Publications
Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

Education/Credentials
BS Computer Engineering

©2016 About.com. All rights reserved.