Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading