简体   繁体   English

文本文件中的 NULL 终止符

[英]NULL Terminator in text files

I am creating a program that reads in the contents of a text file through the command line, character by character.我正在创建一个程序,该程序通过命令行逐个字符地读入文本文件的内容。

Is the NULL value automatically inserted or do I have to add it to the text file manually? NULL 值是自动插入还是我必须手动将其添加到文本文件中?

Text files do not need to have a terminator on modern platforms.在现代平台上,文本文件不需要有终止符。 (On some legacy platforms they did have one, but I doubt it is the case here.) You almost certainly should not write a terminator into the file, as it may cause problems with programs that do not expect one. (在一些遗留平台上他们确实有一个,但我怀疑这里的情况。)您几乎肯定不应该在文件中写入终止符,因为它可能会导致不期望终止符的程序出现问题。 The end of file serves as a terminator when reading.文件末尾在读取时用作终止符。

Text strings in C are arrays of characters terminated by a zero, aka the null character, mnemonic NUL (with one L, and it is not the same thing as NULL in C). C 中的文本字符串是以零结尾的字符数组,也就是空字符,助记符 NUL(带有一个 L,它与 C 中的NULL )。 When creating strings, you do need to terminate them correctly.创建字符串时,您确实需要正确终止它们。 Functions returning strings, including ones that read them from files (eg, fgets ), terminate them for you.返回字符串的函数,包括从文件中读取它们的函数(例如fgets ),会为您终止它们。

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

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