Home > Blockchain >  Confusion about files in C?
Confusion about files in C?

Time:12-17

I want to work with files in C. I know that the standard library has some functions to help with that. 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?

A few things jump to mind such as EOF and NULL but I'm not sure.

CodePudding user response:

C is extremely basic in that the contents of a file is just a byte stream until you write code that interprets otherwise.

Keep in mind the very concept of something like an int is extremely hazy at best due to endian issues. While any given C compiler has strong opinions on the form those take, these are architecture specific and not portable. 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. 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.

It's also important to keep in mind NULL in C means a pointer. 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.

  • Related