3

When using file to check some file types, I noticed that it seems to incorrectly label C++ files as C files:

$ file User.*
User.cpp: C source, ASCII text
User.h:   C++ source, ASCII text
User.o:   ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped

Why is it correctly identifying the header file, but not the cpp file?

1 Answer 1

9

From man page of file command,

file command actually performs 3 tests on determining the file type.

First test

The filesystem tests are based on examining the return from a stat(2) system call.

Second test

The magic number tests are used to check for files with data in particular fixed formats.

Third test

The language tests look for particular strings (cf names.h) that can appear anywhere in the first few blocks of a file. For example, the keyword .br indicates that the file is most likely a troff(1) input file, just as the keyword struct indicates a C program.

The output of the file command is generally based on the result of any of the tests that succeeds.

Now, assuming the C++ program starts like this, and the third test succeeds,

#include <iostream.h>
bla
bla

As per the third test the keyword #include particularly specifies it is of type C program though we have a CPP program in hand. Now, when I check,

$ file example.cpp

example.cpp: ASCII C program text

Now, the concepts of object oriented are specific to C++. Let us create a file specific to C++.

I start my C++ program as,

Class something
{
}
bla
bla

Now, when I issue

$ file example.cpp

The output is,

example.cpp: ASCII C++ program text
1
  • 1
    Of key note is the purpose of file is specific to identifying files by content alone. Commented Jul 1, 2014 at 23:05

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.