File Processing Strategies
An early decision in any Windows development or porting project is to select whether file processing should be done with the C library or with the Windows functions. This is not an either/or decision because the functions can be mixed with caution even when you're processing the same file.The C library offers several distinct advantages, including the following.
- The code will be portable to non-Windows systems.
- Convenient line- and character-oriented functions that do not have direct Windows equivalents simplify string processing.
- C library functions are generally easier to use than Windows functions.
- The line and stream character-oriented functions can easily be changed to generic calls, although the portability advantage will be lost.
- The C library will operate in a multithreaded environment, as shown in Chapter 7.
Nonetheless, there are some limitations to the C library. Here are some examples.
- The C library cannot manage or traverse directories, and it cannot obtain or set most file attributes.
- The C library uses 32-bit file position in the fseek function, so, while it can read huge files sequentially, it is not possible to position anywhere in a huge file, as is required, for instance, by Program 3-1.
- Advanced features such as file security, memory-mapped files, file locking, asynchronous I/O, and interprocess communication are not available with the C library. Some of the advanced features provide performance benefits, as shown in Appendix C.
Another possibility is to port existing UNIX code using a compatibility library. Microsoft C provides a limited compatibility library with many, but not all, UNIX functions. The Microsoft UNIX library includes I/O functions, but most process management and other functions are omitted. Functions are named with an underscore prefixfor example, _read, _write, _stat, and so on.Decisions regarding the use and mix of C library, compatibility libraries, and the Win32/64 API should be driven by project requirements. Many of the Windows advantages will be shown in the following chapters, and the performance figures in Appendix C are useful when performance is a factor.
