1

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?

Daniel Heilper
  • 1,182
  • 2
  • 17
  • 34
Norman
  • 21
  • 2
  • 4
  • Instead of NULL, you can simply use EOF, which means end of file and it's inserted automatically. [http://stackoverflow.com/questions/1782080/what-is-eof-in-the-c-programming-language] – klancar16 Dec 01 '16 at 20:47
  • `NULL` is a macro with a _null pointer constant_. There is no `NULL` character or a null pointer constant in text files. It is not clear what you mean. Please see [ask] and add more information to your question, including your code in question. – too honest for this site Dec 01 '16 at 20:54

1 Answers1

2

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). When creating strings, you do need to terminate them correctly. Functions returning strings, including ones that read them from files (e.g., fgets), terminate them for you.

Arkku
  • 41,011
  • 10
  • 62
  • 84
  • @Norman that legacy terminator *in the text file* was not `NULL` but for example, `Ctrl-Z` or `0x1A`. – Weather Vane Dec 01 '16 at 20:51
  • @WeatherVane Yes, I know, that's why I didn't specify null terminator but simply "a terminator". Anyway, I don't think it is relevant to the question, just a bit of trivia. =) – Arkku Dec 01 '16 at 20:52
  • sorry remark was to OP who asked "is NULL value automatically inserted or do I have to add it to the text file manually", so perhaps relevant to the question. – Weather Vane Dec 01 '16 at 20:55
  • This is what I was looking for. Thank you :) – Norman Dec 01 '16 at 20:57