Recommendations

< Back to Analysis | Return to Main Page

I originally called this page "Conclusions," but after I wrote it I realized that it contains more opinion than fact. So, here are my "recommendations" for processing text files in the C programming language:

Test Your Compiler

You should test your compiler on your platform to see what is actually happening. Feel free to download my work, or create something similar yourself, but you'll never know what's really going on until you see it for yourself in your particular environment.

Always Open Text Files in Binary Mode

Most of the problems and inconsistencies in this demonstration occurred when files were opened in text mode. Don't let the compiler interpret (possibly incorrectly) things for you. The only way you can be sure you're processing a text file correctly is to have access to the complete file, and the only way to do that is to open it in binary mode (unless your compiler is like gcc where there is no difference between text and binary modes).

Use fread() to Read Text Files...

...and read in as much of a file as you can at one time, preferably the whole file, for the performance advantage.

fgets() is also a reasonable function for line-oriented reading, but as we saw, it does have some limitations. Use it if you like it, but be aware that it might not work in all cases.

Don't Make Any Assumptions About the Files You Read

A case in point: The three compilers tested here were unable to correctly read text files in Macintosh format using the fgets() function. You might say "I'll never be reading Mac files, so it's safe to use fgets()."

There's an old adage that states "If something absolutely can't happen, then it definitely will." You have been warned.


I hope you enjoyed this presentation and learned something from it. If you have any questions or comments, please write to me.

--Scott Brueckner, 9 Nov 1999

< Back to Analysis | ^ Up to Top | Return to Main Page