What is a binary file, how to open it?

Table of Contents

Binary file

What is a Binary file?

A binary file is a computer file that is different form a text file. It may contain data, like a computer program, or it may be a file that stores data for a program to use. In generally, it is a machine code, compilated by the assembly language.

how to open binary file?

There are a few different ways to open a binary file. The common way is to use the Binary Editor: C++ or Windows API function.

1.bin file Editor: C++ Programm

To open a binary file with C++, you need to use the ifstream class, which is part of the standard C++ library. To use this method, you first need to include the fstream header file. Then, you can open a binary file by using the ifstream::open() member function, passing in the name or path of the file as a parameter. For example:

1.1 Open bin file

				
					/// C++ open bin file
void getBinSize(std::string path)
{
int size = 0;
std::ifstream infile(path, std::ifstream::binary);

infile.seekg(0, infile.end);
int size= infile.tellg();
infile.seekg(0, infile.beg);

infile.close();
printf("\npath=%s,size=%d \n", path, size);
return size;
}

void readBin(std::string path, char *buf, int size)
{
std::ifstream infile(path, std::ifstream::binary);

infile.read(static_cast<char *>(buf), size);
infile.close();
}
				
			

1.2 Saving Bin File

				
					/// C++ save bin file
void writeBin ( std::string path , char * buf , int size )
{ std::ofstream outfile ( path , std::ifstream::binary );

outfile.write((char *)(buf), size);

outfile.close();
}
				
			

1.3 Reading Bin File

				
					// read binFile
std::string filePath= "./demo.bin";
int size = GetBinSize(filePath);
char *buf= new char[size];
readBin(filePath, buf, size);
float *fbuf = reinterpret_cast<float *>(buf);

// write binFile
std::string saveFilePath= "./demo_saved.bin";
writeBin(saveFilePath, buf, size);

delete buf;
				
			

2. Windows API function CreateFile()

Another way to open a binary file is to use the Windows API function CreateFile(). This function is part of the Windows API, which is not part of the C++ standard library. However, you can use it in your C++ programs by including the Windows.h header file. The CreateFile() function takes a variety of parameters, but the most important ones for opening a binary file are the file name or path, the desired access (for example, GENERIC_READ for reading or GENERIC_WRITE for writing), and the share mode (for example, FILE_SHARE_READ to allow other processes to read the file while your process has it open). For example:

				
					#include Windows.h>

int main()
{
HANDLE file = CreateFile("example.bin", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
// ...
}
				
			

Binary File Error: "cannot execute binary file exec format error"

The “cannot execute binary file: Exec format error” error typically occurs when you try to run a binary file that is not compatible with your system’s architecture. Here’s a case to show how to solve such problem.

The following is the program code I compiled with a cross compiler:

				
					#include <stdio.h>
void main()
{
   printf("hello arm");
}
				
			

Compile on pc:

				
					# arm-linux-gnueabi-gcc -o main main.c
				
			

Error message when running on pc:

				
					root@ubuntu:/home/jinxin/app# ./main
bash: ./main: cannot execute binary file: Exec format error
				
			

The current pc is of x86 architecture, and the binary file just compiled is in arm format:

				
					root@ubuntu:/home/jinxin/app# file main
root@ubuntu:/home/jinxin/app# file main
main: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=ec9b455a1c1df94060c7364d0efc42975207fca9, not stripped
				
			

Solution of EXEC Format Error

To solve this issue, we have a few options:

Run the binary file on an ARM-based device: If you have access to an ARM-based device, you can copy the binary file to that device and run it there.

Cross-compile the code for x86 architecture: You can use an x86 cross-compiler to compile the code for the x86 architecture. To do this, you need to install an x86 cross-compiler on your PC and compile the code with it.

Use an emulator: You can use an emulator such as QEMU to emulate an ARM-based device on your x86-based PC and run the binary file on the emulator.

Use a virtual machine: You can use a virtual machine to emulate an ARM-based device on your x86-based PC and run the binary file on the virtual machine.

Overall, the best solution depends on your specific use case and available resources.

Subscribe

Join our subscribers list to get monthly blog updates, technology news, case studies. We will never send spam, and you can unsubscribe at any time.

About Author

Picture of Aidan Taylor
Aidan Taylor

I am Aidan Taylor and I have over 10 years of experience in the field of PCB Reverse Engineering, PCB design and IC Unlock.

Need Help?

Don't hesitate - Get in touch today to find out how we can help take your project to the next level.

Table of Contents

Scroll to Top
welldone pcb manufacturer

Start to Achieve Your PCB Project