简体   繁体   English

对 C 中的文件感到困惑?

[英]Confusion about files in C?

I want to work with files in C. I know that the standard library has some functions to help with that.我想用 C 语言处理文件。我知道标准库有一些函数可以帮助我。 However I have some questions.但是我有一些问题。 Are the contents of a file just an array of characters or is it a combination of ints, chars, doubles and more data types.文件的内容只是一个字符数组,还是整数、字符、双精度和更多数据类型的组合。 What are files terminated by in C?什么是 C 中终止的文件?

A few things jump to mind such as EOF and NULL but I'm not sure.一些事情跳到脑海中,例如 EOF 和 NULL,但我不确定。

C is extremely basic in that the contents of a file is just a byte stream until you write code that interprets otherwise. C非常基础,因为文件的内容只是一个字节流,除非您编写可以解释的代码。

Keep in mind the very concept of something like an int is extremely hazy at best due to endian issues .请记住,由于字节序问题,像int这样的东西的概念充其量是非常模糊的。 While any given C compiler has strong opinions on the form those take, these are architecture specific and not portable.虽然任何给定的 C 编译器都对它们所采用的形式有强烈的意见,但这些都是特定于体系结构且不可移植的。 Over the decades it has meant anything from binary coded decimal values, to "words" of varying sizes, and likely even stranger things along the way.几十年来,它的含义从二进制编码的十进制值到大小不一的“单词”,甚至可能是更奇怪的东西

Files aren't "terminated" by anything in C. They're just files.文件不会被 C 中的任何内容“终止”。它们只是文件。 They have zero or more bytes of data, and possibly holes to keep things interesting.它们有零个或多个字节的数据,可能还有一些漏洞来让事情变得有趣。

When reading a file you read in either a fixed amount of data, or read until you bump into the "End of File".读取文件时,您要么读取固定数量的数据,要么读取直到遇到“文件末尾”。

Remember, some files may be actively written to, so the EOF position might be constantly changing.请记住,某些文件可能会被主动写入,因此 EOF 位置可能会不断变化。

It's also important to keep in mind NULL in C means a pointer.同样重要的是要记住 C 中的NULL表示指针。 When talking about the zero byte used as a string terminator that's often called NUL to avoid confusion, a term captured in ASCII standard , though it actually pre-dates that standard.在谈论用作字符串终止符的零字节时,通常称为NUL以避免混淆,这是ASCII 标准中捕获的一个术语,尽管它实际上早于该标准。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM