Book contents
- Frontmatter
- Contents
- Preface
- 1 Introduction
- 2 Computer Programming
- 3 Types, Operators, and Expressions
- 4 Control Flow
- 5 Type Conversion, Functions, and Scope
- 6 Pointers, Arrays, and Structures
- 7 File Operations
- 8 Case Studies
- Appendix A C Language Summary
- Appendix B Fortran Program Language Summary
- Appendix C ASCII Tables
- Appendix D C Preprocessor Directives
- Appendix E Precedence Tables
- Glossary
- Annotated Bibliography
- Index
7 - File Operations
Published online by Cambridge University Press: 05 June 2012
- Frontmatter
- Contents
- Preface
- 1 Introduction
- 2 Computer Programming
- 3 Types, Operators, and Expressions
- 4 Control Flow
- 5 Type Conversion, Functions, and Scope
- 6 Pointers, Arrays, and Structures
- 7 File Operations
- 8 Case Studies
- Appendix A C Language Summary
- Appendix B Fortran Program Language Summary
- Appendix C ASCII Tables
- Appendix D C Preprocessor Directives
- Appendix E Precedence Tables
- Glossary
- Annotated Bibliography
- Index
Summary
Access to files allows a program to exploit the largest memory subsystem available to contemporary machines, that of the hard disk, CDROM, or tape drives. The file, regardless of which type of media that it resides on, can be thought of as a massive array of data. You can read from this data source, write to it, and create new instances and eliminate them from within your programs. Two primary modes of access to files are available: the low-level operations that work with bytes of data, and high-level operations that utilize data streams to store and retrieve the values of variables. We explore both methods in this chapter.
Low-Level File Operations
A file can be considered a document that has been stored and that will be accessed as a stream of bytes, such as depicted in Figure 7.1. Files are operating system (OS) resources, and requests must be made to the operating system for access to them. The operating system assigns a filename to a file, and the specifics of what characters are permitted in the name as well as the length are system-dependent. Typically, filenames have a prefix and suffix, and by now you have probably encountered this in your programming. The prefix describes the contents of the file, such as “myprog,” to describe a program that you are writing. The suffix indicates the type of file, such as “.c” for C source or “.for” for Fortran source, and so on.
- Type
- Chapter
- Information
- Fundamentals of Engineering Programming with C and Fortran , pp. 149 - 159Publisher: Cambridge University PressPrint publication year: 1998