简体   繁体   中英

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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