-4

So I've only recently started to try C++ and I've already learned the basics. All I want to know is how can I write/read bytes/ints/longs to/from a file.

First of all, I'd like to tell you why do I need it. Basically I want to read data from a file which has a special format in it. All the data is written in binary in that file.

File format specification

I've already written that program in another language and I'd like to re-write my program in C++. The language I used previously is called BlitzMax and in that language function are already implemented and just called WriteByte, ReadByte, WriteInt, ReadInt etc.. If you guys will be kind enough to write (or at least link me the source) the functions I need, it will be much appreciated. And if you will write them for me, can you also explain how they work?

Thank you very much to everyone who will help me! :)

Edit: Here, as requested, the code that is somewhat does what I need. It does write the int 50 in binary to a file, but that's as far as I could go to. I still can't understand some parts (code was found in google, I edited it a bit). And I still need a way to write bytes and longs.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int num = 50
    ofstream file("file.txt", ios::binary);
    file.write(reinterpret_cast<const char *>(&num), sizeof(num));
    file.close ();
    return 0;
}
3
  • 1
    this is not a 'give me the codez' site. show what you've tried. Commented May 5, 2015 at 13:15
  • I didn't ask you to make me a whole program, all I want is to find the way to write binary into a file. I have a really small idea on how to make it and that's why I asked to make the code and explain how it works. But sure, I can show you what I've tried so far. I edited it in, take a look. Commented May 5, 2015 at 13:19
  • stackoverflow.com/questions/9244563/…; some may advise you against binary writing due to portability Commented May 5, 2015 at 13:21

1 Answer 1

0

Just follow in the same fashion:

int num = 50;
unsigned char x = 9;
float t = 9.0;

ofstream file("file.bin", ios::binary);
file.write(reinterpret_cast<const char *>(&num), sizeof(num));
file.write(reinterpret_cast<const char *>(&x), sizeof(x));
file.write(reinterpret_cast<const char *>(&t), sizeof(t));
file.close ();
return 0;

Some more info on reading.

But beware of portability issues when doing binary reading/writing. Portability issues maybe related to the fact that integer can be 4 bytes on your machine and have different size on other machine. Endianness is also another concern. Encoding floats is binary is also not portable (here).

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much! You can't even imagine for how long I've been looking for the solution...
due to (possible) issues mentioned maybe you wanna consider writing those integers as text; you could search for that
CS2D (the game I'm writing the app for) is using that format, so it's up to creators of that game. But I will try to add some checks for the compatibility issues of that regard. But thanks for the heads-up, will use text integers if I was to write a game.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.