how to determine a file is ASCII or binary?

T

those who know me have no need of my name

[fu-t set]

i've set follow-up's to be mailed. adjust that if you feel there's a more
appropriate venue, i.e., one that actually covers `*nix'. be warned
comp.unix.programmer is not the only possibility. at any rate it's
off-topic for clc hence the fu-t.

in comp.lang.c i read:
[C source files as executables on *nix]
No one said that such a shell cannot be imagined, merely that /bin/sh,
the *only* shell that doesn't need the #! "magic number" in the executable
file is NOT such a shell. And to get the executable file interpreted by
any other program, you need the #! "magic number", but a valid C source
file cannot start with this character pair.

since this is not the appropriate venue i'll refrain from making
corrections -- which might not be warranted, after all i've no idea what
actual platforms are under discussion. it seems to be generally assumed
that there is `one' master *nix which everything follows -- which is not
what exists in reality.
 
O

one2001boy

Leor said:
Hi, all

Since the OS look both ASCII and binary file as a sequence of bytes, is
there any way to determine the file type except to judge the extension?


Portably, in C? Nah, because a "binary" file can simply mimic an ASCII file
and no one, or no /thing/, could possibly tell you whether the data was
written in binary mode or text mode.

The best you can do is take the approach of the Unix "file" command. Here's
a sample output I just got from running it under Cygwin on a text file:

[/home/leor] $ file s2
s2: ASCII English text, with CRLF line terminators

your solution won't work if s2 has attributes ---x--x--x
 
O

one2001boy

Dan said:
You're actually making my point: the *default* interpreter is not an
interpreter for C source code.

Have you ever seen a C interpreter (Ch) for C source code without "#!"?

ch-4.5> cat test.c
#include <stdio.h>
int main() {
printf("Hello, world\n");
return 0;
}
ch-4.5> chmod +x test.c
ch-4.5> ./test.c
Hello, world
ch-4.5>

If you want test.c works in any shells other than ch, you can add #!
in the first line.

In case you are interesetd, you can take a look ch shell
http://www.softintegration.com/docs/ch/shell/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top