Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 7 down vote accepted

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
share|improve this answer
1  
Of key note is the purpose of file is specific to identifying files by content alone. –  Joshua Jul 1 at 23:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.