1

I'm trying to check the given file is binary or not. I refer the link given below to find the solution, How can I check if file is text (ASCII) or binary in C

But the given solutions is not working properly, If I pass the .c file as argument, Its not working, It gives wrong output.

The possible files I may pass as argument:

a.out

filename.c

filename.txt

filename.pl

filename.php

So I need to know whether there is any function or way to solve the problem?

Thanks...

Note : [ Incase of any query, Please ask me before down vote ]

11
  • What is binary for you/your task? Is it sufficient to check file extension or does it require parsing the file. You might also consider providing your not working code for the community to check. Commented Oct 24, 2016 at 6:04
  • No, I won't check the extension, I need to parse the file and find the file is binary or not. Commented Oct 24, 2016 at 6:08
  • I'm trying to implement the 'grep' command in c Commented Oct 24, 2016 at 6:08
  • 1
    Do you really want to check every byte in the file to make sure it is within ASCII range or not? If the file is huge it would cost you. What about Unicode and various UTF encodings? They are also text files. Or a file that is mostly ASCII but contains a few bytes of non-ASCII? You can also consider how grep handle it: unix.stackexchange.com/questions/19907/… Commented Oct 24, 2016 at 6:17
  • 1
    Are you on Linux? If yes, just use the command "file <file_name>" and see the output. You can then call this command from your C program using system(). Refer man or check here for more details on the output of "file" command - linux.die.net/man/1/file Commented Oct 24, 2016 at 7:52

2 Answers 2

2

You need to clearly define what a binary file is for you, and then base your checking on that.

If you just want to filter based on file extensions, then create a list of the ones you consider binary files, and a list of the ones you don't consider binary files and then check based on that.

If you have a list of known formats rather then file extensions, attempt to parse the file in the formats, and if it doesn't parse / the parse result makes no sense, it's a binary file (for your purposes).

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

1 Comment

On UNIX-like platforms, there is also libmagic, a C interface to file, which could be used to detect file types
1

Depending on your OS, binary files begin with a header specifying that they are an executable and contain several informations about them (architecture, size, byte order etc.). So by trying to parse this header, you should know if a file is binary or not. If you are on mac, check the Mach-O file format, if you are on Linux, it should be ELF format. But beware, it's a lot of documentation.

Comments

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.