Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if file data is binary

Tags:

c++

when I write a number to binary file, it won't display. but in case of a character, it does. why? how would you check to see if the file containing character is binary?

like image 214
Gates127 Avatar asked Jun 10 '26 02:06

Gates127


2 Answers

It has all to do with how you interpret what is in the file. Everything in a file is binary, a character an integer etc.

When you do TYPE in the console on a file (or CAT or whatever OS u have) the contents of the file are interpreted as text by default because the programmer of TYPE decided to write it like that.

When you write a program to read out data from a file it is up to you to decide how to interpret the data what you read.

That is why you can only guess file contents and that is why often the extension of a file is used to give a hint on how the contents should be interpreted.

like image 135
AndersK Avatar answered Jun 12 '26 14:06

AndersK


I think what you're really asking is whether you (personally) can interpret what's in the file.

As Anders alluded to, you can usually read a file as text regardless of what's in it - the characters, however, might make no sense.

Assuming you're writing software to perform this task, perhaps the following (high-level) algorithm will help:

  1. Create a list of characters you find acceptable as text
  2. Read the file, interpreting it as ASCII
  3. If any characters in the file aren't in your list, fail.
  4. Repeat steps 2 and 3 for any text-encoding you want to handle (ASCII, UTF-x, etc.)
  5. If nothing passes, it's not text.

Does this help?

like image 27
Damovisa Avatar answered Jun 12 '26 16:06

Damovisa