二进制或Ascii文本? [英] Binary or Ascii Text?

查看:75
本文介绍了二进制或Ascii文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我有一个问题。如何识别文件是否是b $ b二进制文件或ascii文本文件?例如,我写了一段

代码并保存为Test.c。我知道这是一个ascii文本文件。然后

编译后,我得到了一个测试文件,它是一个二进制可执行文件

文件。问题是,我知道这两个文件的类型在我的脑海里

因为我执行了编译过程,但我怎么能让

计算机知道它的类型通过在C中编写代码来获取给定文件?文件是

全部保存为0'和1'。有什么区别?


请帮助我,谢谢。

Hi, everyone. I got a question. How can I identify whether a file is a
binary file or an ascii text file? For instance, I wrote a piece of
code and saved as "Test.c". I knew it was an ascii text file. Then
after compilation, I got a "Test" file and it was a binary executable
file. The problem is, I know the type of those two files in my mind
because I executed the process of compilation, but how can I make the
computer know the type of a given file by writing code in C? Files are
all save as 0''s and 1''s. What''s the difference?

Please help me, thanks.

推荐答案

Claude Yih <无线****** @ gmail.com>写道:
"Claude Yih" <wi******@gmail.com> writes:
大家好。我有一个问题。如何识别文件是二进制文件还是ascii文本文件?例如,我写了一段
代码并保存为Test.c。我知道这是一个ascii文本文件。然后在编译之后,我得到了一个测试。文件,它是一个二进制可执行文件。问题是,我知道这两个文件的类型在我的脑海中因为我执行了编译过程,但是如何通过在C中编写代码使
计算机知道给定文件的类型?文件全部保存为0'和1'。有什么区别?
Hi, everyone. I got a question. How can I identify whether a file is a
binary file or an ascii text file? For instance, I wrote a piece of
code and saved as "Test.c". I knew it was an ascii text file. Then
after compilation, I got a "Test" file and it was a binary executable
file. The problem is, I know the type of those two files in my mind
because I executed the process of compilation, but how can I make the
computer know the type of a given file by writing code in C? Files are
all save as 0''s and 1''s. What''s the difference?




没有通用的解决方案。许多系统实际上并没有区分文本和二进制文件。一个文本文件只是一个文件

碰巧包含可打印的字符 - 以及什么'

被认为是可打印的字符可能会有所不同。您还可以查看一行

终结符(类似Unix的系统上的ASCII LF,一个类似于Windows的系统上的ASCII CR-LF序列,可能是完全不同的

其他地方。


< OT>类Unix系统有一个名为file的命令。试图根据其内容对文件进行分类。< / OT>


-

Keith Thompson(The_Other_Keith ) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



There is no general solution to this. Many systems don''t actually
distinguish between text and binary files; a text file is just a file
that happens to consist of printable characters -- and what''s
considered a printable character can vary. You can also look at line
terminators (ASCII LF on Unix-like systems, an ASCII CR-LF sequence on
Windows-like systems, possibly something completely different
elsewhere).

<OT>Unix-like systems have a command called "file" that attempts to
classify a file based on its contents.</OT>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


" Claude Yih"写道:
"Claude Yih" writes:
大家好。我有一个问题。如何识别文件是二进制文件还是ascii文本文件?例如,我写了一段
代码并保存为Test.c。我知道这是一个ascii文本文件。然后在编译之后,我得到了一个测试。文件,它是一个二进制可执行文件。问题是,我知道这两个文件的类型在我的脑海中因为我执行了编译过程,但是如何通过在C中编写代码使
计算机知道给定文件的类型?文件全部保存为0'和1'。有什么区别?
Hi, everyone. I got a question. How can I identify whether a file is a
binary file or an ascii text file? For instance, I wrote a piece of
code and saved as "Test.c". I knew it was an ascii text file. Then
after compilation, I got a "Test" file and it was a binary executable
file. The problem is, I know the type of those two files in my mind
because I executed the process of compilation, but how can I make the
computer know the type of a given file by writing code in C? Files are
all save as 0''s and 1''s. What''s the difference?




你能做的最好的就是做出猜测。 ASCII的前32个字符是

控制代码,只有少数几个(CR,LF,FF,HT(标签),....存在

in因此,如果你有其他25个左右的代码,那么它可能不是一个文本文件 - 但它只是一个有根据的猜测,没有真正的证明。 />

但请注意,当C程序员讨论文本与

二进制文件时,这并不是什么意思,例如在某些文件函数中。 br />
两种处理行尾的方法之间存在区别。

行的末尾是单个字符(LF)或两个字符< CR>< LF>?

Unix仅使用LF来标记行尾,因此区别是

无意义。使用< CR>< LF>或< LF>< CR>必须检查

流并将两个字符转换为一个,称为''\ n''所以''\ n''是

真的< LF> ..


当你以二进制模式打开一个文件时,你告诉全世界:嘿,你在那里
,保持你的棉花摘取这个文件。



The best you can do is make a guess. The first 32 characters of ASCII are
control codes and only a few of them (CR, LF, FF, HT (tab), .... are present
in text files. So if you have quite a few of the other 25 or so codes, it is
probably not a text file - but it''s only an educated guess, no real proof.

But note that this is not what is meant when C programmers discuss text vs.
binary, for example in some of the file functions. What is referred to
there is the distinction between two ways of handling end of lines. Is an
end of line demarked by a single character (LF) or two characters <CR><LF>?
Unix uses only the LF to mark end of line, so the distinction is
meaningless. Systems that use <CR><LF> or <LF><CR> have to examine the
stream and convert the two characters into one, called ''\n'' So ''\n'' is
really <LF>..

When you open a file in binary mode, you are telling the world: Hey, you
there, keep your cotton-picking hands off this file.


" Claude Yih" <无线****** @ gmail.com>写道:

#大家好。我有一个问题。如何识别文件是否是b $ b#二进制文件或ascii文本文件?例如,我写了一段

#代码并保存为Test.c。我知道这是一个ascii文本文件。然后

#编译后,我得到了一个测试文件,它是一个二进制可执行文件
#文件。问题是,我知道这两个文件的类型在我的脑海里

#因为我执行了编译过程,但我怎么能让

#计算机知道通过在C中编写代码来获取给定文件的类型?文件是


就stdio而言,如果你b $ b包含一个b,那么就会得到一个二进制文件。在打开模式下,否则是文本模式。二进制和

文本文件可以不同地处理行尾指示符,以及如何解释
fseek偏移量。 (在unix中,stdio以相同的方式处理二进制文件和

文本文件。)


-

SM Ryan http://www.rawbw.com/~wyrmwif/

GERBILS

GERBILS

GERBILS
"Claude Yih" <wi******@gmail.com> wrote:
# Hi, everyone. I got a question. How can I identify whether a file is a
# binary file or an ascii text file? For instance, I wrote a piece of
# code and saved as "Test.c". I knew it was an ascii text file. Then
# after compilation, I got a "Test" file and it was a binary executable
# file. The problem is, I know the type of those two files in my mind
# because I executed the process of compilation, but how can I make the
# computer know the type of a given file by writing code in C? Files are

As far as stdio is concerned, a binary file is what you get if you
include a "b" in the open mode, otherwise it''s text mode. Binary and
text files may handle end-of-line indicators differently, and how
fseek offsets are interpretted. (In unix, stdio treats binary and
text files identically.)

--
SM Ryan http://www.rawbw.com/~wyrmwif/
GERBILS
GERBILS
GERBILS


这篇关于二进制或Ascii文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆